blob: ab457b7a68ad6a47c468049419a8f27cab409cec [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 Beckerd5258fa2019-02-07 12:27:42 +0000398#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
401 dst->peer_cert_digest_len = src->peer_cert_digest_len;
402 dst->peer_cert_digest =
403 mbedtls_calloc( 1, dst->peer_cert_digest_len );
404 if( dst->peer_cert_digest == NULL )
405 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
406
407 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
408 src->peer_cert_digest_len );
409 dst->peer_cert_digest_type = src->peer_cert_digest_type;
410 }
411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
412
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],
754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
755 int encrypt_then_mac,
756#endif
757#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
758 int trunc_hmac,
759#endif
760#if defined(MBEDTLS_ZLIB_SUPPORT)
761 int compression,
762#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200763 ssl_tls_prf_t tls_prf,
764 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 int minor_ver,
766 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200767 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000768{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 unsigned char keyblk[256];
771 unsigned char *key1;
772 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100773 unsigned char *mac_enc;
774 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000775 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200776 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000777 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000778 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 const mbedtls_cipher_info_t *cipher_info;
780 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100781
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200782#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
783 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
784 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200785 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200786 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000787#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000788
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200789 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200791 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000792#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200793 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200795 /*
796 * Get various info structures
797 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200798 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200799 if( ciphersuite_info == NULL )
800 {
801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200802 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
804 }
805
Hanno Becker8759e162017-12-27 21:34:08 +0000806 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100807 if( cipher_info == NULL )
808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000810 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
813
Hanno Becker8759e162017-12-27 21:34:08 +0000814 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 if( md_info == NULL )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000818 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100820 }
821
Hanno Beckera5a2b082019-05-15 14:03:01 +0100822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 /* Copy own and peer's CID if the use of the CID
824 * extension has been negotiated. */
825 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
826 {
827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100828
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100830 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100831 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100832 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100833
834 transform->out_cid_len = ssl->handshake->peer_cid_len;
835 memcpy( transform->out_cid, ssl->handshake->peer_cid,
836 ssl->handshake->peer_cid_len );
837 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
838 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100839 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200843 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000844 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200845 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100846 if( ret != 0 )
847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 return( ret );
850 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200853 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200854 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 /*
859 * Determine the appropriate key, IV and MAC length.
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861
Hanno Beckere7f2df02017-12-27 08:17:40 +0000862 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200863
Hanno Beckerf1229442018-01-03 15:32:31 +0000864#if defined(MBEDTLS_GCM_C) || \
865 defined(MBEDTLS_CCM_C) || \
866 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200868 cipher_info->mode == MBEDTLS_MODE_CCM ||
869 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 {
Hanno Becker8759e162017-12-27 21:34:08 +0000871 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200873 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000875 transform->taglen =
876 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200878 /* All modes haves 96-bit IVs;
879 * GCM and CCM has 4 implicit and 8 explicit bytes
880 * ChachaPoly has all 12 bytes implicit
881 */
Paul Bakker68884e32013-01-07 18:20:04 +0100882 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200883 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
884 transform->fixed_ivlen = 12;
885 else
886 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200888 /* Minimum length of encrypted record */
889 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000890 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100891 }
892 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000893#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
894#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
895 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
896 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100897 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200898 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
900 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200903 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len = mbedtls_md_get_size( md_info );
908 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200911 /*
912 * If HMAC is to be truncated, we shall keep the leftmost bytes,
913 * (rfc 6066 page 13 or rfc 2104 section 4),
914 * so we only need to adjust the length here.
915 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200916 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000919
920#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
921 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000922 * HMAC implementation which also truncates the key
923 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000924 mac_key_len = transform->maclen;
925#endif
926 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200928
929 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100930 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200932 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934 transform->minlen = transform->maclen;
935 else
Paul Bakker68884e32013-01-07 18:20:04 +0100936 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 /*
938 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100939 * 1. if EtM is in use: one block plus MAC
940 * otherwise: * first multiple of blocklen greater than maclen
941 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200944 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100945 {
946 transform->minlen = transform->maclen
947 + cipher_info->block_size;
948 }
949 else
950#endif
951 {
952 transform->minlen = transform->maclen
953 + cipher_info->block_size
954 - transform->maclen % cipher_info->block_size;
955 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200958 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
959 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200960 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100961 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200964 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
965 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200966 {
967 transform->minlen += transform->ivlen;
968 }
969 else
970#endif
971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200974 }
Paul Bakker68884e32013-01-07 18:20:04 +0100975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000977 else
978#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Hanno Beckere7f2df02017-12-27 08:17:40 +0000984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
985 (unsigned) keylen,
986 (unsigned) transform->minlen,
987 (unsigned) transform->ivlen,
988 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Finally setup the cipher contexts, IVs and MAC secrets.
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200994 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000997 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001002 /*
1003 * This is not used in TLS v1.1.
1004 */
Paul Bakker48916f92012-09-16 19:57:18 +00001005 iv_copy_len = ( transform->fixed_ivlen ) ?
1006 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001007 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1008 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 }
1011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_CLI_C */
1013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001014 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001016 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001017 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Hanno Becker81c7b182017-11-09 18:39:33 +00001019 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001020 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001022 /*
1023 * This is not used in TLS v1.1.
1024 */
Paul Bakker48916f92012-09-16 19:57:18 +00001025 iv_copy_len = ( transform->fixed_ivlen ) ?
1026 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001027 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1028 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Becker92231322018-01-03 15:32:51 +00001038#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001040 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001041 {
Hanno Becker92231322018-01-03 15:32:51 +00001042 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 }
1047
Hanno Becker81c7b182017-11-09 18:39:33 +00001048 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1049 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001050 }
1051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1054 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001055 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001056 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001057 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1058 For AEAD-based ciphersuites, there is nothing to do here. */
1059 if( mac_key_len != 0 )
1060 {
1061 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1062 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1063 }
Paul Bakker68884e32013-01-07 18:20:04 +01001064 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 else
1066#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001070 }
Hanno Becker92231322018-01-03 15:32:51 +00001071#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1074 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001075 {
1076 int ret = 0;
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001079
Hanno Beckere7f2df02017-12-27 08:17:40 +00001080 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001081 transform->iv_enc, transform->iv_dec,
1082 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001083 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001084 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 }
1089 }
Hanno Becker92231322018-01-03 15:32:51 +00001090#else
1091 ((void) mac_dec);
1092 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001094
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001095#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1096 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001097 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001099 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001100 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001101 iv_copy_len );
1102 }
1103#endif
1104
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001105 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 return( ret );
1110 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001111
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001112 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001113 cipher_info ) ) != 0 )
1114 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 return( ret );
1117 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001120 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001124 return( ret );
1125 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001128 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 return( ret );
1133 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_CIPHER_MODE_CBC)
1136 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1139 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001142 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1146 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001149 return( ret );
1150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001154 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001156 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001158 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001161
Paul Bakker48916f92012-09-16 19:57:18 +00001162 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1163 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001165 if( deflateInit( &transform->ctx_deflate,
1166 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001167 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1170 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 }
1172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 return( 0 );
1176}
1177
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001178/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001179 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001180 *
1181 * Inputs:
1182 * - SSL/TLS minor version
1183 * - hash associated with the ciphersuite (only used by TLS 1.2)
1184 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001185 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001186 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1187 */
1188static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1189 int minor_ver,
1190 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001192#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1193 (void) hash;
1194#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001197 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198 {
1199 handshake->tls_prf = ssl3_prf;
1200 handshake->calc_verify = ssl_calc_verify_ssl;
1201 handshake->calc_finished = ssl_calc_finished_ssl;
1202 }
1203 else
1204#endif
1205#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001206 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001207 {
1208 handshake->tls_prf = tls1_prf;
1209 handshake->calc_verify = ssl_calc_verify_tls;
1210 handshake->calc_finished = ssl_calc_finished_tls;
1211 }
1212 else
1213#endif
1214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1215#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001216 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1217 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001218 {
1219 handshake->tls_prf = tls_prf_sha384;
1220 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1221 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1222 }
1223 else
1224#endif
1225#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001226 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001227 {
1228 handshake->tls_prf = tls_prf_sha256;
1229 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1230 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1231 }
1232 else
1233#endif
1234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1235 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1237 }
1238
1239 return( 0 );
1240}
1241
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001242/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001243 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 *
1245 * Parameters:
1246 * [in/out] handshake
1247 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1248 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001249 * [out] master
1250 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001251 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001253 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001254 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255{
1256 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257
1258#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001259 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260 (void) ssl;
1261#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001264 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1266 return( 0 );
1267 }
1268
1269 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1270 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271
1272#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001273 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001274 {
1275 unsigned char session_hash[48];
1276 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001277
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001278 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001279
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001280 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1281 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001282
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001284 "extended master secret",
1285 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001286 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287 }
1288 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001289#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001290 {
1291 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1292 "master secret",
1293 handshake->randbytes, 64,
1294 master, 48 );
1295 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001296 if( ret != 0 )
1297 {
1298 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1299 return( ret );
1300 }
1301
1302 mbedtls_platform_zeroize( handshake->premaster,
1303 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001304
1305 return( 0 );
1306}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001308int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1309{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001310 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001311 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1312 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1315
1316 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001317 ret = ssl_set_handshake_prfs( ssl->handshake,
1318 ssl->minor_ver,
1319 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001320 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001321 {
1322 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001323 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001324 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001325
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001326 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001328 ssl->session_negotiate->master,
1329 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001330 if( ret != 0 )
1331 {
1332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1333 return( ret );
1334 }
1335
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001336 /* Swap the client and server random values:
1337 * - MS derivation wanted client+server (RFC 5246 8.1)
1338 * - key derivation wants server+client (RFC 5246 6.3) */
1339 {
1340 unsigned char tmp[64];
1341 memcpy( tmp, ssl->handshake->randbytes, 64 );
1342 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1343 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1344 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1345 }
1346
1347 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001349 ssl->session_negotiate->ciphersuite,
1350 ssl->session_negotiate->master,
1351#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1352 ssl->session_negotiate->encrypt_then_mac,
1353#endif
1354#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1355 ssl->session_negotiate->trunc_hmac,
1356#endif
1357#if defined(MBEDTLS_ZLIB_SUPPORT)
1358 ssl->session_negotiate->compression,
1359#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001360 ssl->handshake->tls_prf,
1361 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001362 ssl->minor_ver,
1363 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001364 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001365 if( ret != 0 )
1366 {
1367 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1368 return( ret );
1369 }
1370
1371 /* We no longer need Server/ClientHello.random values */
1372 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1373 sizeof( ssl->handshake->randbytes ) );
1374
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001375 /* Allocate compression buffer */
1376#if defined(MBEDTLS_ZLIB_SUPPORT)
1377 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1378 ssl->compress_buf == NULL )
1379 {
1380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1381 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1382 if( ssl->compress_buf == NULL )
1383 {
1384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001385 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1387 }
1388 }
1389#endif
1390
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1392
1393 return( 0 );
1394}
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001397void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1398 unsigned char hash[36],
1399 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001401 mbedtls_md5_context md5;
1402 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 unsigned char pad_1[48];
1404 unsigned char pad_2[48];
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001408 mbedtls_md5_init( &md5 );
1409 mbedtls_sha1_init( &sha1 );
1410
1411 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1412 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Paul Bakker380da532012-04-18 16:10:25 +00001414 memset( pad_1, 0x36, 48 );
1415 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001417 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1418 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1419 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001421 mbedtls_md5_starts_ret( &md5 );
1422 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1423 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1424 mbedtls_md5_update_ret( &md5, hash, 16 );
1425 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1428 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1429 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001431 mbedtls_sha1_starts_ret( &sha1 );
1432 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1433 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1434 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1435 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001437 *hlen = 36;
1438
1439 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001442 mbedtls_md5_free( &md5 );
1443 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001444
Paul Bakker380da532012-04-18 16:10:25 +00001445 return;
1446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001450void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1451 unsigned char hash[36],
1452 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001453{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_md5_context md5;
1455 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001459 mbedtls_md5_init( &md5 );
1460 mbedtls_sha1_init( &sha1 );
1461
1462 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1463 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001465 mbedtls_md5_finish_ret( &md5, hash );
1466 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001468 *hlen = 36;
1469
1470 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001473 mbedtls_md5_free( &md5 );
1474 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001475
Paul Bakker380da532012-04-18 16:10:25 +00001476 return;
1477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1481#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001482void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1483 unsigned char hash[32],
1484 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001485{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_init( &sha256 );
1489
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001493 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001495 *hlen = 32;
1496
1497 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001500 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001501
Paul Bakker380da532012-04-18 16:10:25 +00001502 return;
1503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001507void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1508 unsigned char hash[48],
1509 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001510{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001511 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001512
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001513 mbedtls_sha512_init( &sha512 );
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001516
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001518 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001520 *hlen = 48;
1521
1522 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001526
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 return;
1528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_SHA512_C */
1530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1533int 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 +02001534{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 unsigned char *p = ssl->handshake->premaster;
1536 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001537 const unsigned char *psk = ssl->conf->psk;
1538 size_t psk_len = ssl->conf->psk_len;
1539
1540 /* If the psk callback was called, use its result */
1541 if( ssl->handshake->psk != NULL )
1542 {
1543 psk = ssl->handshake->psk;
1544 psk_len = ssl->handshake->psk_len;
1545 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546
1547 /*
1548 * PMS = struct {
1549 * opaque other_secret<0..2^16-1>;
1550 * opaque psk<0..2^16-1>;
1551 * };
1552 * with "other_secret" depending on the particular key exchange
1553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1555 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001556 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001557 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001560 *(p++) = (unsigned char)( psk_len >> 8 );
1561 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001562
1563 if( end < p || (size_t)( end - p ) < psk_len )
1564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1565
1566 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001567 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001573 {
1574 /*
1575 * other_secret already set by the ClientKeyExchange message,
1576 * and is 48 bytes long
1577 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001578 if( end - p < 2 )
1579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1580
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001581 *p++ = 0;
1582 *p++ = 48;
1583 p += 48;
1584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1587#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1588 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001590 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001591 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001593 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001595 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001601 *(p++) = (unsigned char)( len >> 8 );
1602 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 p += len;
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001606 }
1607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1609#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1610 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001612 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 size_t zlen;
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001616 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001617 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001620 return( ret );
1621 }
1622
1623 *(p++) = (unsigned char)( zlen >> 8 );
1624 *(p++) = (unsigned char)( zlen );
1625 p += zlen;
1626
Janos Follath3fbdada2018-08-15 10:26:53 +01001627 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1628 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 }
1630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636
1637 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001638 if( end - p < 2 )
1639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001640
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001641 *(p++) = (unsigned char)( psk_len >> 8 );
1642 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001643
1644 if( end < p || (size_t)( end - p ) < psk_len )
1645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647 memcpy( p, psk, psk_len );
1648 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001649
1650 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1651
1652 return( 0 );
1653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001657/*
1658 * SSLv3.0 MAC functions
1659 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001660#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001661static void ssl_mac( mbedtls_md_context_t *md_ctx,
1662 const unsigned char *secret,
1663 const unsigned char *buf, size_t len,
1664 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001665 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666{
1667 unsigned char header[11];
1668 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001669 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1671 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001672
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001673 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001675 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001676 else
Paul Bakker68884e32013-01-07 18:20:04 +01001677 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 memcpy( header, ctr, 8 );
1680 header[ 8] = (unsigned char) type;
1681 header[ 9] = (unsigned char)( len >> 8 );
1682 header[10] = (unsigned char)( len );
1683
Paul Bakker68884e32013-01-07 18:20:04 +01001684 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_md_starts( md_ctx );
1686 mbedtls_md_update( md_ctx, secret, md_size );
1687 mbedtls_md_update( md_ctx, padding, padlen );
1688 mbedtls_md_update( md_ctx, header, 11 );
1689 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001690 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 memset( padding, 0x5C, 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 );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001696 mbedtls_md_update( md_ctx, out, md_size );
1697 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001700
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001701/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001702 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001703#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001704 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1706 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1707/* This function makes sure every byte in the memory region is accessed
1708 * (in ascending addresses order) */
1709static void ssl_read_memory( unsigned char *p, size_t len )
1710{
1711 unsigned char acc = 0;
1712 volatile unsigned char force;
1713
1714 for( ; len != 0; p++, len-- )
1715 acc ^= *p;
1716
1717 force = acc;
1718 (void) force;
1719}
1720#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1721
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001722/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001724 */
Hanno Becker3307b532017-12-27 21:37:21 +00001725
Hanno Beckera5a2b082019-05-15 14:03:01 +01001726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001727/* This functions transforms a DTLS plaintext fragment and a record content
1728 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001729 *
1730 * struct {
1731 * opaque content[DTLSPlaintext.length];
1732 * ContentType real_type;
1733 * uint8 zeros[length_of_padding];
1734 * } DTLSInnerPlaintext;
1735 *
1736 * Input:
1737 * - `content`: The beginning of the buffer holding the
1738 * plaintext to be wrapped.
1739 * - `*content_size`: The length of the plaintext in Bytes.
1740 * - `max_len`: The number of Bytes available starting from
1741 * `content`. This must be `>= *content_size`.
1742 * - `rec_type`: The desired record content type.
1743 *
1744 * Output:
1745 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1746 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1747 *
1748 * Returns:
1749 * - `0` on success.
1750 * - A negative error code if `max_len` didn't offer enough space
1751 * for the expansion.
1752 */
1753static int ssl_cid_build_inner_plaintext( unsigned char *content,
1754 size_t *content_size,
1755 size_t remaining,
1756 uint8_t rec_type )
1757{
1758 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001759 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1760 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1761 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001762
1763 /* Write real content type */
1764 if( remaining == 0 )
1765 return( -1 );
1766 content[ len ] = rec_type;
1767 len++;
1768 remaining--;
1769
1770 if( remaining < pad )
1771 return( -1 );
1772 memset( content + len, 0, pad );
1773 len += pad;
1774 remaining -= pad;
1775
1776 *content_size = len;
1777 return( 0 );
1778}
1779
Hanno Becker7dc25772019-05-20 15:08:01 +01001780/* This function parses a DTLSInnerPlaintext structure.
1781 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001782static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1783 size_t *content_size,
1784 uint8_t *rec_type )
1785{
1786 size_t remaining = *content_size;
1787
1788 /* Determine length of padding by skipping zeroes from the back. */
1789 do
1790 {
1791 if( remaining == 0 )
1792 return( -1 );
1793 remaining--;
1794 } while( content[ remaining ] == 0 );
1795
1796 *content_size = remaining;
1797 *rec_type = content[ remaining ];
1798
1799 return( 0 );
1800}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001801#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001802
Hanno Becker99abf512019-05-20 14:50:53 +01001803/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001804 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001805static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001807 mbedtls_record *rec )
1808{
Hanno Becker99abf512019-05-20 14:50:53 +01001809 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001810 *
1811 * additional_data = seq_num + TLSCompressed.type +
1812 * TLSCompressed.version + TLSCompressed.length;
1813 *
Hanno Becker99abf512019-05-20 14:50:53 +01001814 * For the CID extension, this is extended as follows
1815 * (quoting draft-ietf-tls-dtls-connection-id-05,
1816 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001817 *
1818 * additional_data = seq_num + DTLSPlaintext.type +
1819 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001820 * cid +
1821 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001822 * length_of_DTLSInnerPlaintext;
1823 */
1824
Hanno Becker3307b532017-12-27 21:37:21 +00001825 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1826 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001827 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001828
Hanno Beckera5a2b082019-05-15 14:03:01 +01001829#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001830 if( rec->cid_len != 0 )
1831 {
1832 memcpy( add_data + 11, rec->cid, rec->cid_len );
1833 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1834 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1835 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1836 *add_data_len = 13 + 1 + rec->cid_len;
1837 }
1838 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001840 {
1841 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1842 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1843 *add_data_len = 13;
1844 }
Hanno Becker3307b532017-12-27 21:37:21 +00001845}
1846
Hanno Becker611a83b2018-01-03 14:27:32 +00001847int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1848 mbedtls_ssl_transform *transform,
1849 mbedtls_record *rec,
1850 int (*f_rng)(void *, unsigned char *, size_t),
1851 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001854 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001855 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001856 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001857 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001858 size_t post_avail;
1859
1860 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001861#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001862 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001863 ((void) ssl);
1864#endif
1865
1866 /* The PRNG is used for dynamic IV generation that's used
1867 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1868#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1869 ( defined(MBEDTLS_AES_C) || \
1870 defined(MBEDTLS_ARIA_C) || \
1871 defined(MBEDTLS_CAMELLIA_C) ) && \
1872 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1873 ((void) f_rng);
1874 ((void) p_rng);
1875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Hanno Becker3307b532017-12-27 21:37:21 +00001879 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880 {
Hanno Becker3307b532017-12-27 21:37:21 +00001881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1883 }
Hanno Becker505089d2019-05-01 09:45:57 +01001884 if( rec == NULL
1885 || rec->buf == NULL
1886 || rec->buf_len < rec->data_offset
1887 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001889 || rec->cid_len != 0
1890#endif
1891 )
Hanno Becker3307b532017-12-27 21:37:21 +00001892 {
1893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001895 }
1896
Hanno Becker3307b532017-12-27 21:37:21 +00001897 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001898 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001900 data, rec->data_len );
1901
1902 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1903
1904 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1905 {
1906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1907 (unsigned) rec->data_len,
1908 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1910 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001911
Hanno Beckera5a2b082019-05-15 14:03:01 +01001912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001913 /*
1914 * Add CID information
1915 */
1916 rec->cid_len = transform->out_cid_len;
1917 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1918 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001919
1920 if( rec->cid_len != 0 )
1921 {
1922 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001923 * Wrap plaintext into DTLSInnerPlaintext structure.
1924 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001926 * Note that this changes `rec->data_len`, and hence
1927 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001928 */
1929 if( ssl_cid_build_inner_plaintext( data,
1930 &rec->data_len,
1931 post_avail,
1932 rec->type ) != 0 )
1933 {
1934 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1935 }
1936
1937 rec->type = MBEDTLS_SSL_MSG_CID;
1938 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001939#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001940
Hanno Becker92c930f2019-04-29 17:31:37 +01001941 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001944 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( mode == MBEDTLS_MODE_STREAM ||
1948 ( mode == MBEDTLS_MODE_CBC
1949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001950 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001951#endif
1952 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Hanno Becker3307b532017-12-27 21:37:21 +00001954 if( post_avail < transform->maclen )
1955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1957 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1958 }
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001961 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001962 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001963 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001964 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1965 data, rec->data_len, rec->ctr, rec->type, mac );
1966 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 }
1968 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1971 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001972 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Hanno Becker992b6872017-11-09 18:57:39 +00001974 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1975
Hanno Beckere83efe62019-04-29 13:52:53 +01001976 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001979 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001980 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1981 data, rec->data_len );
1982 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1983 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1984
1985 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 }
1987 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 }
1993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1995 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996
Hanno Becker3307b532017-12-27 21:37:21 +00001997 rec->data_len += transform->maclen;
1998 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001999 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002000 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002001#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002003 /*
2004 * Encrypt
2005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2007 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002010 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002012 "including %d bytes of padding",
2013 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Hanno Becker3307b532017-12-27 21:37:21 +00002015 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2016 transform->iv_enc, transform->ivlen,
2017 data, rec->data_len,
2018 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002021 return( ret );
2022 }
2023
Hanno Becker3307b532017-12-27 21:37:21 +00002024 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakker68884e32013-01-07 18:20:04 +01002030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002032
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002033#if defined(MBEDTLS_GCM_C) || \
2034 defined(MBEDTLS_CCM_C) || \
2035 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 mode == MBEDTLS_MODE_CCM ||
2038 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002039 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002040 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002042 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002043
Hanno Becker3307b532017-12-27 21:37:21 +00002044 /* Check that there's space for both the authentication tag
2045 * and the explicit IV before and after the record content. */
2046 if( post_avail < transform->taglen ||
2047 rec->data_offset < explicit_iv_len )
2048 {
2049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2050 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2051 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002052
Paul Bakker68884e32013-01-07 18:20:04 +01002053 /*
2054 * Generate IV
2055 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002056 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2057 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002058 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002059 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002060 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2061 explicit_iv_len );
2062 /* Prefix record content with explicit IV. */
2063 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 }
2065 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2066 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002067 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 unsigned char i;
2069
2070 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2071
2072 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002073 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 }
2075 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002076 {
2077 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002080 }
2081
Hanno Beckere83efe62019-04-29 13:52:53 +01002082 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002083
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002084 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2085 iv, transform->ivlen );
2086 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002087 data - explicit_iv_len, explicit_iv_len );
2088 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002089 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002091 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002092 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002093
Paul Bakker68884e32013-01-07 18:20:04 +01002094 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002095 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002096 */
Hanno Becker3307b532017-12-27 21:37:21 +00002097
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002099 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002100 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002101 data, rec->data_len, /* source */
2102 data, &rec->data_len, /* destination */
2103 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002106 return( ret );
2107 }
2108
Hanno Becker3307b532017-12-27 21:37:21 +00002109 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2110 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 rec->data_len += transform->taglen + explicit_iv_len;
2113 rec->data_offset -= explicit_iv_len;
2114 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002115 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2119#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002120 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002123 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002124 size_t padlen, i;
2125 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 /* Currently we're always using minimal padding
2128 * (up to 255 bytes would be allowed). */
2129 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2130 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 padlen = 0;
2132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 /* Check there's enough space in the buffer for the padding. */
2134 if( post_avail < padlen + 1 )
2135 {
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2137 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2138 }
2139
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002141 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Hanno Becker3307b532017-12-27 21:37:21 +00002143 rec->data_len += padlen + 1;
2144 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002147 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002148 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2149 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150 */
Hanno Becker3307b532017-12-27 21:37:21 +00002151 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152 {
Hanno Becker3307b532017-12-27 21:37:21 +00002153 if( f_rng == NULL )
2154 {
2155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2157 }
2158
2159 if( rec->data_offset < transform->ivlen )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2162 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2163 }
2164
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002165 /*
2166 * Generate IV
2167 */
Hanno Becker3307b532017-12-27 21:37:21 +00002168 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002169 if( ret != 0 )
2170 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 memcpy( data - transform->ivlen, transform->iv_enc,
2173 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002180 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002181 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Hanno Becker3307b532017-12-27 21:37:21 +00002183 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2184 transform->iv_enc,
2185 transform->ivlen,
2186 data, rec->data_len,
2187 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002190 return( ret );
2191 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002192
Hanno Becker3307b532017-12-27 21:37:21 +00002193 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002200 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002201 {
2202 /*
2203 * Save IV in SSL3 and TLS1
2204 */
Hanno Becker3307b532017-12-27 21:37:21 +00002205 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2206 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 }
Hanno Becker3307b532017-12-27 21:37:21 +00002208 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002209#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002210 {
2211 data -= transform->ivlen;
2212 rec->data_offset -= transform->ivlen;
2213 rec->data_len += transform->ivlen;
2214 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002219 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2220
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221 /*
2222 * MAC(MAC_write_key, seq_num +
2223 * TLSCipherText.type +
2224 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002225 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 * IV + // except for TLS 1.0
2227 * ENC(content + padding + padding_length));
2228 */
Hanno Becker3307b532017-12-27 21:37:21 +00002229
2230 if( post_avail < transform->maclen)
2231 {
2232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2233 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2234 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002235
Hanno Beckere83efe62019-04-29 13:52:53 +01002236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002239 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002240 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241
Hanno Becker3307b532017-12-27 21:37:21 +00002242 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002243 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002244 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2245 data, rec->data_len );
2246 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2247 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002248
Hanno Becker3307b532017-12-27 21:37:21 +00002249 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250
Hanno Becker3307b532017-12-27 21:37:21 +00002251 rec->data_len += transform->maclen;
2252 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002259 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265 /* Make extra sure authentication was performed, exactly once */
2266 if( auth_done != 1 )
2267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002270 }
2271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
2274 return( 0 );
2275}
2276
Hanno Becker611a83b2018-01-03 14:27:32 +00002277int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2278 mbedtls_ssl_transform *transform,
2279 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002284#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002285 size_t padlen = 0, correct = 1;
2286#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002287 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002288 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002289 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290
Hanno Becker611a83b2018-01-03 14:27:32 +00002291#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002292 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 ((void) ssl);
2294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002297 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2301 }
2302 if( rec == NULL ||
2303 rec->buf == NULL ||
2304 rec->buf_len < rec->data_offset ||
2305 rec->buf_len - rec->data_offset < rec->data_len )
2306 {
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 }
2310
Hanno Becker4c6876b2017-12-27 21:28:58 +00002311 data = rec->buf + rec->data_offset;
2312 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Hanno Beckera5a2b082019-05-15 14:03:01 +01002314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002315 /*
2316 * Match record's CID with incoming CID.
2317 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002318 if( rec->cid_len != transform->in_cid_len ||
2319 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2320 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002321 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002322 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2326 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002327 {
2328 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002329 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2330 transform->iv_dec,
2331 transform->ivlen,
2332 data, rec->data_len,
2333 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002336 return( ret );
2337 }
2338
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 }
Paul Bakker68884e32013-01-07 18:20:04 +01002345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347#if defined(MBEDTLS_GCM_C) || \
2348 defined(MBEDTLS_CCM_C) || \
2349 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 mode == MBEDTLS_MODE_CCM ||
2352 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002353 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002356
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 /*
2358 * Compute and update sizes
2359 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002360 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002363 "+ taglen (%d)", rec->data_len,
2364 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002366 }
Paul Bakker68884e32013-01-07 18:20:04 +01002367
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002368 /*
2369 * Prepare IV
2370 */
2371 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2372 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002373 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002375 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 }
2378 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2379 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002380 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002381 unsigned char i;
2382
2383 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2384
2385 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 }
2388 else
2389 {
2390 /* Reminder if we ever add an AEAD mode with a different size */
2391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2393 }
2394
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 data += explicit_iv_len;
2396 rec->data_offset += explicit_iv_len;
2397 rec->data_len -= explicit_iv_len + transform->taglen;
2398
Hanno Beckere83efe62019-04-29 13:52:53 +01002399 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002401 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002402
2403 memcpy( transform->iv_dec + transform->fixed_ivlen,
2404 data - explicit_iv_len, explicit_iv_len );
2405
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002406 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002407 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002408 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Paul Bakker68884e32013-01-07 18:20:04 +01002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002412 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002413 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002414 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2415 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002416 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002417 data, rec->data_len,
2418 data, &olen,
2419 data + rec->data_len,
2420 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2425 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002426
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002427 return( ret );
2428 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002429 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430
Hanno Becker4c6876b2017-12-27 21:28:58 +00002431 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2439#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002440 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002443 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 /*
Paul Bakker45829992013-01-03 14:52:21 +01002446 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002449 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2450 {
2451 /* The ciphertext is prefixed with the CBC IV. */
2452 minlen += transform->ivlen;
2453 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002454#endif
Paul Bakker45829992013-01-03 14:52:21 +01002455
Hanno Becker4c6876b2017-12-27 21:28:58 +00002456 /* Size considerations:
2457 *
2458 * - The CBC cipher text must not be empty and hence
2459 * at least of size transform->ivlen.
2460 *
2461 * Together with the potential IV-prefix, this explains
2462 * the first of the two checks below.
2463 *
2464 * - The record must contain a MAC, either in plain or
2465 * encrypted, depending on whether Encrypt-then-MAC
2466 * is used or not.
2467 * - If it is, the message contains the IV-prefix,
2468 * the CBC ciphertext, and the MAC.
2469 * - If it is not, the padded plaintext, and hence
2470 * the CBC ciphertext, has at least length maclen + 1
2471 * because there is at least the padding length byte.
2472 *
2473 * As the CBC ciphertext is not empty, both cases give the
2474 * lower bound minlen + maclen + 1 on the record size, which
2475 * we test for in the second check below.
2476 */
2477 if( rec->data_len < minlen + transform->ivlen ||
2478 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 "+ 1 ) ( + expl IV )", rec->data_len,
2482 transform->ivlen,
2483 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002485 }
2486
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487 /*
2488 * Authenticate before decrypt if enabled
2489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002491 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 {
Hanno Becker992b6872017-11-09 18:57:39 +00002493 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2498 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Beckere83efe62019-04-29 13:52:53 +01002500 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002501
Hanno Beckere83efe62019-04-29 13:52:53 +01002502 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2503 add_data_len );
2504 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2505 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002506 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2507 data, rec->data_len );
2508 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2509 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002510
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2512 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002514 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2517 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002525
2526 /*
2527 * Check length sanity
2528 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002529 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002534 }
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002537 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002538 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002541 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002542 /* This is safe because data_len >= minlen + maclen + 1 initially,
2543 * and at this point we have at most subtracted maclen (note that
2544 * minlen == transform->ivlen here). */
2545 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 data += transform->ivlen;
2548 rec->data_offset += transform->ivlen;
2549 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2554 transform->iv_dec, transform->ivlen,
2555 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002558 return( ret );
2559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002568 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002569 {
2570 /*
2571 * Save IV in SSL3 and TLS1
2572 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002573 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2574 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578 /* Safe since data_len >= minlen + maclen + 1, so after having
2579 * subtracted at most minlen and maclen up to this point,
2580 * data_len > 0. */
2581 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 if( auth_done == 1 )
2584 {
2585 correct *= ( rec->data_len >= padlen + 1 );
2586 padlen *= ( rec->data_len >= padlen + 1 );
2587 }
2588 else
Paul Bakker45829992013-01-03 14:52:21 +01002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( rec->data_len < transform->maclen + padlen + 1 )
2592 {
2593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2594 rec->data_len,
2595 transform->maclen,
2596 padlen + 1 ) );
2597 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002598#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599
2600 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2601 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604 padlen++;
2605
2606 /* Regardless of the validity of the padding,
2607 * we have data_len >= padlen here. */
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_DEBUG_ALL)
2615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 "should be no more than %d",
2617 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002618#endif
Paul Bakker45829992013-01-03 14:52:21 +01002619 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621 }
2622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2624#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2625 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 /* The padding check involves a series of up to 256
2629 * consecutive memory reads at the end of the record
2630 * plaintext buffer. In order to hide the length and
2631 * validity of the padding, always perform exactly
2632 * `min(256,plaintext_len)` reads (but take into account
2633 * only the last `padlen` bytes for the padding check). */
2634 size_t pad_count = 0;
2635 size_t real_count = 0;
2636 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002637
Hanno Becker4c6876b2017-12-27 21:28:58 +00002638 /* Index of first padding byte; it has been ensured above
2639 * that the subtraction is safe. */
2640 size_t const padding_idx = rec->data_len - padlen;
2641 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2642 size_t const start_idx = rec->data_len - num_checks;
2643 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002646 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002647 real_count |= ( idx >= padding_idx );
2648 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002649 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002653 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002655#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2660 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002664 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 /* If the padding was found to be invalid, padlen == 0
2667 * and the subtraction is safe. If the padding was found valid,
2668 * padlen hasn't been changed and the previous assertion
2669 * data_len >= padlen still holds. */
2670 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002674 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002680#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002682 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002686 * Authenticate if not done yet.
2687 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002689#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 {
Hanno Becker992b6872017-11-09 18:57:39 +00002692 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002693
Hanno Becker4c6876b2017-12-27 21:28:58 +00002694 /* If the initial value of padlen was such that
2695 * data_len < maclen + padlen + 1, then padlen
2696 * got reset to 1, and the initial check
2697 * data_len >= minlen + maclen + 1
2698 * guarantees that at this point we still
2699 * have at least data_len >= maclen.
2700 *
2701 * If the initial value of padlen was such that
2702 * data_len >= maclen + padlen + 1, then we have
2703 * subtracted either padlen + 1 (if the padding was correct)
2704 * or 0 (if the padding was incorrect) since then,
2705 * hence data_len >= maclen in any case.
2706 */
2707 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Hanno Beckere83efe62019-04-29 13:52:53 +01002709 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002712 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002714 ssl_mac( &transform->md_ctx_dec,
2715 transform->mac_dec,
2716 data, rec->data_len,
2717 rec->ctr, rec->type,
2718 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 }
2720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2722#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2723 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002724 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 {
2726 /*
2727 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002728 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002729 *
2730 * Known timing attacks:
2731 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2732 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002733 * To compensate for different timings for the MAC calculation
2734 * depending on how much padding was removed (which is determined
2735 * by padlen), process extra_run more blocks through the hash
2736 * function.
2737 *
2738 * The formula in the paper is
2739 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2740 * where L1 is the size of the header plus the decrypted message
2741 * plus CBC padding and L2 is the size of the header plus the
2742 * decrypted message. This is for an underlying hash function
2743 * with 64-byte blocks.
2744 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2745 * correctly. We round down instead of up, so -56 is the correct
2746 * value for our calculations instead of -55.
2747 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002748 * Repeat the formula rather than defining a block_size variable.
2749 * This avoids requiring division by a variable at runtime
2750 * (which would be marginally less efficient and would require
2751 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002752 */
2753 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002754 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002755
2756 /*
2757 * The next two sizes are the minimum and maximum values of
2758 * in_msglen over all padlen values.
2759 *
2760 * They're independent of padlen, since we previously did
2761 * in_msglen -= padlen.
2762 *
2763 * Note that max_len + maclen is never more than the buffer
2764 * length, as we previously did in_msglen -= maclen too.
2765 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002766 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002767 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2768
Hanno Becker4c6876b2017-12-27 21:28:58 +00002769 memset( tmp, 0, sizeof( tmp ) );
2770
2771 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002772 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002773#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2774 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002775 case MBEDTLS_MD_MD5:
2776 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002779 extra_run =
2780 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2781 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002782 break;
2783#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002784#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002786 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 extra_run =
2788 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2789 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 break;
2791#endif
2792 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2795 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002796
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002797 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002798
Hanno Beckere83efe62019-04-29 13:52:53 +01002799 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2800 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002801 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2802 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002803 /* Make sure we access everything even when padlen > 0. This
2804 * makes the synchronisation requirements for just-in-time
2805 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002806 ssl_read_memory( data + rec->data_len, padlen );
2807 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002808
2809 /* Call mbedtls_md_process at least once due to cache attacks
2810 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002811 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002812 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002813
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815
2816 /* Make sure we access all the memory that could contain the MAC,
2817 * before we check it in the next code block. This makes the
2818 * synchronisation requirements for just-in-time Prime+Probe
2819 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 ssl_read_memory( data + min_len,
2821 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 }
2823 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2825 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002831#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002832 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2833 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Hanno Becker4c6876b2017-12-27 21:28:58 +00002836 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2837 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_DEBUG_ALL)
2840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002841#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002842 correct = 0;
2843 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002844 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002845 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002846
2847 /*
2848 * Finally check the correct flag
2849 */
2850 if( correct == 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002852#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002853
2854 /* Make extra sure authentication was performed, exactly once */
2855 if( auth_done != 1 )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Hanno Beckera5a2b082019-05-15 14:03:01 +01002861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002862 if( rec->cid_len != 0 )
2863 {
2864 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2865 &rec->type );
2866 if( ret != 0 )
2867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
2873 return( 0 );
2874}
2875
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002876#undef MAC_NONE
2877#undef MAC_PLAINTEXT
2878#undef MAC_CIPHERTEXT
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881/*
2882 * Compression/decompression functions
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002885{
2886 int ret;
2887 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002888 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002890 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002894 if( len_pre == 0 )
2895 return( 0 );
2896
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 memcpy( msg_pre, ssl->out_msg, len_pre );
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Paul Bakker48916f92012-09-16 19:57:18 +00002905 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2906 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2907 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002908 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911 if( ret != Z_OK )
2912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2914 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 }
2916
Angus Grattond8213d02016-05-25 20:56:48 +10002917 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002918 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 ssl->out_msglen ) );
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924 ssl->out_msg, ssl->out_msglen );
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927
2928 return( 0 );
2929}
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932{
2933 int ret;
2934 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002937 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002941 if( len_pre == 0 )
2942 return( 0 );
2943
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 memcpy( msg_pre, ssl->in_msg, len_pre );
2945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 ssl->in_msglen ) );
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->in_msg, ssl->in_msglen );
2951
Paul Bakker48916f92012-09-16 19:57:18 +00002952 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2953 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2954 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002955 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002956 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959 if( ret != Z_OK )
2960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2962 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963 }
2964
Angus Grattond8213d02016-05-25 20:56:48 +10002965 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002966 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 ssl->in_msglen ) );
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972 ssl->in_msg, ssl->in_msglen );
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
2976 return( 0 );
2977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2981static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_PROTO_DTLS)
2984static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002985{
2986 /* If renegotiation is not enforced, retransmit until we would reach max
2987 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002989 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002990 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002991 unsigned char doublings = 1;
2992
2993 while( ratio != 0 )
2994 {
2995 ++doublings;
2996 ratio >>= 1;
2997 }
2998
2999 if( ++ssl->renego_records_seen > doublings )
3000 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003002 return( 0 );
3003 }
3004 }
3005
3006 return( ssl_write_hello_request( ssl ) );
3007}
3008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010
Paul Bakker5121ce52009-01-03 21:22:43 +00003011/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003012 * Fill the input message buffer by appending data to it.
3013 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003014 *
3015 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3016 * available (from this read and/or a previous one). Otherwise, an error code
3017 * is returned (possibly EOF or WANT_READ).
3018 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003019 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3020 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3021 * since we always read a whole datagram at once.
3022 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003023 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003024 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027{
Paul Bakker23986e52011-04-24 08:57:21 +00003028 int ret;
3029 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003033 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003036 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003038 }
3039
Angus Grattond8213d02016-05-25 20:56:48 +10003040 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003047 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003049 uint32_t timeout;
3050
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003051 /* Just to be sure */
3052 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3053 {
3054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3055 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3057 }
3058
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 /*
3060 * The point is, we need to always read a full datagram at once, so we
3061 * sometimes read more then requested, and handle the additional data.
3062 * It could be the rest of the current record (while fetching the
3063 * header) and/or some other records in the same datagram.
3064 */
3065
3066 /*
3067 * Move to the next record in the already read datagram if applicable
3068 */
3069 if( ssl->next_record_offset != 0 )
3070 {
3071 if( ssl->in_left < ssl->next_record_offset )
3072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003075 }
3076
3077 ssl->in_left -= ssl->next_record_offset;
3078
3079 if( ssl->in_left != 0 )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003082 ssl->next_record_offset ) );
3083 memmove( ssl->in_hdr,
3084 ssl->in_hdr + ssl->next_record_offset,
3085 ssl->in_left );
3086 }
3087
3088 ssl->next_record_offset = 0;
3089 }
3090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003093
3094 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003095 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 */
3097 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003100 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003101 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003102
3103 /*
3104 * A record can't be split accross datagrams. If we need to read but
3105 * are not at the beginning of a new record, the caller did something
3106 * wrong.
3107 */
3108 if( ssl->in_left != 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003112 }
3113
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003114 /*
3115 * Don't even try to read if time's out already.
3116 * This avoids by-passing the timer when repeatedly receiving messages
3117 * that will end up being dropped.
3118 */
3119 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003120 {
3121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003122 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003123 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003124 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003125 {
Angus Grattond8213d02016-05-25 20:56:48 +10003126 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 timeout = ssl->handshake->retransmit_timeout;
3130 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003131 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003135 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003136 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3137 timeout );
3138 else
3139 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142
3143 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003145 }
3146
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003147 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003153 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003154 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003158 }
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003163 return( ret );
3164 }
3165
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003166 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003169 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003171 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003172 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003175 return( ret );
3176 }
3177
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003178 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003181 }
3182
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 if( ret < 0 )
3184 return( ret );
3185
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003186 ssl->in_left = ret;
3187 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003188 MBEDTLS_SSL_TRANSPORT_ELSE
3189#endif /* MBEDTLS_SSL_PROTO_DTLS */
3190#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003193 ssl->in_left, nb_want ) );
3194
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003195 while( ssl->in_left < nb_want )
3196 {
3197 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003198
3199 if( ssl_check_timer( ssl ) != 0 )
3200 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3201 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003202 {
3203 if( ssl->f_recv_timeout != NULL )
3204 {
3205 ret = ssl->f_recv_timeout( ssl->p_bio,
3206 ssl->in_hdr + ssl->in_left, len,
3207 ssl->conf->read_timeout );
3208 }
3209 else
3210 {
3211 ret = ssl->f_recv( ssl->p_bio,
3212 ssl->in_hdr + ssl->in_left, len );
3213 }
3214 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003217 ssl->in_left, nb_want ) );
3218 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003219
3220 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003222
3223 if( ret < 0 )
3224 return( ret );
3225
mohammad160352aecb92018-03-28 23:41:40 -07003226 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003227 {
Darryl Green11999bb2018-03-13 15:22:58 +00003228 MBEDTLS_SSL_DEBUG_MSG( 1,
3229 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003230 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3232 }
3233
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003234 ssl->in_left += ret;
3235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003237#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 return( 0 );
3242}
3243
3244/*
3245 * Flush any data not yet written
3246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003249 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003250 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003254 if( ssl->f_send == NULL )
3255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003257 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003259 }
3260
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003261 /* Avoid incrementing counter if data is flushed */
3262 if( ssl->out_left == 0 )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003265 return( 0 );
3266 }
3267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 while( ssl->out_left > 0 )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003271 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Hanno Becker2b1e3542018-08-06 11:19:13 +01003273 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003274 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
3278 if( ret <= 0 )
3279 return( ret );
3280
mohammad160352aecb92018-03-28 23:41:40 -07003281 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003282 {
Darryl Green11999bb2018-03-13 15:22:58 +00003283 MBEDTLS_SSL_DEBUG_MSG( 1,
3284 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003285 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3287 }
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 ssl->out_left -= ret;
3290 }
3291
Hanno Becker2b1e3542018-08-06 11:19:13 +01003292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003294 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003295 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003296 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003297 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003298#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003299#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 {
3301 ssl->out_hdr = ssl->out_buf + 8;
3302 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003303#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 return( 0 );
3309}
3310
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003311/*
3312 * Functions to handle the DTLS retransmission state machine
3313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315/*
3316 * Append current handshake message to current outgoing flight
3317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3322 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3323 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324
3325 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003326 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003331 }
3332
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003333 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003334 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338 }
3339
3340 /* Copy current handshake message with headers */
3341 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3342 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 msg->next = NULL;
3345
3346 /* Append to the current flight */
3347 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349 else
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 while( cur->next != NULL )
3353 cur = cur->next;
3354 cur->next = msg;
3355 }
3356
Hanno Becker3b235902018-08-06 09:54:53 +01003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 return( 0 );
3359}
3360
3361/*
3362 * Free the current flight of handshake messages
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_ssl_flight_item *cur = flight;
3367 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
3369 while( cur != NULL )
3370 {
3371 next = cur->next;
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_free( cur->p );
3374 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375
3376 cur = next;
3377 }
3378}
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3381static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003382#endif
3383
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 * Swap transform_out and out_ctr with the alternative ones
3386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390 unsigned char tmp_out_ctr[8];
3391
3392 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395 return;
3396 }
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003399
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003400 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 tmp_transform = ssl->transform_out;
3402 ssl->transform_out = ssl->handshake->alt_transform_out;
3403 ssl->handshake->alt_transform_out = tmp_transform;
3404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003405 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003406 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3407 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003408 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003409
3410 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003411 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3414 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3419 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420 }
3421 }
3422#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003423}
3424
3425/*
3426 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003427 */
3428int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3429{
3430 int ret = 0;
3431
3432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3433
3434 ret = mbedtls_ssl_flight_transmit( ssl );
3435
3436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3437
3438 return( ret );
3439}
3440
3441/*
3442 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443 *
3444 * Need to remember the current message in case flush_output returns
3445 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003448int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003456
3457 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003458 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 ssl_swap_epochs( ssl );
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003463
3464 while( ssl->handshake->cur_msg != NULL )
3465 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003466 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003467 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468
Hanno Beckere1dcb032018-08-17 16:47:58 +01003469 int const is_finished =
3470 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3471 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3472
Hanno Becker04da1892018-08-14 13:22:10 +01003473 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3474 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3475
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003476 /* Swap epochs before sending Finished: we can't do it after
3477 * sending ChangeCipherSpec, in case write returns WANT_READ.
3478 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003479 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003480 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 ssl_swap_epochs( ssl );
3483 }
3484
Hanno Becker67bc7c32018-08-06 11:33:50 +01003485 ret = ssl_get_remaining_payload_in_datagram( ssl );
3486 if( ret < 0 )
3487 return( ret );
3488 max_frag_len = (size_t) ret;
3489
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003490 /* CCS is copied as is, while HS messages may need fragmentation */
3491 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3492 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 if( max_frag_len == 0 )
3494 {
3495 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3496 return( ret );
3497
3498 continue;
3499 }
3500
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003501 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003503 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003505 /* Update position inside current message */
3506 ssl->handshake->cur_msg_p += cur->len;
3507 }
3508 else
3509 {
3510 const unsigned char * const p = ssl->handshake->cur_msg_p;
3511 const size_t hs_len = cur->len - 12;
3512 const size_t frag_off = p - ( cur->p + 12 );
3513 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003514 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003515
Hanno Beckere1dcb032018-08-17 16:47:58 +01003516 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003517 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003518 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520
Hanno Becker67bc7c32018-08-06 11:33:50 +01003521 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3522 return( ret );
3523
3524 continue;
3525 }
3526 max_hs_frag_len = max_frag_len - 12;
3527
3528 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3529 max_hs_frag_len : rem_len;
3530
3531 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 (unsigned) cur_hs_frag_len,
3535 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003536 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003537
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003538 /* Messages are stored with handshake headers as if not fragmented,
3539 * copy beginning of headers then fill fragmentation fields.
3540 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3541 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003542
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3544 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3545 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3546
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3548 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3549 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003550
3551 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3552
Hanno Becker3f7b9732018-08-28 09:53:25 +01003553 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3555 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003556 ssl->out_msgtype = cur->type;
3557
3558 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003560 }
3561
3562 /* If done with the current message move to the next one if any */
3563 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3564 {
3565 if( cur->next != NULL )
3566 {
3567 ssl->handshake->cur_msg = cur->next;
3568 ssl->handshake->cur_msg_p = cur->next->p + 12;
3569 }
3570 else
3571 {
3572 ssl->handshake->cur_msg = NULL;
3573 ssl->handshake->cur_msg_p = NULL;
3574 }
3575 }
3576
3577 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003578 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 return( ret );
3582 }
3583 }
3584
Hanno Becker67bc7c32018-08-06 11:33:50 +01003585 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3586 return( ret );
3587
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003588 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3590 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003591 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003594 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3595 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003596
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598
3599 return( 0 );
3600}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601
3602/*
3603 * To be called when the last message of an incoming flight is received.
3604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606{
3607 /* We won't need to resend that one any more */
3608 ssl_flight_free( ssl->handshake->flight );
3609 ssl->handshake->flight = NULL;
3610 ssl->handshake->cur_msg = NULL;
3611
3612 /* The next incoming flight will start with this msg_seq */
3613 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3614
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003615 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003616 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003617
Hanno Becker0271f962018-08-16 13:23:47 +01003618 /* Clear future message buffering structure. */
3619 ssl_buffering_free( ssl );
3620
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003621 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003622 ssl_set_timer( ssl, 0 );
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3625 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003628 }
3629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632
3633/*
3634 * To be called when the last message of an outgoing flight is send.
3635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003637{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003638 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003639 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3642 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645 }
3646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650
Paul Bakker5121ce52009-01-03 21:22:43 +00003651/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003654
3655/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003656 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 *
3658 * - fill in handshake headers
3659 * - update handshake checksum
3660 * - DTLS: save message for resending
3661 * - then pass to the record layer
3662 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003663 * DTLS: except for HelloRequest, messages are only queued, and will only be
3664 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003665 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666 * Inputs:
3667 * - ssl->out_msglen: 4 + actual handshake message len
3668 * (4 is the size of handshake headers for TLS)
3669 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3670 * - ssl->out_msg + 4: the handshake message body
3671 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003672 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 * - ssl->out_msglen: the length of the record contents
3674 * (including handshake headers but excluding record headers)
3675 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003676 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003677int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003679 int ret;
3680 const size_t hs_len = ssl->out_msglen - 4;
3681 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3684
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003685 /*
3686 * Sanity checks
3687 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003688 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3690 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003691 /* In SSLv3, the client might send a NoCertificate alert. */
3692#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3693 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3694 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3695 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3696#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3697 {
3698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3700 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003703 /* Whenever we send anything different from a
3704 * HelloRequest we should be in a handshake - double check. */
3705 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3706 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003707 ssl->handshake == NULL )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003722
Hanno Beckerb50a2532018-08-06 11:52:54 +01003723 /* Double-check that we did not exceed the bounds
3724 * of the outgoing record buffer.
3725 * This should never fail as the various message
3726 * writing functions must obey the bounds of the
3727 * outgoing record buffer, but better be safe.
3728 *
3729 * Note: We deliberately do not check for the MTU or MFL here.
3730 */
3731 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3732 {
3733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3734 "size %u, maximum %u",
3735 (unsigned) ssl->out_msglen,
3736 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3738 }
3739
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003740 /*
3741 * Fill handshake headers
3742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3746 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3747 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003749 /*
3750 * DTLS has additional fields in the Handshake layer,
3751 * between the length field and the actual payload:
3752 * uint16 message_seq;
3753 * uint24 fragment_offset;
3754 * uint24 fragment_length;
3755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003757 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003758 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003760 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003761 {
3762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3763 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003764 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003765 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3767 }
3768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003771
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003772 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003773 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003774 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003775 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3776 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3777 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003778 }
3779 else
3780 {
3781 ssl->out_msg[4] = 0;
3782 ssl->out_msg[5] = 0;
3783 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003784
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003785 /* Handshake hashes are computed without fragmentation,
3786 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003787 memset( ssl->out_msg + 6, 0x00, 3 );
3788 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003791
Hanno Becker0207e532018-08-28 10:28:28 +01003792 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3794 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 }
3796
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003799 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003800 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3801 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802 {
3803 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806 return( ret );
3807 }
3808 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003809 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003811 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003813 {
3814 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3815 return( ret );
3816 }
3817 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003818
3819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3820
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003822}
3823
3824/*
3825 * Record layer functions
3826 */
3827
3828/*
3829 * Write current record.
3830 *
3831 * Uses:
3832 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3833 * - ssl->out_msglen: length of the record content (excl headers)
3834 * - ssl->out_msg: record content
3835 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003836int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003837{
3838 int ret, done = 0;
3839 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003841
3842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003845 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003847 {
3848 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003851 return( ret );
3852 }
3853
3854 len = ssl->out_msglen;
3855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3859 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 ret = mbedtls_ssl_hw_record_write( ssl );
3864 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3867 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003868 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003869
3870 if( ret == 0 )
3871 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 if( !done )
3875 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 unsigned i;
3877 size_t protected_record_size;
3878
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003879 /* Skip writing the record content type to after the encryption,
3880 * as it may change when using the CID extension. */
3881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003883 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003884
Hanno Becker19859472018-08-06 09:40:20 +01003885 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003886 ssl->out_len[0] = (unsigned char)( len >> 8 );
3887 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003888
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 {
Hanno Becker3307b532017-12-27 21:37:21 +00003891 mbedtls_record rec;
3892
3893 rec.buf = ssl->out_iv;
3894 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3895 ( ssl->out_iv - ssl->out_buf );
3896 rec.data_len = ssl->out_msglen;
3897 rec.data_offset = ssl->out_msg - rec.buf;
3898
3899 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3900 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3901 ssl->conf->transport, rec.ver );
3902 rec.type = ssl->out_msgtype;
3903
Hanno Beckera5a2b082019-05-15 14:03:01 +01003904#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003905 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003906 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003907#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003908
Hanno Becker611a83b2018-01-03 14:27:32 +00003909 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003910 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003913 return( ret );
3914 }
3915
Hanno Becker3307b532017-12-27 21:37:21 +00003916 if( rec.data_offset != 0 )
3917 {
3918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3920 }
3921
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003922 /* Update the record content type and CID. */
3923 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003924#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003925 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003926#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003927 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003928 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3929 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003930 }
3931
Hanno Becker43395762019-05-03 14:46:38 +01003932 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003933
3934#if defined(MBEDTLS_SSL_PROTO_DTLS)
3935 /* In case of DTLS, double-check that we don't exceed
3936 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003937 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003939 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003940 if( ret < 0 )
3941 return( ret );
3942
3943 if( protected_record_size > (size_t) ret )
3944 {
3945 /* Should never happen */
3946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3947 }
3948 }
3949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003950
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003951 /* Now write the potentially updated record content type. */
3952 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003955 "version = [%d:%d], msglen = %d",
3956 ssl->out_hdr[0], ssl->out_hdr[1],
3957 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003960 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003961
3962 ssl->out_left += protected_record_size;
3963 ssl->out_hdr += protected_record_size;
3964 ssl_update_out_pointers( ssl, ssl->transform_out );
3965
Hanno Becker04484622018-08-06 09:49:38 +01003966 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3967 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3968 break;
3969
3970 /* The loop goes to its end iff the counter is wrapping */
3971 if( i == ssl_ep_len( ssl ) )
3972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3974 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003976 }
3977
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003979 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003980 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003982 size_t remaining;
3983 ret = ssl_get_remaining_payload_in_datagram( ssl );
3984 if( ret < 0 )
3985 {
3986 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3987 ret );
3988 return( ret );
3989 }
3990
3991 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003993 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003994 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003995 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003996 else
3997 {
Hanno Becker513815a2018-08-20 11:56:09 +01003998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 }
4000 }
4001#endif /* MBEDTLS_SSL_PROTO_DTLS */
4002
4003 if( ( flush == SSL_FORCE_FLUSH ) &&
4004 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 return( ret );
4008 }
4009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
4012 return( 0 );
4013}
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004016
4017static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4018{
4019 if( ssl->in_msglen < ssl->in_hslen ||
4020 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4021 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4022 {
4023 return( 1 );
4024 }
4025 return( 0 );
4026}
Hanno Becker44650b72018-08-16 12:51:11 +01004027
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004028static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004029{
4030 return( ( ssl->in_msg[9] << 16 ) |
4031 ( ssl->in_msg[10] << 8 ) |
4032 ssl->in_msg[11] );
4033}
4034
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004035static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004036{
4037 return( ( ssl->in_msg[6] << 16 ) |
4038 ( ssl->in_msg[7] << 8 ) |
4039 ssl->in_msg[8] );
4040}
4041
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004042static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004043{
4044 uint32_t msg_len, frag_off, frag_len;
4045
4046 msg_len = ssl_get_hs_total_len( ssl );
4047 frag_off = ssl_get_hs_frag_off( ssl );
4048 frag_len = ssl_get_hs_frag_len( ssl );
4049
4050 if( frag_off > msg_len )
4051 return( -1 );
4052
4053 if( frag_len > msg_len - frag_off )
4054 return( -1 );
4055
4056 if( frag_len + 12 > ssl->in_msglen )
4057 return( -1 );
4058
4059 return( 0 );
4060}
4061
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004062/*
4063 * Mark bits in bitmask (used for DTLS HS reassembly)
4064 */
4065static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4066{
4067 unsigned int start_bits, end_bits;
4068
4069 start_bits = 8 - ( offset % 8 );
4070 if( start_bits != 8 )
4071 {
4072 size_t first_byte_idx = offset / 8;
4073
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004074 /* Special case */
4075 if( len <= start_bits )
4076 {
4077 for( ; len != 0; len-- )
4078 mask[first_byte_idx] |= 1 << ( start_bits - len );
4079
4080 /* Avoid potential issues with offset or len becoming invalid */
4081 return;
4082 }
4083
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004084 offset += start_bits; /* Now offset % 8 == 0 */
4085 len -= start_bits;
4086
4087 for( ; start_bits != 0; start_bits-- )
4088 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4089 }
4090
4091 end_bits = len % 8;
4092 if( end_bits != 0 )
4093 {
4094 size_t last_byte_idx = ( offset + len ) / 8;
4095
4096 len -= end_bits; /* Now len % 8 == 0 */
4097
4098 for( ; end_bits != 0; end_bits-- )
4099 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4100 }
4101
4102 memset( mask + offset / 8, 0xFF, len / 8 );
4103}
4104
4105/*
4106 * Check that bitmask is full
4107 */
4108static int ssl_bitmask_check( unsigned char *mask, size_t len )
4109{
4110 size_t i;
4111
4112 for( i = 0; i < len / 8; i++ )
4113 if( mask[i] != 0xFF )
4114 return( -1 );
4115
4116 for( i = 0; i < len % 8; i++ )
4117 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4118 return( -1 );
4119
4120 return( 0 );
4121}
4122
Hanno Becker56e205e2018-08-16 09:06:12 +01004123/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004124static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004125 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004126{
Hanno Becker56e205e2018-08-16 09:06:12 +01004127 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004128
Hanno Becker56e205e2018-08-16 09:06:12 +01004129 alloc_len = 12; /* Handshake header */
4130 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004131
Hanno Beckerd07df862018-08-16 09:14:58 +01004132 if( add_bitmap )
4133 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004134
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004135 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136}
Hanno Becker56e205e2018-08-16 09:06:12 +01004137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004140static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004141{
4142 return( ( ssl->in_msg[1] << 16 ) |
4143 ( ssl->in_msg[2] << 8 ) |
4144 ssl->in_msg[3] );
4145}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004146
Simon Butcher99000142016-10-13 17:21:01 +01004147int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004152 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004154 }
4155
Hanno Becker12555c62018-08-16 12:47:53 +01004156 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004159 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004160 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004163 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004164 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004165 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004166 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004167
Hanno Becker44650b72018-08-16 12:51:11 +01004168 if( ssl_check_hs_header( ssl ) != 0 )
4169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4172 }
4173
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004174 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004175 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4176 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4177 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4178 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004179 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004180 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4183 recv_msg_seq,
4184 ssl->handshake->in_msg_seq ) );
4185 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4186 }
4187
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004188 /* Retransmit only on last message from previous flight, to avoid
4189 * too many retransmissions.
4190 * Besides, No sane server ever retransmits HelloVerifyRequest */
4191 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004195 "message_seq = %d, start_of_flight = %d",
4196 recv_msg_seq,
4197 ssl->handshake->in_flight_start_seq ) );
4198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004202 return( ret );
4203 }
4204 }
4205 else
4206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 "message_seq = %d, expected = %d",
4209 recv_msg_seq,
4210 ssl->handshake->in_msg_seq ) );
4211 }
4212
Hanno Becker90333da2017-10-10 11:27:13 +01004213 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004214 }
4215 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004216
Hanno Becker6d97ef52018-08-16 13:09:04 +01004217 /* Message reassembly is handled alongside buffering of future
4218 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004219 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004220 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004221 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004224 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004225 }
4226 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004227 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004229#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004231 /* With TLS we don't handle fragmentation (for now) */
4232 if( ssl->in_msglen < ssl->in_hslen )
4233 {
4234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4235 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4236 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004237 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004238#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004239
Simon Butcher99000142016-10-13 17:21:01 +01004240 return( 0 );
4241}
4242
4243void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4244{
Hanno Becker0271f962018-08-16 13:23:47 +01004245 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004246
Hanno Becker0271f962018-08-16 13:23:47 +01004247 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004248 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004249 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004250 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004251
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004252 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004254 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004255 ssl->handshake != NULL )
4256 {
Hanno Becker0271f962018-08-16 13:23:47 +01004257 unsigned offset;
4258 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004259
Hanno Becker0271f962018-08-16 13:23:47 +01004260 /* Increment handshake sequence number */
4261 hs->in_msg_seq++;
4262
4263 /*
4264 * Clear up handshake buffering and reassembly structure.
4265 */
4266
4267 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004268 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004269
4270 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004271 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4272 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004273 offset++, hs_buf++ )
4274 {
4275 *hs_buf = *(hs_buf + 1);
4276 }
4277
4278 /* Create a fresh last entry */
4279 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004280 }
4281#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004282}
4283
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004285 * DTLS anti-replay: RFC 6347 4.1.2.6
4286 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004287 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4288 * Bit n is set iff record number in_window_top - n has been seen.
4289 *
4290 * Usually, in_window_top is the last record number seen and the lsb of
4291 * in_window is set. The only exception is the initial state (record number 0
4292 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4295static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004296{
4297 ssl->in_window_top = 0;
4298 ssl->in_window = 0;
4299}
4300
4301static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4302{
4303 return( ( (uint64_t) buf[0] << 40 ) |
4304 ( (uint64_t) buf[1] << 32 ) |
4305 ( (uint64_t) buf[2] << 24 ) |
4306 ( (uint64_t) buf[3] << 16 ) |
4307 ( (uint64_t) buf[4] << 8 ) |
4308 ( (uint64_t) buf[5] ) );
4309}
4310
4311/*
4312 * Return 0 if sequence number is acceptable, -1 otherwise
4313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004315{
4316 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4317 uint64_t bit;
4318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004319 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004320 return( 0 );
4321
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004322 if( rec_seqnum > ssl->in_window_top )
4323 return( 0 );
4324
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326
4327 if( bit >= 64 )
4328 return( -1 );
4329
4330 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4331 return( -1 );
4332
4333 return( 0 );
4334}
4335
4336/*
4337 * Update replay window on new validated record
4338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004340{
4341 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4342
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004343 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004344 return;
4345
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346 if( rec_seqnum > ssl->in_window_top )
4347 {
4348 /* Update window_top and the contents of the window */
4349 uint64_t shift = rec_seqnum - ssl->in_window_top;
4350
4351 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004352 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004353 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004354 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004356 ssl->in_window |= 1;
4357 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358
4359 ssl->in_window_top = rec_seqnum;
4360 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 else
4362 {
4363 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365
4366 if( bit < 64 ) /* Always true, but be extra sure */
4367 ssl->in_window |= (uint64_t) 1 << bit;
4368 }
4369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004372#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004374static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4375
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 * Without any SSL context, check if a datagram looks like a ClientHello with
4378 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004379 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 *
4381 * - if cookie is valid, return 0
4382 * - if ClientHello looks superficially valid but cookie is not,
4383 * fill obuf and set olen, then
4384 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4385 * - otherwise return a specific error code
4386 */
4387static int ssl_check_dtls_clihlo_cookie(
4388 mbedtls_ssl_cookie_write_t *f_cookie_write,
4389 mbedtls_ssl_cookie_check_t *f_cookie_check,
4390 void *p_cookie,
4391 const unsigned char *cli_id, size_t cli_id_len,
4392 const unsigned char *in, size_t in_len,
4393 unsigned char *obuf, size_t buf_len, size_t *olen )
4394{
4395 size_t sid_len, cookie_len;
4396 unsigned char *p;
4397
4398 if( f_cookie_write == NULL || f_cookie_check == NULL )
4399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4400
4401 /*
4402 * Structure of ClientHello with record and handshake headers,
4403 * and expected values. We don't need to check a lot, more checks will be
4404 * done when actually parsing the ClientHello - skipping those checks
4405 * avoids code duplication and does not make cookie forging any easier.
4406 *
4407 * 0-0 ContentType type; copied, must be handshake
4408 * 1-2 ProtocolVersion version; copied
4409 * 3-4 uint16 epoch; copied, must be 0
4410 * 5-10 uint48 sequence_number; copied
4411 * 11-12 uint16 length; (ignored)
4412 *
4413 * 13-13 HandshakeType msg_type; (ignored)
4414 * 14-16 uint24 length; (ignored)
4415 * 17-18 uint16 message_seq; copied
4416 * 19-21 uint24 fragment_offset; copied, must be 0
4417 * 22-24 uint24 fragment_length; (ignored)
4418 *
4419 * 25-26 ProtocolVersion client_version; (ignored)
4420 * 27-58 Random random; (ignored)
4421 * 59-xx SessionID session_id; 1 byte len + sid_len content
4422 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4423 * ...
4424 *
4425 * Minimum length is 61 bytes.
4426 */
4427 if( in_len < 61 ||
4428 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4429 in[3] != 0 || in[4] != 0 ||
4430 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4431 {
4432 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4433 }
4434
4435 sid_len = in[59];
4436 if( sid_len > in_len - 61 )
4437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4438
4439 cookie_len = in[60 + sid_len];
4440 if( cookie_len > in_len - 60 )
4441 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4442
4443 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4444 cli_id, cli_id_len ) == 0 )
4445 {
4446 /* Valid cookie */
4447 return( 0 );
4448 }
4449
4450 /*
4451 * If we get here, we've got an invalid cookie, let's prepare HVR.
4452 *
4453 * 0-0 ContentType type; copied
4454 * 1-2 ProtocolVersion version; copied
4455 * 3-4 uint16 epoch; copied
4456 * 5-10 uint48 sequence_number; copied
4457 * 11-12 uint16 length; olen - 13
4458 *
4459 * 13-13 HandshakeType msg_type; hello_verify_request
4460 * 14-16 uint24 length; olen - 25
4461 * 17-18 uint16 message_seq; copied
4462 * 19-21 uint24 fragment_offset; copied
4463 * 22-24 uint24 fragment_length; olen - 25
4464 *
4465 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4466 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4467 *
4468 * Minimum length is 28.
4469 */
4470 if( buf_len < 28 )
4471 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4472
4473 /* Copy most fields and adapt others */
4474 memcpy( obuf, in, 25 );
4475 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4476 obuf[25] = 0xfe;
4477 obuf[26] = 0xff;
4478
4479 /* Generate and write actual cookie */
4480 p = obuf + 28;
4481 if( f_cookie_write( p_cookie,
4482 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4483 {
4484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4485 }
4486
4487 *olen = p - obuf;
4488
4489 /* Go back and fill length fields */
4490 obuf[27] = (unsigned char)( *olen - 28 );
4491
4492 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4493 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4494 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4495
4496 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4497 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4498
4499 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4500}
4501
4502/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004503 * Handle possible client reconnect with the same UDP quadruplet
4504 * (RFC 6347 Section 4.2.8).
4505 *
4506 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4507 * that looks like a ClientHello.
4508 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 * - if the input looks like a ClientHello without cookies,
4510 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004511 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004512 * - if the input looks like a ClientHello with a valid cookie,
4513 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004514 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004515 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004516 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004518 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4519 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 */
4521static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4522{
4523 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004524 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004526 ret = ssl_check_dtls_clihlo_cookie(
4527 ssl->conf->f_cookie_write,
4528 ssl->conf->f_cookie_check,
4529 ssl->conf->p_cookie,
4530 ssl->cli_id, ssl->cli_id_len,
4531 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004532 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004534 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4535
4536 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004538 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004539 * If the error is permanent we'll catch it later,
4540 * if it's not, then hopefully it'll work next time. */
4541 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4542
4543 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004544 }
4545
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004546 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004547 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004548 /* Got a valid cookie, partially reset context */
4549 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4550 {
4551 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4552 return( ret );
4553 }
4554
4555 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004556 }
4557
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004558 return( ret );
4559}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004560#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561
Hanno Becker46483f12019-05-03 13:25:54 +01004562static int ssl_check_record_type( uint8_t record_type )
4563{
4564 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4565 record_type != MBEDTLS_SSL_MSG_ALERT &&
4566 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4567 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4568 {
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4570 }
4571
4572 return( 0 );
4573}
4574
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 * ContentType type;
4577 * ProtocolVersion version;
4578 * uint16 epoch; // DTLS only
4579 * uint48 sequence_number; // DTLS only
4580 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004581 *
4582 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004583 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004584 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4585 *
4586 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004587 * 1. proceed with the record if this function returns 0
4588 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4589 * 3. return CLIENT_RECONNECT if this function return that value
4590 * 4. drop the whole datagram if this function returns anything else.
4591 * Point 2 is needed when the peer is resending, and we have already received
4592 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004595{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004596 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004597 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Hanno Becker8b09b732019-05-08 12:03:28 +01004599 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004600
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004602 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004604 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004606 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004607 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4608 ssl->conf->cid_len != 0 )
4609 {
4610 /* Shift pointers to account for record header including CID
4611 * struct {
4612 * ContentType special_type = tls12_cid;
4613 * ProtocolVersion version;
4614 * uint16 epoch;
4615 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004616 * opaque cid[cid_length]; // Additional field compared to
4617 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004618 * uint16 length;
4619 * opaque enc_content[DTLSCiphertext.length];
4620 * } DTLSCiphertext;
4621 */
4622
4623 /* So far, we only support static CID lengths
4624 * fixed in the configuration. */
4625 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4626 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4627 }
4628 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004629#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004630 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004633
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004634#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004635 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004636 * Section 4.1.2.7, that is, send alert only with TLS */
4637 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4638 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004639 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4640 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004641 }
4642#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004645 }
4646
4647 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004648 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 }
4653
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004654 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
4659
Hanno Becker8b09b732019-05-08 12:03:28 +01004660 /* Now that the total length of the record header is known, ensure
4661 * that the current datagram is large enough to hold it.
4662 * This would fail, for example, if we received a datagram of
4663 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4664 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4665 if( ret != 0 )
4666 {
4667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4668 return( ret );
4669 }
4670 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4671
4672 /* Parse and validate record length
4673 * This must happen after the CID parsing because
4674 * its position in the record header depends on
4675 * the presence of a CID. */
4676
4677 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004678 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004679 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004683 }
4684
Hanno Becker8b09b732019-05-08 12:03:28 +01004685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004686 "version = [%d:%d], msglen = %d",
4687 ssl->in_msgtype,
4688 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004691 * DTLS-related tests.
4692 * Check epoch before checking length constraint because
4693 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4694 * message gets duplicated before the corresponding Finished message,
4695 * the second ChangeCipherSpec should be discarded because it belongs
4696 * to an old epoch, but not because its length is shorter than
4697 * the minimum record length for packets using the new record transform.
4698 * Note that these two kinds of failures are handled differently,
4699 * as an unexpected record is silently skipped but an invalid
4700 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004701 */
4702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004703 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004704 {
4705 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4706
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004707 /* Check epoch (and sequence number) with DTLS */
4708 if( rec_epoch != ssl->in_epoch )
4709 {
4710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4711 "expected %d, received %d",
4712 ssl->in_epoch, rec_epoch ) );
4713
4714#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4715 /*
4716 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4717 * access the first byte of record content (handshake type), as we
4718 * have an active transform (possibly iv_len != 0), so use the
4719 * fact that the record header len is 13 instead.
4720 */
4721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4722 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4723 rec_epoch == 0 &&
4724 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4725 ssl->in_left > 13 &&
4726 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4729 "from the same port" ) );
4730 return( ssl_handle_possible_reconnect( ssl ) );
4731 }
4732 else
4733#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004734 {
4735 /* Consider buffering the record. */
4736 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4737 {
4738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4739 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4740 }
4741
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004742 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004743 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004744 }
4745
4746#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4747 /* Replay detection only works for the current epoch */
4748 if( rec_epoch == ssl->in_epoch &&
4749 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4750 {
4751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4752 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4753 }
4754#endif
4755 }
4756#endif /* MBEDTLS_SSL_PROTO_DTLS */
4757
Hanno Becker52c6dc62017-05-26 16:07:36 +01004758
4759 /* Check length against bounds of the current transform and version */
4760 if( ssl->transform_in == NULL )
4761 {
4762 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768 }
4769 else
4770 {
4771 if( ssl->in_msglen < ssl->transform_in->minlen )
4772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4775 }
4776
4777#if defined(MBEDTLS_SSL_PROTO_SSL3)
4778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004779 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4783 }
4784#endif
4785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4786 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4787 /*
4788 * TLS encrypted messages can have up to 256 bytes of padding
4789 */
4790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4791 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004792 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4795 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4796 }
4797#endif
4798 }
4799
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004800 return( 0 );
4801}
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803/*
4804 * If applicable, decrypt (and decompress) record content
4805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004807{
4808 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004811 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4814 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 ret = mbedtls_ssl_hw_record_read( ssl );
4819 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004824
4825 if( ret == 0 )
4826 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004829 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004831 mbedtls_record rec;
4832
4833 rec.buf = ssl->in_iv;
4834 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4835 - ( ssl->in_iv - ssl->in_buf );
4836 rec.data_len = ssl->in_msglen;
4837 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004838#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004839 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004840 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004841#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004842
4843 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4844 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4845 ssl->conf->transport, rec.ver );
4846 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004847 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4848 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004851
Hanno Beckera5a2b082019-05-15 14:03:01 +01004852#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004853 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4854 ssl->conf->ignore_unexpected_cid
4855 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4856 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004858 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004860#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004861
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 return( ret );
4863 }
4864
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004865 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004866 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004867 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4868 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004871 /* The record content type may change during decryption,
4872 * so re-read it. */
4873 ssl->in_msgtype = rec.type;
4874 /* Also update the input buffer, because unfortunately
4875 * the server-side ssl_parse_client_hello() reparses the
4876 * record header when receiving a ClientHello initiating
4877 * a renegotiation. */
4878 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004879 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004880 ssl->in_msglen = rec.data_len;
4881 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4882 ssl->in_len[1] = (unsigned char)( rec.data_len );
4883
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4885 ssl->in_msg, ssl->in_msglen );
4886
Hanno Beckera5a2b082019-05-15 14:03:01 +01004887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004888 /* We have already checked the record content type
4889 * in ssl_parse_record_header(), failing or silently
4890 * dropping the record in the case of an unknown type.
4891 *
4892 * Since with the use of CIDs, the record content type
4893 * might change during decryption, re-check the record
4894 * content type, but treat a failure as fatal this time. */
4895 if( ssl_check_record_type( ssl->in_msgtype ) )
4896 {
4897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4899 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004900#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004901
Angus Grattond8213d02016-05-25 20:56:48 +10004902 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004907 else if( ssl->in_msglen == 0 )
4908 {
4909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4910 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4911 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4912 {
4913 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4915 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4916 }
4917#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4918
4919 ssl->nb_zero++;
4920
4921 /*
4922 * Three or more empty messages may be a DoS attack
4923 * (excessive CPU consumption).
4924 */
4925 if( ssl->nb_zero > 3 )
4926 {
4927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004928 "messages, possible DoS attack" ) );
4929 /* Treat the records as if they were not properly authenticated,
4930 * thereby failing the connection if we see more than allowed
4931 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004932 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4933 }
4934 }
4935 else
4936 ssl->nb_zero = 0;
4937
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4939#if defined(MBEDTLS_SSL_PROTO_TLS)
4940 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004941 {
4942 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004944 if( ++ssl->in_ctr[i - 1] != 0 )
4945 break;
4946
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004947 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4951 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4952 }
4953 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004954#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004955
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 }
4957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004959 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004961 {
4962 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004965 return( ret );
4966 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004974 }
4975#endif
4976
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004977 return( 0 );
4978}
4979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004981
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004982/*
4983 * Read a record.
4984 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004985 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4986 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4987 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004988 */
Hanno Becker1097b342018-08-15 14:09:41 +01004989
4990/* Helper functions for mbedtls_ssl_read_record(). */
4991static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004992static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4993static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004994
Hanno Becker327c93b2018-08-15 13:56:18 +01004995int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004996 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004997{
4998 int ret;
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005002 if( ssl->keep_current_message == 0 )
5003 {
5004 do {
Simon Butcher99000142016-10-13 17:21:01 +01005005
Hanno Becker26994592018-08-15 14:14:59 +01005006 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005007 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005008 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005009
Hanno Beckere74d5562018-08-15 14:26:08 +01005010 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005011 {
Hanno Becker40f50842018-08-15 14:48:01 +01005012#if defined(MBEDTLS_SSL_PROTO_DTLS)
5013 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005014
Hanno Becker40f50842018-08-15 14:48:01 +01005015 /* We only check for buffered messages if the
5016 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005017 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005018 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005019 {
Hanno Becker40f50842018-08-15 14:48:01 +01005020 if( ssl_load_buffered_message( ssl ) == 0 )
5021 have_buffered = 1;
5022 }
5023
5024 if( have_buffered == 0 )
5025#endif /* MBEDTLS_SSL_PROTO_DTLS */
5026 {
5027 ret = ssl_get_next_record( ssl );
5028 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5029 continue;
5030
5031 if( ret != 0 )
5032 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005033 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005034 return( ret );
5035 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005036 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005037 }
5038
5039 ret = mbedtls_ssl_handle_message_type( ssl );
5040
Hanno Becker40f50842018-08-15 14:48:01 +01005041#if defined(MBEDTLS_SSL_PROTO_DTLS)
5042 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5043 {
5044 /* Buffer future message */
5045 ret = ssl_buffer_message( ssl );
5046 if( ret != 0 )
5047 return( ret );
5048
5049 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5050 }
5051#endif /* MBEDTLS_SSL_PROTO_DTLS */
5052
Hanno Becker90333da2017-10-10 11:27:13 +01005053 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5054 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055
5056 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005057 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005058 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005059 return( ret );
5060 }
5061
Hanno Becker327c93b2018-08-15 13:56:18 +01005062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005063 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005064 {
5065 mbedtls_ssl_update_handshake_status( ssl );
5066 }
Simon Butcher99000142016-10-13 17:21:01 +01005067 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005068 else
Simon Butcher99000142016-10-13 17:21:01 +01005069 {
Hanno Becker02f59072018-08-15 14:00:24 +01005070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005071 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005072 }
5073
5074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5075
5076 return( 0 );
5077}
5078
Hanno Becker40f50842018-08-15 14:48:01 +01005079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005080static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005081{
Hanno Becker40f50842018-08-15 14:48:01 +01005082 if( ssl->in_left > ssl->next_record_offset )
5083 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005084
Hanno Becker40f50842018-08-15 14:48:01 +01005085 return( 0 );
5086}
5087
5088static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5089{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005090 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005091 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005092 int ret = 0;
5093
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005094 if( hs == NULL )
5095 return( -1 );
5096
Hanno Beckere00ae372018-08-20 09:39:42 +01005097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5098
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5100 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5101 {
5102 /* Check if we have seen a ChangeCipherSpec before.
5103 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005104 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 {
5106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5107 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005108 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005109 }
5110
Hanno Becker39b8bc92018-08-28 17:17:13 +01005111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005112 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5113 ssl->in_msglen = 1;
5114 ssl->in_msg[0] = 1;
5115
5116 /* As long as they are equal, the exact value doesn't matter. */
5117 ssl->in_left = 0;
5118 ssl->next_record_offset = 0;
5119
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005120 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 goto exit;
5122 }
Hanno Becker37f95322018-08-16 13:55:32 +01005123
Hanno Beckerb8f50142018-08-28 10:01:34 +01005124#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005125 /* Debug only */
5126 {
5127 unsigned offset;
5128 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5129 {
5130 hs_buf = &hs->buffering.hs[offset];
5131 if( hs_buf->is_valid == 1 )
5132 {
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5134 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005135 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005136 }
5137 }
5138 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005139#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005140
5141 /* Check if we have buffered and/or fully reassembled the
5142 * next handshake message. */
5143 hs_buf = &hs->buffering.hs[0];
5144 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5145 {
5146 /* Synthesize a record containing the buffered HS message. */
5147 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5148 ( hs_buf->data[2] << 8 ) |
5149 hs_buf->data[3];
5150
5151 /* Double-check that we haven't accidentally buffered
5152 * a message that doesn't fit into the input buffer. */
5153 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5157 }
5158
5159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5160 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5161 hs_buf->data, msg_len + 12 );
5162
5163 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5164 ssl->in_hslen = msg_len + 12;
5165 ssl->in_msglen = msg_len + 12;
5166 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5167
5168 ret = 0;
5169 goto exit;
5170 }
5171 else
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5174 hs->in_msg_seq ) );
5175 }
5176
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005177 ret = -1;
5178
5179exit:
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5182 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005183}
5184
Hanno Beckera02b0b42018-08-21 17:20:27 +01005185static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5186 size_t desired )
5187{
5188 int offset;
5189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5191 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005192
Hanno Becker01315ea2018-08-21 17:22:17 +01005193 /* Get rid of future records epoch first, if such exist. */
5194 ssl_free_buffered_record( ssl );
5195
5196 /* Check if we have enough space available now. */
5197 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5198 hs->buffering.total_bytes_buffered ) )
5199 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005201 return( 0 );
5202 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005203
Hanno Becker4f432ad2018-08-28 10:02:32 +01005204 /* We don't have enough space to buffer the next expected handshake
5205 * message. Remove buffers used for future messages to gain space,
5206 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005207 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5208 offset >= 0; offset-- )
5209 {
5210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5211 offset ) );
5212
Hanno Beckerb309b922018-08-23 13:18:05 +01005213 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005214
5215 /* Check if we have enough space available now. */
5216 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5217 hs->buffering.total_bytes_buffered ) )
5218 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005220 return( 0 );
5221 }
5222 }
5223
5224 return( -1 );
5225}
5226
Hanno Becker40f50842018-08-15 14:48:01 +01005227static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5228{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005229 int ret = 0;
5230 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5231
5232 if( hs == NULL )
5233 return( 0 );
5234
5235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5236
5237 switch( ssl->in_msgtype )
5238 {
5239 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005241
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005242 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243 break;
5244
5245 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005246 {
5247 unsigned recv_msg_seq_offset;
5248 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5249 mbedtls_ssl_hs_buffer *hs_buf;
5250 size_t msg_len = ssl->in_hslen - 12;
5251
5252 /* We should never receive an old handshake
5253 * message - double-check nonetheless. */
5254 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5258 }
5259
5260 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5261 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5262 {
5263 /* Silently ignore -- message too far in the future */
5264 MBEDTLS_SSL_DEBUG_MSG( 2,
5265 ( "Ignore future HS message with sequence number %u, "
5266 "buffering window %u - %u",
5267 recv_msg_seq, ssl->handshake->in_msg_seq,
5268 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5269
5270 goto exit;
5271 }
5272
5273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5274 recv_msg_seq, recv_msg_seq_offset ) );
5275
5276 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5277
5278 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005279 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005280 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005281 size_t reassembly_buf_sz;
5282
Hanno Becker37f95322018-08-16 13:55:32 +01005283 hs_buf->is_fragmented =
5284 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5285
5286 /* We copy the message back into the input buffer
5287 * after reassembly, so check that it's not too large.
5288 * This is an implementation-specific limitation
5289 * and not one from the standard, hence it is not
5290 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005291 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005292 {
5293 /* Ignore message */
5294 goto exit;
5295 }
5296
Hanno Beckere0b150f2018-08-21 15:51:03 +01005297 /* Check if we have enough space to buffer the message. */
5298 if( hs->buffering.total_bytes_buffered >
5299 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5303 }
5304
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005305 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5306 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005307
5308 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5309 hs->buffering.total_bytes_buffered ) )
5310 {
5311 if( recv_msg_seq_offset > 0 )
5312 {
5313 /* If we can't buffer a future message because
5314 * of space limitations -- ignore. */
5315 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",
5316 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5317 (unsigned) hs->buffering.total_bytes_buffered ) );
5318 goto exit;
5319 }
Hanno Beckere1801392018-08-21 16:51:05 +01005320 else
5321 {
5322 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",
5323 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5324 (unsigned) hs->buffering.total_bytes_buffered ) );
5325 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005326
Hanno Beckera02b0b42018-08-21 17:20:27 +01005327 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005328 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005329 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",
5330 (unsigned) msg_len,
5331 (unsigned) reassembly_buf_sz,
5332 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005333 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005334 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5335 goto exit;
5336 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005337 }
5338
5339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5340 msg_len ) );
5341
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005342 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5343 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005344 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005346 goto exit;
5347 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005348 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005349
5350 /* Prepare final header: copy msg_type, length and message_seq,
5351 * then add standardised fragment_offset and fragment_length */
5352 memcpy( hs_buf->data, ssl->in_msg, 6 );
5353 memset( hs_buf->data + 6, 0, 3 );
5354 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5355
5356 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005357
5358 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005359 }
5360 else
5361 {
5362 /* Make sure msg_type and length are consistent */
5363 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5364 {
5365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5366 /* Ignore */
5367 goto exit;
5368 }
5369 }
5370
Hanno Becker4422bbb2018-08-20 09:40:19 +01005371 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005372 {
5373 size_t frag_len, frag_off;
5374 unsigned char * const msg = hs_buf->data + 12;
5375
5376 /*
5377 * Check and copy current fragment
5378 */
5379
5380 /* Validation of header fields already done in
5381 * mbedtls_ssl_prepare_handshake_record(). */
5382 frag_off = ssl_get_hs_frag_off( ssl );
5383 frag_len = ssl_get_hs_frag_len( ssl );
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5386 frag_off, frag_len ) );
5387 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5388
5389 if( hs_buf->is_fragmented )
5390 {
5391 unsigned char * const bitmask = msg + msg_len;
5392 ssl_bitmask_set( bitmask, frag_off, frag_len );
5393 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5394 msg_len ) == 0 );
5395 }
5396 else
5397 {
5398 hs_buf->is_complete = 1;
5399 }
5400
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5402 hs_buf->is_complete ? "" : "not yet " ) );
5403 }
5404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005406 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005407
5408 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005409 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005410 break;
5411 }
5412
5413exit:
5414
5415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5416 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005417}
5418#endif /* MBEDTLS_SSL_PROTO_DTLS */
5419
Hanno Becker1097b342018-08-15 14:09:41 +01005420static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005421{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 * Consume last content-layer message and potentially
5424 * update in_msglen which keeps track of the contents'
5425 * consumption state.
5426 *
5427 * (1) Handshake messages:
5428 * Remove last handshake message, move content
5429 * and adapt in_msglen.
5430 *
5431 * (2) Alert messages:
5432 * Consume whole record content, in_msglen = 0.
5433 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 * (3) Change cipher spec:
5435 * Consume whole record content, in_msglen = 0.
5436 *
5437 * (4) Application data:
5438 * Don't do anything - the record layer provides
5439 * the application data as a stream transport
5440 * and consumes through mbedtls_ssl_read only.
5441 *
5442 */
5443
5444 /* Case (1): Handshake messages */
5445 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005446 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005447 /* Hard assertion to be sure that no application data
5448 * is in flight, as corrupting ssl->in_msglen during
5449 * ssl->in_offt != NULL is fatal. */
5450 if( ssl->in_offt != NULL )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5454 }
5455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005456 /*
5457 * Get next Handshake message in the current record
5458 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005461 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005462 * current handshake content: If DTLS handshake
5463 * fragmentation is used, that's the fragment
5464 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005465 * size here is faulty and should be changed at
5466 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 * (2) While it doesn't seem to cause problems, one
5468 * has to be very careful not to assume that in_hslen
5469 * is always <= in_msglen in a sensible communication.
5470 * Again, it's wrong for DTLS handshake fragmentation.
5471 * The following check is therefore mandatory, and
5472 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005473 * Additionally, ssl->in_hslen might be arbitrarily out of
5474 * bounds after handling a DTLS message with an unexpected
5475 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005476 */
5477 if( ssl->in_hslen < ssl->in_msglen )
5478 {
5479 ssl->in_msglen -= ssl->in_hslen;
5480 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5481 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005482
Hanno Becker4a810fb2017-05-24 16:27:30 +01005483 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5484 ssl->in_msg, ssl->in_msglen );
5485 }
5486 else
5487 {
5488 ssl->in_msglen = 0;
5489 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005490
Hanno Becker4a810fb2017-05-24 16:27:30 +01005491 ssl->in_hslen = 0;
5492 }
5493 /* Case (4): Application data */
5494 else if( ssl->in_offt != NULL )
5495 {
5496 return( 0 );
5497 }
5498 /* Everything else (CCS & Alerts) */
5499 else
5500 {
5501 ssl->in_msglen = 0;
5502 }
5503
Hanno Becker1097b342018-08-15 14:09:41 +01005504 return( 0 );
5505}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005506
Hanno Beckere74d5562018-08-15 14:26:08 +01005507static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5508{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005509 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005510 return( 1 );
5511
5512 return( 0 );
5513}
5514
Hanno Becker5f066e72018-08-16 14:56:31 +01005515#if defined(MBEDTLS_SSL_PROTO_DTLS)
5516
5517static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 if( hs == NULL )
5521 return;
5522
Hanno Becker01315ea2018-08-21 17:22:17 +01005523 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005524 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005525 hs->buffering.total_bytes_buffered -=
5526 hs->buffering.future_record.len;
5527
5528 mbedtls_free( hs->buffering.future_record.data );
5529 hs->buffering.future_record.data = NULL;
5530 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005531}
5532
5533static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5534{
5535 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5536 unsigned char * rec;
5537 size_t rec_len;
5538 unsigned rec_epoch;
5539
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005540 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005541 return( 0 );
5542
5543 if( hs == NULL )
5544 return( 0 );
5545
Hanno Becker5f066e72018-08-16 14:56:31 +01005546 rec = hs->buffering.future_record.data;
5547 rec_len = hs->buffering.future_record.len;
5548 rec_epoch = hs->buffering.future_record.epoch;
5549
5550 if( rec == NULL )
5551 return( 0 );
5552
Hanno Becker4cb782d2018-08-20 11:19:05 +01005553 /* Only consider loading future records if the
5554 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005555 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005556 return( 0 );
5557
Hanno Becker5f066e72018-08-16 14:56:31 +01005558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5559
5560 if( rec_epoch != ssl->in_epoch )
5561 {
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5563 goto exit;
5564 }
5565
5566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5567
5568 /* Double-check that the record is not too large */
5569 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5570 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5574 }
5575
5576 memcpy( ssl->in_hdr, rec, rec_len );
5577 ssl->in_left = rec_len;
5578 ssl->next_record_offset = 0;
5579
5580 ssl_free_buffered_record( ssl );
5581
5582exit:
5583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5584 return( 0 );
5585}
5586
5587static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5588{
5589 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5590 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005591 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005592
5593 /* Don't buffer future records outside handshakes. */
5594 if( hs == NULL )
5595 return( 0 );
5596
5597 /* Only buffer handshake records (we are only interested
5598 * in Finished messages). */
5599 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5600 return( 0 );
5601
5602 /* Don't buffer more than one future epoch record. */
5603 if( hs->buffering.future_record.data != NULL )
5604 return( 0 );
5605
Hanno Becker01315ea2018-08-21 17:22:17 +01005606 /* Don't buffer record if there's not enough buffering space remaining. */
5607 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5608 hs->buffering.total_bytes_buffered ) )
5609 {
5610 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",
5611 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5612 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613 return( 0 );
5614 }
5615
Hanno Becker5f066e72018-08-16 14:56:31 +01005616 /* Buffer record */
5617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5618 ssl->in_epoch + 1 ) );
5619 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5620 rec_hdr_len + ssl->in_msglen );
5621
5622 /* ssl_parse_record_header() only considers records
5623 * of the next epoch as candidates for buffering. */
5624 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005625 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005626
5627 hs->buffering.future_record.data =
5628 mbedtls_calloc( 1, hs->buffering.future_record.len );
5629 if( hs->buffering.future_record.data == NULL )
5630 {
5631 /* If we run out of RAM trying to buffer a
5632 * record from the next epoch, just ignore. */
5633 return( 0 );
5634 }
5635
Hanno Becker01315ea2018-08-21 17:22:17 +01005636 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005637
Hanno Becker01315ea2018-08-21 17:22:17 +01005638 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005639 return( 0 );
5640}
5641
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
5643
Hanno Beckere74d5562018-08-15 14:26:08 +01005644static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005645{
5646 int ret;
5647
Hanno Becker5f066e72018-08-16 14:56:31 +01005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
5649 /* We might have buffered a future record; if so,
5650 * and if the epoch matches now, load it.
5651 * On success, this call will set ssl->in_left to
5652 * the length of the buffered record, so that
5653 * the calls to ssl_fetch_input() below will
5654 * essentially be no-ops. */
5655 ret = ssl_load_buffered_record( ssl );
5656 if( ret != 0 )
5657 return( ret );
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005659
Hanno Becker8b09b732019-05-08 12:03:28 +01005660 /* Reset in pointers to default state for TLS/DTLS records,
5661 * assuming no CID and no offset between record content and
5662 * record plaintext. */
5663 ssl_update_in_pointers( ssl );
5664
5665 /* Ensure that we have enough space available for the default form
5666 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5667 * with no space for CIDs counted in). */
5668 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5669 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005672 return( ret );
5673 }
5674
5675 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005678 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005679 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005681 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5682 {
5683 ret = ssl_buffer_future_record( ssl );
5684 if( ret != 0 )
5685 return( ret );
5686
5687 /* Fall through to handling of unexpected records */
5688 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5689 }
5690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005691 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5692 {
5693 /* Skip unexpected record (but not whole datagram) */
5694 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005695 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5698 "(header)" ) );
5699 }
5700 else
5701 {
5702 /* Skip invalid record and the rest of the datagram */
5703 ssl->next_record_offset = 0;
5704 ssl->in_left = 0;
5705
5706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5707 "(header)" ) );
5708 }
5709
5710 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005711 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005712 }
5713#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005715 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005716
5717 /*
5718 * Read and optionally decrypt the message contents
5719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005721 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724 return( ret );
5725 }
5726
5727 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005730 {
Hanno Becker43395762019-05-03 14:46:38 +01005731 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005732 if( ssl->next_record_offset < ssl->in_left )
5733 {
5734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5735 }
5736 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005738#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005739#if defined(MBEDTLS_SSL_PROTO_TLS)
5740 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005741 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005742 }
5743#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744
5745 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005748 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005749 {
5750 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005751 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005753 /* Except when waiting for Finished as a bad mac here
5754 * probably means something went wrong in the handshake
5755 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5756 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5757 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5758 {
5759#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5760 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5761 {
5762 mbedtls_ssl_send_alert_message( ssl,
5763 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5764 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5765 }
5766#endif
5767 return( ret );
5768 }
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005771 if( ssl->conf->badmac_limit != 0 &&
5772 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5775 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005776 }
5777#endif
5778
Hanno Becker4a810fb2017-05-24 16:27:30 +01005779 /* As above, invalid records cause
5780 * dismissal of the whole datagram. */
5781
5782 ssl->next_record_offset = 0;
5783 ssl->in_left = 0;
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005786 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005787 }
5788
5789 return( ret );
5790 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005791 MBEDTLS_SSL_TRANSPORT_ELSE
5792#endif /* MBEDTLS_SSL_PROTO_DTLS */
5793#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005794 {
5795 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5797 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 mbedtls_ssl_send_alert_message( ssl,
5800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5801 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 }
5803#endif
5804 return( ret );
5805 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005806#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005807 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005808
Simon Butcher99000142016-10-13 17:21:01 +01005809 return( 0 );
5810}
5811
5812int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5813{
5814 int ret;
5815
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005816 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005817 * Handle particular types of records
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Simon Butcher99000142016-10-13 17:21:01 +01005821 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5822 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005823 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Hanno Beckere678eaa2018-08-21 14:57:46 +01005827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005830 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5832 ssl->in_msglen ) );
5833 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005834 }
5835
Hanno Beckere678eaa2018-08-21 14:57:46 +01005836 if( ssl->in_msg[0] != 1 )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5839 ssl->in_msg[0] ) );
5840 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5841 }
5842
5843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005844 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005845 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5846 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5847 {
5848 if( ssl->handshake == NULL )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5851 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5852 }
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5855 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5856 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005858 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005862 if( ssl->in_msglen != 2 )
5863 {
5864 /* Note: Standard allows for more than one 2 byte alert
5865 to be packed in a single message, but Mbed TLS doesn't
5866 currently support this. */
5867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5868 ssl->in_msglen ) );
5869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5870 }
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 ssl->in_msg[0], ssl->in_msg[1] ) );
5874
5875 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005876 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005881 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 }
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5886 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5889 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005891
5892#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5893 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5894 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5895 {
Hanno Becker90333da2017-10-10 11:27:13 +01005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005897 /* Will be handled when trying to parse ServerHello */
5898 return( 0 );
5899 }
5900#endif
5901
5902#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5904 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5905 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5906 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5909 /* Will be handled in mbedtls_ssl_parse_certificate() */
5910 return( 0 );
5911 }
5912#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5913
5914 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005915 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
Hanno Beckerc76c6192017-06-06 10:03:17 +01005918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005919 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005920 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005921 /* Drop unexpected ApplicationData records,
5922 * except at the beginning of renegotiations */
5923 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5924 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5925#if defined(MBEDTLS_SSL_RENEGOTIATION)
5926 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5927 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005928#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005929 )
5930 {
5931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5932 return( MBEDTLS_ERR_SSL_NON_FATAL );
5933 }
5934
5935 if( ssl->handshake != NULL &&
5936 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5937 {
5938 ssl_handshake_wrapup_free_hs_transform( ssl );
5939 }
5940 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005942
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 return( 0 );
5944}
5945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005947{
5948 int ret;
5949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5951 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5952 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005953 {
5954 return( ret );
5955 }
5956
5957 return( 0 );
5958}
5959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005961 unsigned char level,
5962 unsigned char message )
5963{
5964 int ret;
5965
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005966 if( ssl == NULL || ssl->conf == NULL )
5967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005973 ssl->out_msglen = 2;
5974 ssl->out_msg[0] = level;
5975 ssl->out_msg[1] = message;
5976
Hanno Becker67bc7c32018-08-06 11:33:50 +01005977 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005980 return( ret );
5981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005983
5984 return( 0 );
5985}
5986
Paul Bakker5121ce52009-01-03 21:22:43 +00005987/*
5988 * Handshake functions
5989 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005990#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005991/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005993{
Hanno Becker8759e162017-12-27 21:34:08 +00005994 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005997
Hanno Becker5097cba2019-02-05 13:36:46 +00005998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006001 ssl->state++;
6002 return( 0 );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007}
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006010{
Hanno Becker8759e162017-12-27 21:34:08 +00006011 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006014
Hanno Becker5097cba2019-02-05 13:36:46 +00006015 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006018 ssl->state++;
6019 return( 0 );
6020 }
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006024}
Gilles Peskinef9828522017-05-03 12:28:43 +02006025
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006026#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006027/* Some certificate support -> implement write and parse */
6028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006032 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006034 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037
Hanno Becker5097cba2019-02-05 13:36:46 +00006038 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006041 ssl->state++;
6042 return( 0 );
6043 }
6044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006046 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006047 {
6048 if( ssl->client_auth == 0 )
6049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006051 ssl->state++;
6052 return( 0 );
6053 }
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 /*
6057 * If using SSLv3 and got no cert, send an Alert message
6058 * (otherwise an empty Certificate message will be sent).
6059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6061 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 {
6063 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6065 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6066 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 goto write_msg;
6070 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073#endif /* MBEDTLS_SSL_CLI_C */
6074#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006075 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6080 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081 }
6082 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006083#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006086
6087 /*
6088 * 0 . 0 handshake type
6089 * 1 . 3 handshake length
6090 * 4 . 6 length of all certs
6091 * 7 . 9 length of cert. 1
6092 * 10 . n-1 peer certificate
6093 * n . n+2 length of cert. 2
6094 * n+3 . ... upper level cert, etc.
6095 */
6096 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006098
Paul Bakker29087132010-03-21 21:03:34 +00006099 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006100 {
6101 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006102 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006105 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006107 }
6108
6109 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6110 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6111 ssl->out_msg[i + 2] = (unsigned char)( n );
6112
6113 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6114 i += n; crt = crt->next;
6115 }
6116
6117 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6118 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6119 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6120
6121 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6123 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006124
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006125#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006126write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006127#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006128
6129 ssl->state++;
6130
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006131 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 return( ret );
6135 }
6136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006138
Paul Bakkered27a042013-04-18 22:46:23 +02006139 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140}
6141
Hanno Becker285ff0c2019-01-31 07:44:03 +00006142#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006143
6144#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006145static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6146 unsigned char *crt_buf,
6147 size_t crt_buf_len )
6148{
6149 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6150
6151 if( peer_crt == NULL )
6152 return( -1 );
6153
6154 if( peer_crt->raw.len != crt_buf_len )
6155 return( -1 );
6156
Hanno Becker68b856d2019-02-08 14:00:04 +00006157 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006158}
Hanno Beckerdf759382019-02-05 17:02:46 +00006159#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6160static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6161 unsigned char *crt_buf,
6162 size_t crt_buf_len )
6163{
6164 int ret;
6165 unsigned char const * const peer_cert_digest =
6166 ssl->session->peer_cert_digest;
6167 mbedtls_md_type_t const peer_cert_digest_type =
6168 ssl->session->peer_cert_digest_type;
6169 mbedtls_md_info_t const * const digest_info =
6170 mbedtls_md_info_from_type( peer_cert_digest_type );
6171 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6172 size_t digest_len;
6173
6174 if( peer_cert_digest == NULL || digest_info == NULL )
6175 return( -1 );
6176
6177 digest_len = mbedtls_md_get_size( digest_info );
6178 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6179 return( -1 );
6180
6181 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6182 if( ret != 0 )
6183 return( -1 );
6184
6185 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6186}
6187#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006188#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006189
Hanno Becker22141592019-02-05 12:38:15 +00006190static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6191{
Hanno Becker50628972019-02-07 13:17:53 +00006192#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker22141592019-02-05 12:38:15 +00006193 if( session->peer_cert != NULL )
6194 {
6195 mbedtls_x509_crt_free( session->peer_cert );
6196 mbedtls_free( session->peer_cert );
6197 session->peer_cert = NULL;
6198 }
Hanno Becker50628972019-02-07 13:17:53 +00006199#else
Hanno Becker9fb6e2e2019-02-05 17:00:50 +00006200 if( session->peer_cert_digest != NULL )
6201 {
6202 /* Zeroization is not necessary. */
6203 mbedtls_free( session->peer_cert_digest );
6204 session->peer_cert_digest = NULL;
6205 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6206 session->peer_cert_digest_len = 0;
6207 }
Hanno Becker50628972019-02-07 13:17:53 +00006208#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker22141592019-02-05 12:38:15 +00006209}
6210
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006211/*
6212 * Once the certificate message is read, parse it into a cert chain and
6213 * perform basic checks, but leave actual verification to the caller
6214 */
Hanno Becker35e41772019-02-05 15:37:23 +00006215static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6216 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006217{
Hanno Becker35e41772019-02-05 15:37:23 +00006218 int ret;
6219#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6220 int crt_cnt=0;
6221#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006222 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006223 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006228 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6229 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006231 }
6232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6234 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006237 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6238 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 }
6241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006243
Paul Bakker5121ce52009-01-03 21:22:43 +00006244 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006247 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006248
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006249 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006250 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006253 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6254 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 }
6257
Hanno Becker33c3dc82019-01-30 14:46:46 +00006258 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6259 i += 3;
6260
Hanno Becker33c3dc82019-01-30 14:46:46 +00006261 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 while( i < ssl->in_hslen )
6263 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006264 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006265 if ( i + 3 > ssl->in_hslen ) {
6266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006267 mbedtls_ssl_send_alert_message( ssl,
6268 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6269 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006270 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6271 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006272 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6273 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 if( ssl->in_msg[i] != 0 )
6275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006277 mbedtls_ssl_send_alert_message( ssl,
6278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6279 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006281 }
6282
Hanno Becker33c3dc82019-01-30 14:46:46 +00006283 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6285 | (unsigned int) ssl->in_msg[i + 2];
6286 i += 3;
6287
6288 if( n < 128 || i + n > ssl->in_hslen )
6289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006291 mbedtls_ssl_send_alert_message( ssl,
6292 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6293 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 }
6296
Hanno Becker33c3dc82019-01-30 14:46:46 +00006297 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006298#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6299 if( crt_cnt++ == 0 &&
6300 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6301 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006302 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006303 /* During client-side renegotiation, check that the server's
6304 * end-CRTs hasn't changed compared to the initial handshake,
6305 * mitigating the triple handshake attack. On success, reuse
6306 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6308 if( ssl_check_peer_crt_unchanged( ssl,
6309 &ssl->in_msg[i],
6310 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006311 {
Hanno Becker35e41772019-02-05 15:37:23 +00006312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6313 mbedtls_ssl_send_alert_message( ssl,
6314 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6315 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6316 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006317 }
Hanno Becker35e41772019-02-05 15:37:23 +00006318
6319 /* Now we can safely free the original chain. */
6320 ssl_clear_peer_cert( ssl->session );
6321 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006322#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6323
Hanno Becker33c3dc82019-01-30 14:46:46 +00006324 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006325 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006326 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006327 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006328 case 0: /*ok*/
6329 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6330 /* Ignore certificate with an unknown algorithm: maybe a
6331 prior certificate was already trusted. */
6332 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006333
Hanno Becker33c3dc82019-01-30 14:46:46 +00006334 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6335 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6336 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006337
Hanno Becker33c3dc82019-01-30 14:46:46 +00006338 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6339 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6340 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341
Hanno Becker33c3dc82019-01-30 14:46:46 +00006342 default:
6343 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6344 crt_parse_der_failed:
6345 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6346 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6347 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 }
6349
6350 i += n;
6351 }
6352
Hanno Becker35e41772019-02-05 15:37:23 +00006353 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006354 return( 0 );
6355}
6356
Hanno Beckerb8a08572019-02-05 12:49:06 +00006357#if defined(MBEDTLS_SSL_SRV_C)
6358static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6359{
6360 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6361 return( -1 );
6362
6363#if defined(MBEDTLS_SSL_PROTO_SSL3)
6364 /*
6365 * Check if the client sent an empty certificate
6366 */
6367 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6368 {
6369 if( ssl->in_msglen == 2 &&
6370 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6371 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6372 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6373 {
6374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6375 return( 0 );
6376 }
6377
6378 return( -1 );
6379 }
6380#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6381
6382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6384 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6385 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6386 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6387 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6388 {
6389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6390 return( 0 );
6391 }
6392
6393 return( -1 );
6394#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6395 MBEDTLS_SSL_PROTO_TLS1_2 */
6396}
6397#endif /* MBEDTLS_SSL_SRV_C */
6398
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006399/* Check if a certificate message is expected.
6400 * Return either
6401 * - SSL_CERTIFICATE_EXPECTED, or
6402 * - SSL_CERTIFICATE_SKIP
6403 * indicating whether a Certificate message is expected or not.
6404 */
6405#define SSL_CERTIFICATE_EXPECTED 0
6406#define SSL_CERTIFICATE_SKIP 1
6407static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6408 int authmode )
6409{
6410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6411 ssl->handshake->ciphersuite_info;
6412
6413 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6414 return( SSL_CERTIFICATE_SKIP );
6415
6416#if defined(MBEDTLS_SSL_SRV_C)
6417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6418 {
6419 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6420 return( SSL_CERTIFICATE_SKIP );
6421
6422 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6423 {
6424 /* NOTE: Is it intentional that we set verify_result
6425 * to SKIP_VERIFY on server-side only? */
6426 ssl->session_negotiate->verify_result =
6427 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6428 return( SSL_CERTIFICATE_SKIP );
6429 }
6430 }
6431#endif /* MBEDTLS_SSL_SRV_C */
6432
6433 return( SSL_CERTIFICATE_EXPECTED );
6434}
6435
Hanno Becker3cf50612019-02-05 14:36:34 +00006436static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6437 int authmode,
6438 mbedtls_x509_crt *chain,
6439 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006440{
Hanno Becker613d4902019-02-05 13:11:17 +00006441 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006442 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6443 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006444 mbedtls_x509_crt *ca_chain;
6445 mbedtls_x509_crl *ca_crl;
6446
6447 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6448 return( 0 );
6449
6450#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6451 if( ssl->handshake->sni_ca_chain != NULL )
6452 {
6453 ca_chain = ssl->handshake->sni_ca_chain;
6454 ca_crl = ssl->handshake->sni_ca_crl;
6455 }
6456 else
6457#endif
6458 {
6459 ca_chain = ssl->conf->ca_chain;
6460 ca_crl = ssl->conf->ca_crl;
6461 }
6462
6463 /*
6464 * Main check: verify certificate
6465 */
6466 ret = mbedtls_x509_crt_verify_restartable(
6467 chain,
6468 ca_chain, ca_crl,
6469 ssl->conf->cert_profile,
6470 ssl->hostname,
6471 &ssl->session_negotiate->verify_result,
6472 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6473
6474 if( ret != 0 )
6475 {
6476 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6477 }
6478
6479#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6480 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6481 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6482#endif
6483
6484 /*
6485 * Secondary checks: always done, but change 'ret' only if it was 0
6486 */
6487
6488#if defined(MBEDTLS_ECP_C)
6489 {
6490 const mbedtls_pk_context *pk = &chain->pk;
6491
6492 /* If certificate uses an EC key, make sure the curve is OK */
6493 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6494 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6495 {
6496 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6497
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6499 if( ret == 0 )
6500 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6501 }
6502 }
6503#endif /* MBEDTLS_ECP_C */
6504
6505 if( mbedtls_ssl_check_cert_usage( chain,
6506 ciphersuite_info,
6507 ! ssl->conf->endpoint,
6508 &ssl->session_negotiate->verify_result ) != 0 )
6509 {
6510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6511 if( ret == 0 )
6512 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6513 }
6514
6515 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6516 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6517 * with details encoded in the verification flags. All other kinds
6518 * of error codes, including those from the user provided f_vrfy
6519 * functions, are treated as fatal and lead to a failure of
6520 * ssl_parse_certificate even if verification was optional. */
6521 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6522 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6523 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6524 {
6525 ret = 0;
6526 }
6527
6528 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6529 {
6530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6531 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6532 }
6533
6534 if( ret != 0 )
6535 {
6536 uint8_t alert;
6537
6538 /* The certificate may have been rejected for several reasons.
6539 Pick one and send the corresponding alert. Which alert to send
6540 may be a subject of debate in some cases. */
6541 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6542 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6543 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6544 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6545 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6546 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6547 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6548 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6549 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6550 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6551 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6552 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6553 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6554 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6555 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6556 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6557 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6558 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6559 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6560 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6561 else
6562 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6563 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6564 alert );
6565 }
6566
6567#if defined(MBEDTLS_DEBUG_C)
6568 if( ssl->session_negotiate->verify_result != 0 )
6569 {
6570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6571 ssl->session_negotiate->verify_result ) );
6572 }
6573 else
6574 {
6575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6576 }
6577#endif /* MBEDTLS_DEBUG_C */
6578
6579 return( ret );
6580}
6581
6582int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6583{
6584 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006585 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006586#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6587 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6588 ? ssl->handshake->sni_authmode
6589 : ssl->conf->authmode;
6590#else
6591 const int authmode = ssl->conf->authmode;
6592#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006593 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006594 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006595
6596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6597
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006598 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6599 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006600 {
6601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006602 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006603 }
6604
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006605#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6606 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006607 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006608 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006609 chain = ssl->handshake->ecrs_peer_cert;
6610 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006611 goto crt_verify;
6612 }
6613#endif
6614
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006615 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006616 {
6617 /* mbedtls_ssl_read_record may have sent an alert already. We
6618 let it decide whether to alert. */
6619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006620 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006621 }
6622
Hanno Beckerb8a08572019-02-05 12:49:06 +00006623#if defined(MBEDTLS_SSL_SRV_C)
6624 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6625 {
6626 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006627
6628 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006629 ret = 0;
6630 else
6631 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006632
Hanno Becker613d4902019-02-05 13:11:17 +00006633 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006634 }
6635#endif /* MBEDTLS_SSL_SRV_C */
6636
Hanno Becker35e41772019-02-05 15:37:23 +00006637 /* Clear existing peer CRT structure in case we tried to
6638 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006639 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006640
6641 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6642 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006643 {
6644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6645 sizeof( mbedtls_x509_crt ) ) );
6646 mbedtls_ssl_send_alert_message( ssl,
6647 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6648 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006649
Hanno Beckere4aeb762019-02-05 17:19:52 +00006650 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6651 goto exit;
6652 }
6653 mbedtls_x509_crt_init( chain );
6654
6655 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006656 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006657 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006658
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006659#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6660 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006661 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006662
6663crt_verify:
6664 if( ssl->handshake->ecrs_enabled)
6665 rs_ctx = &ssl->handshake->ecrs_ctx;
6666#endif
6667
Hanno Becker3cf50612019-02-05 14:36:34 +00006668 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006669 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006670 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006671 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006672
Hanno Becker3008d282019-02-05 17:02:28 +00006673#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckere4aeb762019-02-05 17:19:52 +00006674
Hanno Becker3008d282019-02-05 17:02:28 +00006675 /* Remember digest of the peer's end-CRT. */
6676 ssl->session_negotiate->peer_cert_digest =
6677 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6678 if( ssl->session_negotiate->peer_cert_digest == NULL )
6679 {
6680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6681 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6682 mbedtls_ssl_send_alert_message( ssl,
6683 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6684 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006685
6686 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6687 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006688 }
6689 ret = mbedtls_md( mbedtls_md_info_from_type(
Hanno Beckere4aeb762019-02-05 17:19:52 +00006690 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6691 chain->raw.p, chain->raw.len,
Hanno Becker3008d282019-02-05 17:02:28 +00006692 ssl->session_negotiate->peer_cert_digest );
6693 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006694 goto exit;
Hanno Becker3008d282019-02-05 17:02:28 +00006695
6696 ssl->session_negotiate->peer_cert_digest_type =
6697 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6698 ssl->session_negotiate->peer_cert_digest_len =
6699 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6700#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6701
Hanno Beckercf291d62019-02-06 16:19:04 +00006702#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6703 /* Make a copy of the peer's raw public key. */
6704 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6705 {
6706 unsigned char *p, *end;
6707 p = chain->pk_raw.p;
6708 end = p + chain->pk_raw.len;
6709 ret = mbedtls_pk_parse_subpubkey( &p, end,
6710 &ssl->handshake->peer_pubkey );
6711 if( ret != 0 )
6712 {
6713 /* We should have parsed the public key before. */
6714 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6715 goto exit;
6716 }
6717 }
Hanno Becker81d11aa2019-02-07 13:18:21 +00006718#else
Hanno Beckere4aeb762019-02-05 17:19:52 +00006719 ssl->session_negotiate->peer_cert = chain;
6720 chain = NULL;
Hanno Becker81d11aa2019-02-07 13:18:21 +00006721#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006724
Hanno Becker613d4902019-02-05 13:11:17 +00006725exit:
6726
Hanno Beckere4aeb762019-02-05 17:19:52 +00006727 if( ret == 0 )
6728 ssl->state++;
6729
6730#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6731 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6732 {
6733 ssl->handshake->ecrs_peer_cert = chain;
6734 chain = NULL;
6735 }
6736#endif
6737
6738 if( chain != NULL )
6739 {
6740 mbedtls_x509_crt_free( chain );
6741 mbedtls_free( chain );
6742 }
6743
Paul Bakker5121ce52009-01-03 21:22:43 +00006744 return( ret );
6745}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006746#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006749{
6750 int ret;
6751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006754 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006755 ssl->out_msglen = 1;
6756 ssl->out_msg[0] = 1;
6757
Paul Bakker5121ce52009-01-03 21:22:43 +00006758 ssl->state++;
6759
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006760 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006761 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006762 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006763 return( ret );
6764 }
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006767
6768 return( 0 );
6769}
6770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006771int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006772{
6773 int ret;
6774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776
Hanno Becker327c93b2018-08-15 13:56:18 +01006777 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 return( ret );
6781 }
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006786 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6787 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790
Hanno Beckere678eaa2018-08-21 14:57:46 +01006791 /* CCS records are only accepted if they have length 1 and content '1',
6792 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006793
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006794 /*
6795 * Switch to our negotiated transform and session parameters for inbound
6796 * data.
6797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006799 ssl->transform_in = ssl->transform_negotiate;
6800 ssl->session_in = ssl->session_negotiate;
6801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006803 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006806 ssl_dtls_replay_reset( ssl );
6807#endif
6808
6809 /* Increment epoch */
6810 if( ++ssl->in_epoch == 0 )
6811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006813 /* This is highly unlikely to happen for legitimate reasons, so
6814 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006816 }
6817 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006818 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006820#if defined(MBEDTLS_SSL_PROTO_TLS)
6821 {
6822 memset( ssl->in_ctr, 0, 8 );
6823 }
6824#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006825
Hanno Beckerf5970a02019-05-08 09:38:41 +01006826 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006828#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6829 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006834 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6835 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006837 }
6838 }
6839#endif
6840
Paul Bakker5121ce52009-01-03 21:22:43 +00006841 ssl->state++;
6842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844
6845 return( 0 );
6846}
6847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6849 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006850{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006851 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6854 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6855 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006856 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006857 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6860#if defined(MBEDTLS_SHA512_C)
6861 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006862 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6863 else
6864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SHA256_C)
6866 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006867 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006868 else
6869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006873 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006874 }
Paul Bakker380da532012-04-18 16:10:25 +00006875}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006878{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6880 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006881 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6882 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006883#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6885#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006886 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006888#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006889 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006892}
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006895 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006896{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6898 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006899 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6900 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006902#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6903#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006904 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006907 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006909#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006910}
6911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6913 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6914static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006915 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006916{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006917 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6918 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006919}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006920#endif
Paul Bakker380da532012-04-18 16:10:25 +00006921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6923#if defined(MBEDTLS_SHA256_C)
6924static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006925 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006926{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006927 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006928}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006929#endif
Paul Bakker380da532012-04-18 16:10:25 +00006930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931#if defined(MBEDTLS_SHA512_C)
6932static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006933 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006934{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006935 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006936}
Paul Bakker769075d2012-11-24 11:26:46 +01006937#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006941static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006943{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006944 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006945 mbedtls_md5_context md5;
6946 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006947
Paul Bakker5121ce52009-01-03 21:22:43 +00006948 unsigned char padbuf[48];
6949 unsigned char md5sum[16];
6950 unsigned char sha1sum[20];
6951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006953 if( !session )
6954 session = ssl->session;
6955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006957
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006958 mbedtls_md5_init( &md5 );
6959 mbedtls_sha1_init( &sha1 );
6960
6961 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6962 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006963
6964 /*
6965 * SSLv3:
6966 * hash =
6967 * MD5( master + pad2 +
6968 * MD5( handshake + sender + master + pad1 ) )
6969 * + SHA1( master + pad2 +
6970 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006971 */
6972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006974 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6975 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006976#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006979 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6980 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006981#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006984 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006985
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006987
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006988 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6989 mbedtls_md5_update_ret( &md5, session->master, 48 );
6990 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6991 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006992
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006993 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6994 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6995 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6996 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006997
Paul Bakker1ef83d62012-04-11 12:09:53 +00006998 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006999
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007000 mbedtls_md5_starts_ret( &md5 );
7001 mbedtls_md5_update_ret( &md5, session->master, 48 );
7002 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7003 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7004 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007005
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007006 mbedtls_sha1_starts_ret( &sha1 );
7007 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7008 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7009 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7010 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007014 mbedtls_md5_free( &md5 );
7015 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007016
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007017 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7018 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7019 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007022}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007026static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007028{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007029 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007030 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007031 mbedtls_md5_context md5;
7032 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007033 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007036 if( !session )
7037 session = ssl->session;
7038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007040
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007041 mbedtls_md5_init( &md5 );
7042 mbedtls_sha1_init( &sha1 );
7043
7044 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7045 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007046
Paul Bakker1ef83d62012-04-11 12:09:53 +00007047 /*
7048 * TLSv1:
7049 * hash = PRF( master, finished_label,
7050 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7051 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007053#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007054 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7055 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007056#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007059 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7060 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007061#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007064 ? "client finished"
7065 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007066
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007067 mbedtls_md5_finish_ret( &md5, padbuf );
7068 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007069
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007070 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007071 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007074
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007075 mbedtls_md5_free( &md5 );
7076 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007077
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007078 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7085#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007086static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007088{
7089 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007090 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007091 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007092 unsigned char padbuf[32];
7093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007095 if( !session )
7096 session = ssl->session;
7097
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007098 mbedtls_sha256_init( &sha256 );
7099
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007101
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007102 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
7104 /*
7105 * TLSv1.2:
7106 * hash = PRF( master, finished_label,
7107 * Hash( handshake ) )[0.11]
7108 */
7109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110#if !defined(MBEDTLS_SHA256_ALT)
7111 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007112 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007113#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007116 ? "client finished"
7117 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007118
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007119 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007121 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007122 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007125
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007126 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007127
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007128 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007135static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007137{
7138 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007139 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007140 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007141 unsigned char padbuf[48];
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007144 if( !session )
7145 session = ssl->session;
7146
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007147 mbedtls_sha512_init( &sha512 );
7148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007150
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007151 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007152
7153 /*
7154 * TLSv1.2:
7155 * hash = PRF( master, finished_label,
7156 * Hash( handshake ) )[0.11]
7157 */
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007160 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7161 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007162#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007165 ? "client finished"
7166 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007167
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007168 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007169
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007170 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007171 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007174
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007175 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007176
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007177 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181#endif /* MBEDTLS_SHA512_C */
7182#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007185{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007187
7188 /*
7189 * Free our handshake params
7190 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007191 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007193 ssl->handshake = NULL;
7194
7195 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007196 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007197 */
7198 if( ssl->transform )
7199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 mbedtls_ssl_transform_free( ssl->transform );
7201 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007202 }
7203 ssl->transform = ssl->transform_negotiate;
7204 ssl->transform_negotiate = NULL;
7205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007207}
7208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007210{
7211 int resume = ssl->handshake->resume;
7212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_RENEGOTIATION)
7216 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007219 ssl->renego_records_seen = 0;
7220 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007221#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007222
7223 /*
7224 * Free the previous session and switch in the current one
7225 */
Paul Bakker0a597072012-09-25 21:55:46 +00007226 if( ssl->session )
7227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007229 /* RFC 7366 3.1: keep the EtM state */
7230 ssl->session_negotiate->encrypt_then_mac =
7231 ssl->session->encrypt_then_mac;
7232#endif
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 mbedtls_ssl_session_free( ssl->session );
7235 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007236 }
7237 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007238 ssl->session_negotiate = NULL;
7239
Paul Bakker0a597072012-09-25 21:55:46 +00007240 /*
7241 * Add cache entry
7242 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007243 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007244 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007245 resume == 0 )
7246 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007247 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007249 }
Paul Bakker0a597072012-09-25 21:55:46 +00007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007252 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007253 ssl->handshake->flight != NULL )
7254 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007255 /* Cancel handshake timer */
7256 ssl_set_timer( ssl, 0 );
7257
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007258 /* Keep last flight around in case we need to resend it:
7259 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007261 }
7262 else
7263#endif
7264 ssl_handshake_wrapup_free_hs_transform( ssl );
7265
Paul Bakker48916f92012-09-16 19:57:18 +00007266 ssl->state++;
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007269}
7270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007272{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007273 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007276
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007277 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007278
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007279 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007280
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007281 /*
7282 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7283 * may define some other value. Currently (early 2016), no defined
7284 * ciphersuite does this (and this is unlikely to change as activity has
7285 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007290 ssl->verify_data_len = hash_len;
7291 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007292#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007293
Paul Bakker5121ce52009-01-03 21:22:43 +00007294 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7296 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007297
7298 /*
7299 * In case of session resuming, invert the client and server
7300 * ChangeCipherSpec messages order.
7301 */
Paul Bakker0a597072012-09-25 21:55:46 +00007302 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007305 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007309 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 }
7313 else
7314 ssl->state++;
7315
Paul Bakker48916f92012-09-16 19:57:18 +00007316 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007317 * Switch to our negotiated transform and session parameters for outbound
7318 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007324 {
7325 unsigned char i;
7326
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007327 /* Remember current epoch settings for resending */
7328 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007329 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007330
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007331 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007332 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007333
7334 /* Increment epoch */
7335 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007336 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007337 break;
7338
7339 /* The loop goes to its end iff the counter is wrapping */
7340 if( i == 0 )
7341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7343 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007344 }
7345 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007346 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007348#if defined(MBEDTLS_SSL_PROTO_TLS)
7349 {
7350 memset( ssl->cur_out_ctr, 0, 8 );
7351 }
7352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007353
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007354 ssl->transform_out = ssl->transform_negotiate;
7355 ssl->session_out = ssl->session_negotiate;
7356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7358 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7363 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007364 }
7365 }
7366#endif
7367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007369 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007371#endif
7372
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007373 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007374 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376 return( ret );
7377 }
7378
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007379#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007380 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007381 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7382 {
7383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7384 return( ret );
7385 }
7386#endif
7387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
7390 return( 0 );
7391}
7392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007394#define SSL_MAX_HASH_LEN 36
7395#else
7396#define SSL_MAX_HASH_LEN 12
7397#endif
7398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007400{
Paul Bakker23986e52011-04-24 08:57:21 +00007401 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007402 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007403 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007406
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007407 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007408
Hanno Becker327c93b2018-08-15 13:56:18 +01007409 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 return( ret );
7413 }
7414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007418 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7419 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007421 }
7422
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007423 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424#if defined(MBEDTLS_SSL_PROTO_SSL3)
7425 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007426 hash_len = 36;
7427 else
7428#endif
7429 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7432 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007435 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7436 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007438 }
7439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007441 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007444 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7445 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007447 }
7448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007450 ssl->verify_data_len = hash_len;
7451 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007452#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007453
Paul Bakker0a597072012-09-25 21:55:46 +00007454 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007456#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007457 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007459#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007460#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007461 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007464 }
7465 else
7466 ssl->state++;
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007469 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007471#endif
7472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007474
7475 return( 0 );
7476}
7477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007479{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7483 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7484 mbedtls_md5_init( &handshake->fin_md5 );
7485 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007486 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7487 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007488#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7490#if defined(MBEDTLS_SHA256_C)
7491 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007492 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494#if defined(MBEDTLS_SHA512_C)
7495 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007496 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007497#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007499
7500 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007501
7502#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7503 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7504 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7505#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507#if defined(MBEDTLS_DHM_C)
7508 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007509#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_ECDH_C)
7511 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007512#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007513#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007514 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007515#if defined(MBEDTLS_SSL_CLI_C)
7516 handshake->ecjpake_cache = NULL;
7517 handshake->ecjpake_cache_len = 0;
7518#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007519#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007520
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007521#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007522 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007523#endif
7524
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007525#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7526 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7527#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007528
7529#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7530 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7531 mbedtls_pk_init( &handshake->peer_pubkey );
7532#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007533}
7534
Hanno Becker611a83b2018-01-03 14:27:32 +00007535void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7540 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007541
Hanno Becker92231322018-01-03 15:32:51 +00007542#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543 mbedtls_md_init( &transform->md_ctx_enc );
7544 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007545#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007546}
7547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007548void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007549{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007551}
7552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007554{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007555 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007556 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007558 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007560 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007561 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007562
7563 /*
7564 * Either the pointers are now NULL or cleared properly and can be freed.
7565 * Now allocate missing structures.
7566 */
7567 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007568 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007569 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007570 }
Paul Bakker48916f92012-09-16 19:57:18 +00007571
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007572 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007573 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007574 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007575 }
Paul Bakker48916f92012-09-16 19:57:18 +00007576
Paul Bakker82788fb2014-10-20 13:59:19 +02007577 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007578 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007579 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007580 }
Paul Bakker48916f92012-09-16 19:57:18 +00007581
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007583 if( ssl->handshake == NULL ||
7584 ssl->transform_negotiate == NULL ||
7585 ssl->session_negotiate == NULL )
7586 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 mbedtls_free( ssl->handshake );
7590 mbedtls_free( ssl->transform_negotiate );
7591 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007592
7593 ssl->handshake = NULL;
7594 ssl->transform_negotiate = NULL;
7595 ssl->session_negotiate = NULL;
7596
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007598 }
7599
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007600 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007602 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007603 ssl_handshake_params_init( ssl->handshake );
7604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007606 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007607 {
7608 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007609
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007610 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7612 else
7613 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007614
7615 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007616 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007617#endif
7618
Paul Bakker48916f92012-09-16 19:57:18 +00007619 return( 0 );
7620}
7621
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007622#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007623/* Dummy cookie callbacks for defaults */
7624static int ssl_cookie_write_dummy( void *ctx,
7625 unsigned char **p, unsigned char *end,
7626 const unsigned char *cli_id, size_t cli_id_len )
7627{
7628 ((void) ctx);
7629 ((void) p);
7630 ((void) end);
7631 ((void) cli_id);
7632 ((void) cli_id_len);
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007635}
7636
7637static int ssl_cookie_check_dummy( void *ctx,
7638 const unsigned char *cookie, size_t cookie_len,
7639 const unsigned char *cli_id, size_t cli_id_len )
7640{
7641 ((void) ctx);
7642 ((void) cookie);
7643 ((void) cookie_len);
7644 ((void) cli_id);
7645 ((void) cli_id_len);
7646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007648}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007649#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007650
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007651/* Once ssl->out_hdr as the address of the beginning of the
7652 * next outgoing record is set, deduce the other pointers.
7653 *
7654 * Note: For TLS, we save the implicit record sequence number
7655 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7656 * and the caller has to make sure there's space for this.
7657 */
7658
7659static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7660 mbedtls_ssl_transform *transform )
7661{
7662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007663 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007664 {
7665 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007666#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007667 ssl->out_cid = ssl->out_ctr + 8;
7668 ssl->out_len = ssl->out_cid;
7669 if( transform != NULL )
7670 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007671#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007672 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007673#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007674 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007675 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007676 MBEDTLS_SSL_TRANSPORT_ELSE
7677#endif /* MBEDTLS_SSL_PROTO_DTLS */
7678#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007679 {
7680 ssl->out_ctr = ssl->out_hdr - 8;
7681 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007682#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007683 ssl->out_cid = ssl->out_len;
7684#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007685 ssl->out_iv = ssl->out_hdr + 5;
7686 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007687#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007688
7689 /* Adjust out_msg to make space for explicit IV, if used. */
7690 if( transform != NULL &&
7691 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7692 {
7693 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7694 }
7695 else
7696 ssl->out_msg = ssl->out_iv;
7697}
7698
7699/* Once ssl->in_hdr as the address of the beginning of the
7700 * next incoming record is set, deduce the other pointers.
7701 *
7702 * Note: For TLS, we save the implicit record sequence number
7703 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7704 * and the caller has to make sure there's space for this.
7705 */
7706
Hanno Beckerf5970a02019-05-08 09:38:41 +01007707static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007708{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007709 /* This function sets the pointers to match the case
7710 * of unprotected TLS/DTLS records, with both ssl->in_iv
7711 * and ssl->in_msg pointing to the beginning of the record
7712 * content.
7713 *
7714 * When decrypting a protected record, ssl->in_msg
7715 * will be shifted to point to the beginning of the
7716 * record plaintext.
7717 */
7718
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007720 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007721 {
Hanno Becker70e79282019-05-03 14:34:53 +01007722 /* This sets the header pointers to match records
7723 * without CID. When we receive a record containing
7724 * a CID, the fields are shifted accordingly in
7725 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007726 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007727#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007728 ssl->in_cid = ssl->in_ctr + 8;
7729 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007730#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007731 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007733 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007734 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007735 MBEDTLS_SSL_TRANSPORT_ELSE
7736#endif /* MBEDTLS_SSL_PROTO_DTLS */
7737#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007738 {
7739 ssl->in_ctr = ssl->in_hdr - 8;
7740 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007742 ssl->in_cid = ssl->in_len;
7743#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007744 ssl->in_iv = ssl->in_hdr + 5;
7745 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007746#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007747
Hanno Beckerf5970a02019-05-08 09:38:41 +01007748 /* This will be adjusted at record decryption time. */
7749 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007750}
7751
Paul Bakker5121ce52009-01-03 21:22:43 +00007752/*
7753 * Initialize an SSL context
7754 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007755void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7756{
7757 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7758}
7759
7760/*
7761 * Setup an SSL context
7762 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007763
7764static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7765{
7766 /* Set the incoming and outgoing record pointers. */
7767#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007768 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007769 {
7770 ssl->out_hdr = ssl->out_buf;
7771 ssl->in_hdr = ssl->in_buf;
7772 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007773 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007775#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007776 {
7777 ssl->out_hdr = ssl->out_buf + 8;
7778 ssl->in_hdr = ssl->in_buf + 8;
7779 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007780#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007781
7782 /* Derive other internal pointers. */
7783 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007784 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007785}
7786
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007787int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007788 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007789{
Paul Bakker48916f92012-09-16 19:57:18 +00007790 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007791
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007792 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007793
7794 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007795 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007796 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007797
7798 /* Set to NULL in case of an error condition */
7799 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007800
Angus Grattond8213d02016-05-25 20:56:48 +10007801 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7802 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007803 {
Angus Grattond8213d02016-05-25 20:56:48 +10007804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007805 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007806 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007807 }
7808
7809 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7810 if( ssl->out_buf == NULL )
7811 {
7812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007813 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007814 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007815 }
7816
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007817 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007818
Paul Bakker48916f92012-09-16 19:57:18 +00007819 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007820 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
7822 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007823
7824error:
7825 mbedtls_free( ssl->in_buf );
7826 mbedtls_free( ssl->out_buf );
7827
7828 ssl->conf = NULL;
7829
7830 ssl->in_buf = NULL;
7831 ssl->out_buf = NULL;
7832
7833 ssl->in_hdr = NULL;
7834 ssl->in_ctr = NULL;
7835 ssl->in_len = NULL;
7836 ssl->in_iv = NULL;
7837 ssl->in_msg = NULL;
7838
7839 ssl->out_hdr = NULL;
7840 ssl->out_ctr = NULL;
7841 ssl->out_len = NULL;
7842 ssl->out_iv = NULL;
7843 ssl->out_msg = NULL;
7844
k-stachowiak9f7798e2018-07-31 16:52:32 +02007845 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007846}
7847
7848/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007849 * Reset an initialized and used SSL context for re-use while retaining
7850 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007851 *
7852 * If partial is non-zero, keep data in the input buffer and client ID.
7853 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007854 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007855static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007856{
Paul Bakker48916f92012-09-16 19:57:18 +00007857 int ret;
7858
Hanno Becker7e772132018-08-10 12:38:21 +01007859#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7860 !defined(MBEDTLS_SSL_SRV_C)
7861 ((void) partial);
7862#endif
7863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007865
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007866 /* Cancel any possibly running timer */
7867 ssl_set_timer( ssl, 0 );
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_RENEGOTIATION)
7870 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007871 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007872
7873 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7875 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007876#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007878
Paul Bakker7eb013f2011-10-06 12:37:39 +00007879 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007880 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007881
7882 ssl->in_msgtype = 0;
7883 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007885 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007886 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007887#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007889 ssl_dtls_replay_reset( ssl );
7890#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007891
7892 ssl->in_hslen = 0;
7893 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007894
7895 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007896
7897 ssl->out_msgtype = 0;
7898 ssl->out_msglen = 0;
7899 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7901 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007902 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007903#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007904
Hanno Becker19859472018-08-06 09:40:20 +01007905 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7906
Paul Bakker48916f92012-09-16 19:57:18 +00007907 ssl->transform_in = NULL;
7908 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007909
Hanno Becker78640902018-08-13 16:35:15 +01007910 ssl->session_in = NULL;
7911 ssl->session_out = NULL;
7912
Angus Grattond8213d02016-05-25 20:56:48 +10007913 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007914
7915#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007916 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007917#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7918 {
7919 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007920 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007921 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7924 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7927 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7930 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007931 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007932 }
7933#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007934
Paul Bakker48916f92012-09-16 19:57:18 +00007935 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 mbedtls_ssl_transform_free( ssl->transform );
7938 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007939 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007940 }
Paul Bakker48916f92012-09-16 19:57:18 +00007941
Paul Bakkerc0463502013-02-14 11:19:38 +01007942 if( ssl->session )
7943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 mbedtls_ssl_session_free( ssl->session );
7945 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007946 ssl->session = NULL;
7947 }
7948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007950 ssl->alpn_chosen = NULL;
7951#endif
7952
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007953#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007954#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007955 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007956#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007957 {
7958 mbedtls_free( ssl->cli_id );
7959 ssl->cli_id = NULL;
7960 ssl->cli_id_len = 0;
7961 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007962#endif
7963
Paul Bakker48916f92012-09-16 19:57:18 +00007964 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7965 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007966
7967 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007968}
7969
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007970/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007971 * Reset an initialized and used SSL context for re-use while retaining
7972 * all application-set variables, function pointers and data.
7973 */
7974int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7975{
7976 return( ssl_session_reset_int( ssl, 0 ) );
7977}
7978
7979/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007980 * SSL set accessors
7981 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007982void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007983{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007984 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007985}
7986
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007987void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007988{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007989 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007990}
7991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007993void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007995 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007996}
7997#endif
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008000void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008001{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008002 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008003}
8004#endif
8005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008007
Hanno Becker1841b0a2018-08-24 11:13:57 +01008008void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8009 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008010{
8011 ssl->disable_datagram_packing = !allow_packing;
8012}
8013
8014void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8015 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008016{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008017 conf->hs_timeout_min = min;
8018 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008019}
8020#endif
8021
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008022void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008023{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008024 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008025}
8026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008028void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008029 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008030 void *p_vrfy )
8031{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008032 conf->f_vrfy = f_vrfy;
8033 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008036
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008037void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008038 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008039 void *p_rng )
8040{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008041 conf->f_rng = f_rng;
8042 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008043}
8044
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008045void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008046 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008047 void *p_dbg )
8048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008049 conf->f_dbg = f_dbg;
8050 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008051}
8052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008054 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008055 mbedtls_ssl_send_t *f_send,
8056 mbedtls_ssl_recv_t *f_recv,
8057 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008058{
8059 ssl->p_bio = p_bio;
8060 ssl->f_send = f_send;
8061 ssl->f_recv = f_recv;
8062 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008063}
8064
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008065#if defined(MBEDTLS_SSL_PROTO_DTLS)
8066void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8067{
8068 ssl->mtu = mtu;
8069}
8070#endif
8071
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008072void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008073{
8074 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008075}
8076
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008077void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8078 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008079 mbedtls_ssl_set_timer_t *f_set_timer,
8080 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008081{
8082 ssl->p_timer = p_timer;
8083 ssl->f_set_timer = f_set_timer;
8084 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008085
8086 /* Make sure we start with no timer running */
8087 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008088}
8089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008091void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008092 void *p_cache,
8093 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8094 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008095{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008096 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008098 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008102#if defined(MBEDTLS_SSL_CLI_C)
8103int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008104{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008105 int ret;
8106
8107 if( ssl == NULL ||
8108 session == NULL ||
8109 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008110 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008112 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008113 }
8114
Hanno Becker58fccf22019-02-06 14:30:46 +00008115 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8116 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008117 return( ret );
8118
Paul Bakker0a597072012-09-25 21:55:46 +00008119 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008120
8121 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008124
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008125void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008126 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008128 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8129 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8130 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8131 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008132}
8133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008134void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008135 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008136 int major, int minor )
8137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008139 return;
8140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008142 return;
8143
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008144 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008145}
8146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008148void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008149 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008150{
8151 conf->cert_profile = profile;
8152}
8153
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008154/* Append a new keycert entry to a (possibly empty) list */
8155static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8156 mbedtls_x509_crt *cert,
8157 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008158{
niisato8ee24222018-06-25 19:05:48 +09008159 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008160
niisato8ee24222018-06-25 19:05:48 +09008161 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8162 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008163 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008164
niisato8ee24222018-06-25 19:05:48 +09008165 new_cert->cert = cert;
8166 new_cert->key = key;
8167 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008168
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008169 /* Update head is the list was null, else add to the end */
8170 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008171 {
niisato8ee24222018-06-25 19:05:48 +09008172 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008173 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008174 else
8175 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008176 mbedtls_ssl_key_cert *cur = *head;
8177 while( cur->next != NULL )
8178 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008179 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008180 }
8181
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008182 return( 0 );
8183}
8184
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008185int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008186 mbedtls_x509_crt *own_cert,
8187 mbedtls_pk_context *pk_key )
8188{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008189 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008190}
8191
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008192void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008193 mbedtls_x509_crt *ca_chain,
8194 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008195{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008196 conf->ca_chain = ca_chain;
8197 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008200
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008201#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8202int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8203 mbedtls_x509_crt *own_cert,
8204 mbedtls_pk_context *pk_key )
8205{
8206 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8207 own_cert, pk_key ) );
8208}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008209
8210void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8211 mbedtls_x509_crt *ca_chain,
8212 mbedtls_x509_crl *ca_crl )
8213{
8214 ssl->handshake->sni_ca_chain = ca_chain;
8215 ssl->handshake->sni_ca_crl = ca_crl;
8216}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008217
8218void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8219 int authmode )
8220{
8221 ssl->handshake->sni_authmode = authmode;
8222}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008223#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8224
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008225#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008226/*
8227 * Set EC J-PAKE password for current handshake
8228 */
8229int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8230 const unsigned char *pw,
8231 size_t pw_len )
8232{
8233 mbedtls_ecjpake_role role;
8234
Janos Follath8eb64132016-06-03 15:40:57 +01008235 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8237
8238 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8239 role = MBEDTLS_ECJPAKE_SERVER;
8240 else
8241 role = MBEDTLS_ECJPAKE_CLIENT;
8242
8243 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8244 role,
8245 MBEDTLS_MD_SHA256,
8246 MBEDTLS_ECP_DP_SECP256R1,
8247 pw, pw_len ) );
8248}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008249#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008252int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008253 const unsigned char *psk, size_t psk_len,
8254 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008255{
Paul Bakker6db455e2013-09-18 17:29:31 +02008256 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008259 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008261
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008262 /* Identity len will be encoded on two bytes */
8263 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008264 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008265 {
8266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8267 }
8268
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008269 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008270 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008271 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008272
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008273 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008274 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008275 conf->psk_len = 0;
8276 }
8277 if( conf->psk_identity != NULL )
8278 {
8279 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008280 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008281 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008282 }
8283
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008284 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8285 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008286 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008287 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008288 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008289 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008290 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008291 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008292 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008293
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008294 conf->psk_len = psk_len;
8295 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008296
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008297 memcpy( conf->psk, psk, conf->psk_len );
8298 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008299
8300 return( 0 );
8301}
8302
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008303int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8304 const unsigned char *psk, size_t psk_len )
8305{
8306 if( psk == NULL || ssl->handshake == NULL )
8307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8308
8309 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8310 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8311
8312 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008314 mbedtls_platform_zeroize( ssl->handshake->psk,
8315 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008316 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008317 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008318 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008319
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008320 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008321 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008322
8323 ssl->handshake->psk_len = psk_len;
8324 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8325
8326 return( 0 );
8327}
8328
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008329void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008331 size_t),
8332 void *p_psk )
8333{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008334 conf->f_psk = f_psk;
8335 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008336}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008338
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008339#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008340
8341#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008342int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008343{
8344 int ret;
8345
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008346 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8347 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8348 {
8349 mbedtls_mpi_free( &conf->dhm_P );
8350 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008351 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008353
8354 return( 0 );
8355}
Hanno Becker470a8c42017-10-04 15:28:46 +01008356#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008357
Hanno Beckera90658f2017-10-04 15:29:08 +01008358int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8359 const unsigned char *dhm_P, size_t P_len,
8360 const unsigned char *dhm_G, size_t G_len )
8361{
8362 int ret;
8363
8364 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8365 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8366 {
8367 mbedtls_mpi_free( &conf->dhm_P );
8368 mbedtls_mpi_free( &conf->dhm_G );
8369 return( ret );
8370 }
8371
8372 return( 0 );
8373}
Paul Bakker5121ce52009-01-03 21:22:43 +00008374
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008375int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008376{
8377 int ret;
8378
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008379 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8380 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8381 {
8382 mbedtls_mpi_free( &conf->dhm_P );
8383 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008384 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008385 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008386
8387 return( 0 );
8388}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008389#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008390
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008391#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8392/*
8393 * Set the minimum length for Diffie-Hellman parameters
8394 */
8395void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8396 unsigned int bitlen )
8397{
8398 conf->dhm_min_bitlen = bitlen;
8399}
8400#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8401
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008402#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008403/*
8404 * Set allowed/preferred hashes for handshake signatures
8405 */
8406void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8407 const int *hashes )
8408{
8409 conf->sig_hashes = hashes;
8410}
Hanno Becker947194e2017-04-07 13:25:49 +01008411#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008412
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008413#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008414/*
8415 * Set the allowed elliptic curves
8416 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008417void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008418 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008419{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008420 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008421}
Hanno Becker947194e2017-04-07 13:25:49 +01008422#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008423
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008424#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008425int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008426{
Hanno Becker947194e2017-04-07 13:25:49 +01008427 /* Initialize to suppress unnecessary compiler warning */
8428 size_t hostname_len = 0;
8429
8430 /* Check if new hostname is valid before
8431 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008432 if( hostname != NULL )
8433 {
8434 hostname_len = strlen( hostname );
8435
8436 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8438 }
8439
8440 /* Now it's clear that we will overwrite the old hostname,
8441 * so we can free it safely */
8442
8443 if( ssl->hostname != NULL )
8444 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008445 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008446 mbedtls_free( ssl->hostname );
8447 }
8448
8449 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008450
Paul Bakker5121ce52009-01-03 21:22:43 +00008451 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008452 {
8453 ssl->hostname = NULL;
8454 }
8455 else
8456 {
8457 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008458 if( ssl->hostname == NULL )
8459 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008460
Hanno Becker947194e2017-04-07 13:25:49 +01008461 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008462
Hanno Becker947194e2017-04-07 13:25:49 +01008463 ssl->hostname[hostname_len] = '\0';
8464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008465
8466 return( 0 );
8467}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008468#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008469
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008470#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008471void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008472 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008473 const unsigned char *, size_t),
8474 void *p_sni )
8475{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008476 conf->f_sni = f_sni;
8477 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008479#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008481#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008482int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008483{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008484 size_t cur_len, tot_len;
8485 const char **p;
8486
8487 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008488 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8489 * MUST NOT be truncated."
8490 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008491 */
8492 tot_len = 0;
8493 for( p = protos; *p != NULL; p++ )
8494 {
8495 cur_len = strlen( *p );
8496 tot_len += cur_len;
8497
8498 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008500 }
8501
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008503
8504 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008505}
8506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008508{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008509 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008512
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008513void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 conf->max_major_ver = major;
8516 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008517}
8518
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008519void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008520{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008521 conf->min_major_ver = major;
8522 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008523}
8524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008526void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008527{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008528 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008529}
8530#endif
8531
Janos Follath088ce432017-04-10 12:42:31 +01008532#if defined(MBEDTLS_SSL_SRV_C)
8533void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8534 char cert_req_ca_list )
8535{
8536 conf->cert_req_ca_list = cert_req_ca_list;
8537}
8538#endif
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008541void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008543 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008544}
8545#endif
8546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008547#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008548void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008549{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008550 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008551}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008552
8553void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008554 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008555{
8556 conf->enforce_extended_master_secret = ems_enf;
8557}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008558#endif
8559
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008560#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008561void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008562{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008563 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008564}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008565#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008567#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008568int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008569{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008571 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008574 }
8575
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008576 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008577
8578 return( 0 );
8579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008580#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008583void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008585 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008589#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008591{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008592 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008593}
8594#endif
8595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008596void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008598 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008599}
8600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008602void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008604 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008605}
8606
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008607void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008609 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008610}
8611
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008612void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008613 const unsigned char period[8] )
8614{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008615 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008616}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008620#if defined(MBEDTLS_SSL_CLI_C)
8621void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008622{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008623 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008624}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008625#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008626
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008627#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008628void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8629 mbedtls_ssl_ticket_write_t *f_ticket_write,
8630 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8631 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008632{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008633 conf->f_ticket_write = f_ticket_write;
8634 conf->f_ticket_parse = f_ticket_parse;
8635 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008636}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008639
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008640#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8641void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8642 mbedtls_ssl_export_keys_t *f_export_keys,
8643 void *p_export_keys )
8644{
8645 conf->f_export_keys = f_export_keys;
8646 conf->p_export_keys = p_export_keys;
8647}
8648#endif
8649
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008650#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008651void mbedtls_ssl_conf_async_private_cb(
8652 mbedtls_ssl_config *conf,
8653 mbedtls_ssl_async_sign_t *f_async_sign,
8654 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8655 mbedtls_ssl_async_resume_t *f_async_resume,
8656 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008657 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008658{
8659 conf->f_async_sign_start = f_async_sign;
8660 conf->f_async_decrypt_start = f_async_decrypt;
8661 conf->f_async_resume = f_async_resume;
8662 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008663 conf->p_async_config_data = async_config_data;
8664}
8665
Gilles Peskine8f97af72018-04-26 11:46:10 +02008666void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8667{
8668 return( conf->p_async_config_data );
8669}
8670
Gilles Peskine1febfef2018-04-30 11:54:39 +02008671void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008672{
8673 if( ssl->handshake == NULL )
8674 return( NULL );
8675 else
8676 return( ssl->handshake->user_async_ctx );
8677}
8678
Gilles Peskine1febfef2018-04-30 11:54:39 +02008679void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008680 void *ctx )
8681{
8682 if( ssl->handshake != NULL )
8683 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008684}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008685#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008686
Paul Bakker5121ce52009-01-03 21:22:43 +00008687/*
8688 * SSL get accessors
8689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008690size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008691{
8692 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8693}
8694
Hanno Becker8b170a02017-10-10 11:51:19 +01008695int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8696{
8697 /*
8698 * Case A: We're currently holding back
8699 * a message for further processing.
8700 */
8701
8702 if( ssl->keep_current_message == 1 )
8703 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008704 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008705 return( 1 );
8706 }
8707
8708 /*
8709 * Case B: Further records are pending in the current datagram.
8710 */
8711
8712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008714 ssl->in_left > ssl->next_record_offset )
8715 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008717 return( 1 );
8718 }
8719#endif /* MBEDTLS_SSL_PROTO_DTLS */
8720
8721 /*
8722 * Case C: A handshake message is being processed.
8723 */
8724
Hanno Becker8b170a02017-10-10 11:51:19 +01008725 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8726 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008727 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008728 return( 1 );
8729 }
8730
8731 /*
8732 * Case D: An application data message is being processed
8733 */
8734 if( ssl->in_offt != NULL )
8735 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008737 return( 1 );
8738 }
8739
8740 /*
8741 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008742 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008743 * we implement support for multiple alerts in single records.
8744 */
8745
8746 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8747 return( 0 );
8748}
8749
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008750uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008751{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008752 if( ssl->session != NULL )
8753 return( ssl->session->verify_result );
8754
8755 if( ssl->session_negotiate != NULL )
8756 return( ssl->session_negotiate->verify_result );
8757
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008758 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008759}
8760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008761const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008762{
Paul Bakker926c8e42013-03-06 10:23:34 +01008763 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008764 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008767}
8768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008769const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008770{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008771#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008772 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008773 {
8774 switch( ssl->minor_ver )
8775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008776 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008777 return( "DTLSv1.0" );
8778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008779 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008780 return( "DTLSv1.2" );
8781
8782 default:
8783 return( "unknown (DTLS)" );
8784 }
8785 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008786 MBEDTLS_SSL_TRANSPORT_ELSE
8787#endif /* MBEDTLS_SSL_PROTO_DTLS */
8788#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008789 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008790 switch( ssl->minor_ver )
8791 {
8792 case MBEDTLS_SSL_MINOR_VERSION_0:
8793 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008794
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008795 case MBEDTLS_SSL_MINOR_VERSION_1:
8796 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008797
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008798 case MBEDTLS_SSL_MINOR_VERSION_2:
8799 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008800
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008801 case MBEDTLS_SSL_MINOR_VERSION_3:
8802 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008803
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008804 default:
8805 return( "unknown" );
8806 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008807 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008808#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008809}
8810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008812{
Hanno Becker3136ede2018-08-17 15:28:19 +01008813 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008815 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008816
Hanno Becker43395762019-05-03 14:46:38 +01008817 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8818
Hanno Becker78640902018-08-13 16:35:15 +01008819 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008820 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822#if defined(MBEDTLS_ZLIB_SUPPORT)
8823 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8824 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008825#endif
8826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008827 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829 case MBEDTLS_MODE_GCM:
8830 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008831 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008833 transform_expansion = transform->minlen;
8834 break;
8835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008837
8838 block_size = mbedtls_cipher_get_block_size(
8839 &transform->cipher_ctx_enc );
8840
Hanno Becker3136ede2018-08-17 15:28:19 +01008841 /* Expansion due to the addition of the MAC. */
8842 transform_expansion += transform->maclen;
8843
8844 /* Expansion due to the addition of CBC padding;
8845 * Theoretically up to 256 bytes, but we never use
8846 * more than the block size of the underlying cipher. */
8847 transform_expansion += block_size;
8848
8849 /* For TLS 1.1 or higher, an explicit IV is added
8850 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008851#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8852 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008853 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008854#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008855
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008856 break;
8857
8858 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008861 }
8862
Hanno Beckera5a2b082019-05-15 14:03:01 +01008863#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008864 if( transform->out_cid_len != 0 )
8865 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008866#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008867
Hanno Becker43395762019-05-03 14:46:38 +01008868 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008869}
8870
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008871#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8872size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8873{
8874 size_t max_len;
8875
8876 /*
8877 * Assume mfl_code is correct since it was checked when set
8878 */
Angus Grattond8213d02016-05-25 20:56:48 +10008879 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008880
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008881 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008882 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008883 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008884 {
Angus Grattond8213d02016-05-25 20:56:48 +10008885 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008886 }
8887
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008888 /* During a handshake, use the value being negotiated */
8889 if( ssl->session_negotiate != NULL &&
8890 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8891 {
8892 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8893 }
8894
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008895 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008896}
8897#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8898
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008899#if defined(MBEDTLS_SSL_PROTO_DTLS)
8900static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8901{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008902 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8903 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8904 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8905 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8906 return ( 0 );
8907
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008908 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8909 return( ssl->mtu );
8910
8911 if( ssl->mtu == 0 )
8912 return( ssl->handshake->mtu );
8913
8914 return( ssl->mtu < ssl->handshake->mtu ?
8915 ssl->mtu : ssl->handshake->mtu );
8916}
8917#endif /* MBEDTLS_SSL_PROTO_DTLS */
8918
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008919int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8920{
8921 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8922
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008923#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8924 !defined(MBEDTLS_SSL_PROTO_DTLS)
8925 (void) ssl;
8926#endif
8927
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008928#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8929 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8930
8931 if( max_len > mfl )
8932 max_len = mfl;
8933#endif
8934
8935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008936 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008937 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008938 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008939 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8940 const size_t overhead = (size_t) ret;
8941
8942 if( ret < 0 )
8943 return( ret );
8944
8945 if( mtu <= overhead )
8946 {
8947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8949 }
8950
8951 if( max_len > mtu - overhead )
8952 max_len = mtu - overhead;
8953 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008955
Hanno Becker0defedb2018-08-10 12:35:02 +01008956#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8957 !defined(MBEDTLS_SSL_PROTO_DTLS)
8958 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008959#endif
8960
8961 return( (int) max_len );
8962}
8963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008964#if defined(MBEDTLS_X509_CRT_PARSE_C)
8965const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008966{
8967 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008968 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008969
Hanno Beckerbfab9df2019-02-07 13:18:46 +00008970#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008971 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00008972#else
8973 return( NULL );
8974#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008975}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008976#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008979int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8980 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008981{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008982 if( ssl == NULL ||
8983 dst == NULL ||
8984 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008985 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008987 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008988 }
8989
Hanno Becker58fccf22019-02-06 14:30:46 +00008990 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008993
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008994const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8995{
8996 if( ssl == NULL )
8997 return( NULL );
8998
8999 return( ssl->session );
9000}
9001
Paul Bakker5121ce52009-01-03 21:22:43 +00009002/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009003 * Define ticket header determining Mbed TLS version
9004 * and structure of the ticket.
9005 */
9006
Hanno Becker41527622019-05-16 12:50:45 +01009007/*
Hanno Becker26829e92019-05-28 14:30:45 +01009008 * Define bitflag determining compile-time settings influencing
9009 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009010 */
9011
Hanno Becker26829e92019-05-28 14:30:45 +01009012#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009013#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009014#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009015#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009016#endif /* MBEDTLS_HAVE_TIME */
9017
9018#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009019#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009020#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009021#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009022#endif /* MBEDTLS_X509_CRT_PARSE_C */
9023
9024#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009025#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009026#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009027#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009028#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9029
9030#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009031#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009032#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009033#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009034#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9035
9036#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009037#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009038#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009039#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009040#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9041
9042#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009043#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009044#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009045#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009046#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9047
Hanno Becker41527622019-05-16 12:50:45 +01009048#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9049#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9050#else
9051#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9052#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9053
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009054#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9055#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9056#else
9057#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9058#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9059
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009060#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9061#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9062#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9063#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9064#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9065#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9066#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009067#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009068
Hanno Becker26829e92019-05-28 14:30:45 +01009069#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009070 ( (uint16_t) ( \
9071 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9072 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9073 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9074 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9075 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9076 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009077 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9078 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009079
Hanno Becker557fe9f2019-05-16 12:41:07 +01009080static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009081 MBEDTLS_VERSION_MAJOR,
9082 MBEDTLS_VERSION_MINOR,
9083 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009084 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9085 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009086};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009087
9088/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009089 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009090 * (in the presentation language of TLS, RFC 8446 section 3)
9091 *
Hanno Becker26829e92019-05-28 14:30:45 +01009092 * opaque mbedtls_version[3]; // major, minor, patch
9093 * opaque session_format[2]; // version-specific 16-bit field determining
9094 * // the format of the remaining
9095 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009096 *
9097 * Note: When updating the format, remember to keep
9098 * these version+format bytes.
9099 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009100 * // In this version, `session_format` determines
9101 * // the setting of those compile-time
9102 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009103 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009104 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009105 * uint8 ciphersuite[2]; // defined by the standard
9106 * uint8 compression; // 0 or 1
9107 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009108 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009109 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009110 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01009111 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009112 * uint8_t peer_cert_digest_type;
9113 * opaque peer_cert_digest<0..2^8-1>;
Hanno Becker26829e92019-05-28 14:30:45 +01009114 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009115 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009116 * uint8 mfl_code; // up to 255 according to standard
9117 * uint8 trunc_hmac; // 0 or 1
9118 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009119 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009120 * The order is the same as in the definition of the structure, except
9121 * verify_result is put before peer_cert so that all mandatory fields come
9122 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009123 */
9124int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9125 unsigned char *buf,
9126 size_t buf_len,
9127 size_t *olen )
9128{
9129 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009130 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009131#if defined(MBEDTLS_HAVE_TIME)
9132 uint64_t start;
9133#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009134#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009135#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009136 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009137#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009138#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009139
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009140 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009141 * Add version identifier
9142 */
9143
9144 used += sizeof( ssl_serialized_session_header );
9145
9146 if( used <= buf_len )
9147 {
9148 memcpy( p, ssl_serialized_session_header,
9149 sizeof( ssl_serialized_session_header ) );
9150 p += sizeof( ssl_serialized_session_header );
9151 }
9152
9153 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009154 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009155 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009156#if defined(MBEDTLS_HAVE_TIME)
9157 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009158
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009159 if( used <= buf_len )
9160 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009161 start = (uint64_t) session->start;
9162
9163 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9164 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9165 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9166 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9167 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9168 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9169 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9170 *p++ = (unsigned char)( ( start ) & 0xFF );
9171 }
9172#endif /* MBEDTLS_HAVE_TIME */
9173
9174 /*
9175 * Basic mandatory fields
9176 */
9177 used += 2 /* ciphersuite */
9178 + 1 /* compression */
9179 + 1 /* id_len */
9180 + sizeof( session->id )
9181 + sizeof( session->master )
9182 + 4; /* verify_result */
9183
9184 if( used <= buf_len )
9185 {
9186 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9187 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9188
9189 *p++ = (unsigned char)( session->compression & 0xFF );
9190
9191 *p++ = (unsigned char)( session->id_len & 0xFF );
9192 memcpy( p, session->id, 32 );
9193 p += 32;
9194
9195 memcpy( p, session->master, 48 );
9196 p += 48;
9197
9198 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9199 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9200 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9201 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009202 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009203
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009204 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009205 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009206 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009207#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009208#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009209 if( session->peer_cert == NULL )
9210 cert_len = 0;
9211 else
9212 cert_len = session->peer_cert->raw.len;
9213
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009214 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009215
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009216 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009217 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009218 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9219 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9220 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9221
9222 if( session->peer_cert != NULL )
9223 {
9224 memcpy( p, session->peer_cert->raw.p, cert_len );
9225 p += cert_len;
9226 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009227 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009228
Hanno Becker2e6d3472019-02-06 15:40:27 +00009229#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009230 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009231 if( session->peer_cert_digest != NULL )
9232 {
9233 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9234 if( used <= buf_len )
9235 {
9236 *p++ = (unsigned char) session->peer_cert_digest_type;
9237 *p++ = (unsigned char) session->peer_cert_digest_len;
9238 memcpy( p, session->peer_cert_digest,
9239 session->peer_cert_digest_len );
9240 p += session->peer_cert_digest_len;
9241 }
9242 }
9243 else
9244 {
9245 used += 2;
9246 if( used <= buf_len )
9247 {
9248 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9249 *p++ = 0;
9250 }
9251 }
9252#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009253#endif /* MBEDTLS_X509_CRT_PARSE_C */
9254
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009255 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009256 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009257 */
9258#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009259 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009260
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009261 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009262 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009263 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9264 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9265 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9266
9267 if( session->ticket != NULL )
9268 {
9269 memcpy( p, session->ticket, session->ticket_len );
9270 p += session->ticket_len;
9271 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009272
9273 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9274 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9275 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9276 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009277 }
9278#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9279
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009280 /*
9281 * Misc extension-related info
9282 */
9283#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9284 used += 1;
9285
9286 if( used <= buf_len )
9287 *p++ = session->mfl_code;
9288#endif
9289
9290#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9291 used += 1;
9292
9293 if( used <= buf_len )
9294 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9295#endif
9296
9297#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9298 used += 1;
9299
9300 if( used <= buf_len )
9301 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9302#endif
9303
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009304 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009305 *olen = used;
9306
9307 if( used > buf_len )
9308 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009309
9310 return( 0 );
9311}
9312
9313/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009314 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009315 *
9316 * This internal version is wrapped by a public function that cleans up in
9317 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009318 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009319static int ssl_session_load( mbedtls_ssl_session *session,
9320 const unsigned char *buf,
9321 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009322{
9323 const unsigned char *p = buf;
9324 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009325#if defined(MBEDTLS_HAVE_TIME)
9326 uint64_t start;
9327#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009328#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009329#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009330 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009331#endif
9332#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009333
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009334 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009335 * Check version identifier
9336 */
9337
9338 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009340
9341 if( memcmp( p, ssl_serialized_session_header,
9342 sizeof( ssl_serialized_session_header ) ) != 0 )
9343 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009344 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009345 }
9346 p += sizeof( ssl_serialized_session_header );
9347
9348 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009349 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009350 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009351#if defined(MBEDTLS_HAVE_TIME)
9352 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9354
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009355 start = ( (uint64_t) p[0] << 56 ) |
9356 ( (uint64_t) p[1] << 48 ) |
9357 ( (uint64_t) p[2] << 40 ) |
9358 ( (uint64_t) p[3] << 32 ) |
9359 ( (uint64_t) p[4] << 24 ) |
9360 ( (uint64_t) p[5] << 16 ) |
9361 ( (uint64_t) p[6] << 8 ) |
9362 ( (uint64_t) p[7] );
9363 p += 8;
9364
9365 session->start = (time_t) start;
9366#endif /* MBEDTLS_HAVE_TIME */
9367
9368 /*
9369 * Basic mandatory fields
9370 */
9371 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9373
9374 session->ciphersuite = ( p[0] << 8 ) | p[1];
9375 p += 2;
9376
9377 session->compression = *p++;
9378
9379 session->id_len = *p++;
9380 memcpy( session->id, p, 32 );
9381 p += 32;
9382
9383 memcpy( session->master, p, 48 );
9384 p += 48;
9385
9386 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9387 ( (uint32_t) p[1] << 16 ) |
9388 ( (uint32_t) p[2] << 8 ) |
9389 ( (uint32_t) p[3] );
9390 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009391
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009392 /* Immediately clear invalid pointer values that have been read, in case
9393 * we exit early before we replaced them with valid ones. */
9394#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009395#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009396 session->peer_cert = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009397#else
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009398 session->peer_cert_digest = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009399#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009400#endif
9401#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9402 session->ticket = NULL;
9403#endif
9404
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009405 /*
9406 * Peer certificate
9407 */
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 if( 3 > (size_t)( end - p ) )
9411 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9412
9413 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9414 p += 3;
9415
9416 if( cert_len == 0 )
9417 {
9418 session->peer_cert = NULL;
9419 }
9420 else
9421 {
9422 int ret;
9423
9424 if( cert_len > (size_t)( end - p ) )
9425 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9426
9427 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9428
9429 if( session->peer_cert == NULL )
9430 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9431
9432 mbedtls_x509_crt_init( session->peer_cert );
9433
9434 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9435 p, cert_len ) ) != 0 )
9436 {
9437 mbedtls_x509_crt_free( session->peer_cert );
9438 mbedtls_free( session->peer_cert );
9439 session->peer_cert = NULL;
9440 return( ret );
9441 }
9442
9443 p += cert_len;
9444 }
Hanno Becker2e6d3472019-02-06 15:40:27 +00009445#else /* defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009446 /* Deserialize CRT digest from the end of the ticket. */
9447 if( 2 > (size_t)( end - p ) )
9448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9449
9450 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9451 session->peer_cert_digest_len = (size_t) *p++;
9452
9453 if( session->peer_cert_digest_len != 0 )
9454 {
9455 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9456 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9457
9458 session->peer_cert_digest =
9459 mbedtls_calloc( 1, session->peer_cert_digest_len );
9460 if( session->peer_cert_digest == NULL )
9461 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9462
9463 memcpy( session->peer_cert_digest, p,
9464 session->peer_cert_digest_len );
9465 p += session->peer_cert_digest_len;
9466 }
9467#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009468#endif /* MBEDTLS_X509_CRT_PARSE_C */
9469
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009470 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009471 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009472 */
9473#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9474 if( 3 > (size_t)( end - p ) )
9475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9476
9477 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9478 p += 3;
9479
9480 if( session->ticket_len != 0 )
9481 {
9482 if( session->ticket_len > (size_t)( end - p ) )
9483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9484
9485 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9486 if( session->ticket == NULL )
9487 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9488
9489 memcpy( session->ticket, p, session->ticket_len );
9490 p += session->ticket_len;
9491 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009492
9493 if( 4 > (size_t)( end - p ) )
9494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9495
9496 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9497 ( (uint32_t) p[1] << 16 ) |
9498 ( (uint32_t) p[2] << 8 ) |
9499 ( (uint32_t) p[3] );
9500 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009501#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9502
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009503 /*
9504 * Misc extension-related info
9505 */
9506#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9507 if( 1 > (size_t)( end - p ) )
9508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9509
9510 session->mfl_code = *p++;
9511#endif
9512
9513#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9514 if( 1 > (size_t)( end - p ) )
9515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9516
9517 session->trunc_hmac = *p++;
9518#endif
9519
9520#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9521 if( 1 > (size_t)( end - p ) )
9522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9523
9524 session->encrypt_then_mac = *p++;
9525#endif
9526
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009527 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009528 if( p != end )
9529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9530
9531 return( 0 );
9532}
9533
9534/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009535 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009536 */
9537int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9538 const unsigned char *buf,
9539 size_t len )
9540{
9541 int ret = ssl_session_load( session, buf, len );
9542
9543 if( ret != 0 )
9544 mbedtls_ssl_session_free( session );
9545
9546 return( ret );
9547}
9548
9549/*
Paul Bakker1961b702013-01-25 14:49:24 +01009550 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009555
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009556 if( ssl == NULL || ssl->conf == NULL )
9557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009564 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009566#endif
9567
Paul Bakker1961b702013-01-25 14:49:24 +01009568 return( ret );
9569}
9570
9571/*
9572 * Perform the SSL handshake
9573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009575{
9576 int ret = 0;
9577
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009578 if( ssl == NULL || ssl->conf == NULL )
9579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009586
9587 if( ret != 0 )
9588 break;
9589 }
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009592
9593 return( ret );
9594}
9595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#if defined(MBEDTLS_SSL_RENEGOTIATION)
9597#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009598/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009599 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009602{
9603 int ret;
9604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009606
9607 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9609 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009610
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009611 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009612 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009614 return( ret );
9615 }
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009618
9619 return( 0 );
9620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009622
9623/*
9624 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 * - any side: calling mbedtls_ssl_renegotiate(),
9626 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9627 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009628 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009629 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009633{
9634 int ret;
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009637
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009638 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9639 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009640
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009641 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9642 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009644 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009646 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009648 ssl->handshake->out_msg_seq = 1;
9649 else
9650 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009651 }
9652#endif
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9655 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009660 return( ret );
9661 }
9662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009664
9665 return( 0 );
9666}
9667
9668/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009669 * Renegotiate current connection on client,
9670 * or request renegotiation on server
9671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009676 if( ssl == NULL || ssl->conf == NULL )
9677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009687
9688 /* Did we already try/start sending HelloRequest? */
9689 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009691
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009692 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009697 /*
9698 * On client, either start the renegotiation process or,
9699 * if already in progress, continue the handshake
9700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9704 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009705
9706 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009709 return( ret );
9710 }
9711 }
9712 else
9713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009717 return( ret );
9718 }
9719 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009721
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009722 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009723}
9724
9725/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009726 * Check record counters and renegotiate if they're above the limit.
9727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009729{
Andres AG2196c7f2016-12-15 17:01:16 +00009730 size_t ep_len = ssl_ep_len( ssl );
9731 int in_ctr_cmp;
9732 int out_ctr_cmp;
9733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9735 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009736 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009737 {
9738 return( 0 );
9739 }
9740
Andres AG2196c7f2016-12-15 17:01:16 +00009741 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9742 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009743 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009744 ssl->conf->renego_period + ep_len, 8 - ep_len );
9745
9746 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009747 {
9748 return( 0 );
9749 }
9750
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009755
9756/*
9757 * Receive application data decrypted from the SSL layer
9758 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009759int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009760{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009761 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009762 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009763
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009764 if( ssl == NULL || ssl->conf == NULL )
9765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009770 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009773 return( ret );
9774
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009775 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009777 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009778 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009779 return( ret );
9780 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009781 }
9782#endif
9783
Hanno Becker4a810fb2017-05-24 16:27:30 +01009784 /*
9785 * Check if renegotiation is necessary and/or handshake is
9786 * in process. If yes, perform/continue, and fall through
9787 * if an unexpected packet is received while the client
9788 * is waiting for the ServerHello.
9789 *
9790 * (There is no equivalent to the last condition on
9791 * the server-side as it is not treated as within
9792 * a handshake while waiting for the ClientHello
9793 * after a renegotiation request.)
9794 */
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009797 ret = ssl_check_ctr_renegotiate( ssl );
9798 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9799 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009802 return( ret );
9803 }
9804#endif
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009809 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9810 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009813 return( ret );
9814 }
9815 }
9816
Hanno Beckere41158b2017-10-23 13:30:32 +01009817 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009818 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009819 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009820 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009821 if( ssl->f_get_timer != NULL &&
9822 ssl->f_get_timer( ssl->p_timer ) == -1 )
9823 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009824 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009825 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009826
Hanno Becker327c93b2018-08-15 13:56:18 +01009827 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009828 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009829 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9830 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009831
Hanno Becker4a810fb2017-05-24 16:27:30 +01009832 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9833 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009834 }
9835
9836 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 {
9839 /*
9840 * OpenSSL sends empty messages to randomize the IV
9841 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009842 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009845 return( 0 );
9846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009848 return( ret );
9849 }
9850 }
9851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009855
Hanno Becker4a810fb2017-05-24 16:27:30 +01009856 /*
9857 * - For client-side, expect SERVER_HELLO_REQUEST.
9858 * - For server-side, expect CLIENT_HELLO.
9859 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9860 */
9861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009863 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009865 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009868
9869 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009871 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009872 {
9873 continue;
9874 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009875 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009876#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009877#if defined(MBEDTLS_SSL_PROTO_TLS)
9878 {
9879 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9880 }
9881#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009882 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009883#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009884
Hanno Becker4a810fb2017-05-24 16:27:30 +01009885#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009886 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009890
9891 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009893 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009894 {
9895 continue;
9896 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009897 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009898#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009899#if defined(MBEDTLS_SSL_PROTO_TLS)
9900 {
9901 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9902 }
9903#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009904 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009905#endif /* MBEDTLS_SSL_SRV_C */
9906
Hanno Becker21df7f92017-10-17 11:03:26 +01009907#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009908 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009909 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9910 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9911 ssl->conf->allow_legacy_renegotiation ==
9912 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9913 {
9914 /*
9915 * Accept renegotiation request
9916 */
Paul Bakker48916f92012-09-16 19:57:18 +00009917
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009918 /* DTLS clients need to know renego is server-initiated */
9919#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009920 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009921 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9922 {
9923 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9924 }
9925#endif
9926 ret = ssl_start_renegotiation( ssl );
9927 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9928 ret != 0 )
9929 {
9930 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9931 return( ret );
9932 }
9933 }
9934 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009935#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009936 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009937 /*
9938 * Refuse renegotiation
9939 */
9940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943#if defined(MBEDTLS_SSL_PROTO_SSL3)
9944 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009945 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009946 /* SSLv3 does not have a "no_renegotiation" warning, so
9947 we send a fatal alert and abort the connection. */
9948 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9949 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9950 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009951 }
9952 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9956 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9959 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9960 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009961 {
9962 return( ret );
9963 }
Paul Bakker48916f92012-09-16 19:57:18 +00009964 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009965 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9967 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009971 }
Paul Bakker48916f92012-09-16 19:57:18 +00009972 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009973
Hanno Becker90333da2017-10-10 11:27:13 +01009974 /* At this point, we don't know whether the renegotiation has been
9975 * completed or not. The cases to consider are the following:
9976 * 1) The renegotiation is complete. In this case, no new record
9977 * has been read yet.
9978 * 2) The renegotiation is incomplete because the client received
9979 * an application data record while awaiting the ServerHello.
9980 * 3) The renegotiation is incomplete because the client received
9981 * a non-handshake, non-application data message while awaiting
9982 * the ServerHello.
9983 * In each of these case, looping will be the proper action:
9984 * - For 1), the next iteration will read a new record and check
9985 * if it's application data.
9986 * - For 2), the loop condition isn't satisfied as application data
9987 * is present, hence continue is the same as break
9988 * - For 3), the loop condition is satisfied and read_record
9989 * will re-deliver the message that was held back by the client
9990 * when expecting the ServerHello.
9991 */
9992 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009993 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009994#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009996 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009997 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009998 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009999 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010002 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010004 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010005 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10010 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010013 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010014 }
10015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010016 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10019 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010020 }
10021
10022 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010023
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010024 /* We're going to return something now, cancel timer,
10025 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010027 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010028
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010030 /* If we requested renego but received AppData, resend HelloRequest.
10031 * Do it now, after setting in_offt, to avoid taking this branch
10032 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010036 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010037 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010040 return( ret );
10041 }
10042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010044#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 }
10046
10047 n = ( len < ssl->in_msglen )
10048 ? len : ssl->in_msglen;
10049
10050 memcpy( buf, ssl->in_offt, n );
10051 ssl->in_msglen -= n;
10052
10053 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010054 {
10055 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010056 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010057 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010058 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010059 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010060 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010061 /* more data available */
10062 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010063 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010066
Paul Bakker23986e52011-04-24 08:57:21 +000010067 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010068}
10069
10070/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010071 * Send application data to be encrypted by the SSL layer, taking care of max
10072 * fragment length and buffer size.
10073 *
10074 * According to RFC 5246 Section 6.2.1:
10075 *
10076 * Zero-length fragments of Application data MAY be sent as they are
10077 * potentially useful as a traffic analysis countermeasure.
10078 *
10079 * Therefore, it is possible that the input message length is 0 and the
10080 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010082static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010083 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010084{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010085 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10086 const size_t max_len = (size_t) ret;
10087
10088 if( ret < 0 )
10089 {
10090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10091 return( ret );
10092 }
10093
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010094 if( len > max_len )
10095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010097 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010100 "maximum fragment length: %d > %d",
10101 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010103 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010104 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010105#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010106#if defined(MBEDTLS_SSL_PROTO_TLS)
10107 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010108 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010109 }
10110#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010111 }
Paul Bakker887bd502011-06-08 13:10:54 +000010112
Paul Bakker5121ce52009-01-03 21:22:43 +000010113 if( ssl->out_left != 0 )
10114 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010115 /*
10116 * The user has previously tried to send the data and
10117 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10118 * written. In this case, we expect the high-level write function
10119 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010124 return( ret );
10125 }
10126 }
Paul Bakker887bd502011-06-08 13:10:54 +000010127 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010128 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010129 /*
10130 * The user is trying to send a message the first time, so we need to
10131 * copy the data into the internal buffers and setup the data structure
10132 * to keep track of partial writes
10133 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010134 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010136 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010137
Hanno Becker67bc7c32018-08-06 11:33:50 +010010138 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010141 return( ret );
10142 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010143 }
10144
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010145 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010146}
10147
10148/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010149 * Write application data, doing 1/n-1 splitting if necessary.
10150 *
10151 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010152 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010153 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010156static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010157 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010158{
10159 int ret;
10160
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010161 if( ssl->conf->cbc_record_splitting ==
10162 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010163 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10165 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10166 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010167 {
10168 return( ssl_write_real( ssl, buf, len ) );
10169 }
10170
10171 if( ssl->split_done == 0 )
10172 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010173 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010174 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010175 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010176 }
10177
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010178 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10179 return( ret );
10180 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010181
10182 return( ret + 1 );
10183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010185
10186/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010187 * Write application data (public-facing wrapper)
10188 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010189int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010190{
10191 int ret;
10192
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010194
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010195 if( ssl == NULL || ssl->conf == NULL )
10196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10197
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010198#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010199 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10200 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010201 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010202 return( ret );
10203 }
10204#endif
10205
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010206 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010207 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010208 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010209 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010210 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010211 return( ret );
10212 }
10213 }
10214
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010215#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010216 ret = ssl_write_split( ssl, buf, len );
10217#else
10218 ret = ssl_write_real( ssl, buf, len );
10219#endif
10220
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010222
10223 return( ret );
10224}
10225
10226/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010227 * Notify the peer that the connection is being closed
10228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010230{
10231 int ret;
10232
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010233 if( ssl == NULL || ssl->conf == NULL )
10234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010237
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010238 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10244 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10245 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010248 return( ret );
10249 }
10250 }
10251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010253
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010254 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010255}
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010258{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010259 if( transform == NULL )
10260 return;
10261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010262#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010263 deflateEnd( &transform->ctx_deflate );
10264 inflateEnd( &transform->ctx_inflate );
10265#endif
10266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10268 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010269
Hanno Becker92231322018-01-03 15:32:51 +000010270#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 mbedtls_md_free( &transform->md_ctx_enc );
10272 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010273#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010274
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010275 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010276}
10277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010278#if defined(MBEDTLS_X509_CRT_PARSE_C)
10279static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010281 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010282
10283 while( cur != NULL )
10284 {
10285 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010287 cur = next;
10288 }
10289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010290#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010291
Hanno Becker0271f962018-08-16 13:23:47 +010010292#if defined(MBEDTLS_SSL_PROTO_DTLS)
10293
10294static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10295{
10296 unsigned offset;
10297 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10298
10299 if( hs == NULL )
10300 return;
10301
Hanno Becker283f5ef2018-08-24 09:34:47 +010010302 ssl_free_buffered_record( ssl );
10303
Hanno Becker0271f962018-08-16 13:23:47 +010010304 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010305 ssl_buffering_free_slot( ssl, offset );
10306}
10307
10308static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10309 uint8_t slot )
10310{
10311 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10312 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010313
10314 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10315 return;
10316
Hanno Beckere605b192018-08-21 15:59:07 +010010317 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010318 {
Hanno Beckere605b192018-08-21 15:59:07 +010010319 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010320 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010321 mbedtls_free( hs_buf->data );
10322 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010323 }
10324}
10325
10326#endif /* MBEDTLS_SSL_PROTO_DTLS */
10327
Gilles Peskine9b562d52018-04-25 20:32:43 +020010328void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010329{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010330 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10331
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010332 if( handshake == NULL )
10333 return;
10334
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010335#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10336 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10337 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010338 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010339 handshake->async_in_progress = 0;
10340 }
10341#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10342
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010343#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10344 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10345 mbedtls_md5_free( &handshake->fin_md5 );
10346 mbedtls_sha1_free( &handshake->fin_sha1 );
10347#endif
10348#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10349#if defined(MBEDTLS_SHA256_C)
10350 mbedtls_sha256_free( &handshake->fin_sha256 );
10351#endif
10352#if defined(MBEDTLS_SHA512_C)
10353 mbedtls_sha512_free( &handshake->fin_sha512 );
10354#endif
10355#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357#if defined(MBEDTLS_DHM_C)
10358 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#if defined(MBEDTLS_ECDH_C)
10361 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010362#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010363#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010364 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010365#if defined(MBEDTLS_SSL_CLI_C)
10366 mbedtls_free( handshake->ecjpake_cache );
10367 handshake->ecjpake_cache = NULL;
10368 handshake->ecjpake_cache_len = 0;
10369#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010370#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010371
Janos Follath4ae5c292016-02-10 11:27:43 +000010372#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10373 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010374 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010376#endif
10377
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010378#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10379 if( handshake->psk != NULL )
10380 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010381 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010382 mbedtls_free( handshake->psk );
10383 }
10384#endif
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10387 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010388 /*
10389 * Free only the linked list wrapper, not the keys themselves
10390 * since the belong to the SNI callback
10391 */
10392 if( handshake->sni_key_cert != NULL )
10393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010394 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010395
10396 while( cur != NULL )
10397 {
10398 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010400 cur = next;
10401 }
10402 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010404
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010405#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010406 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010407 if( handshake->ecrs_peer_cert != NULL )
10408 {
10409 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10410 mbedtls_free( handshake->ecrs_peer_cert );
10411 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010412#endif
10413
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010414#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10415 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10416 mbedtls_pk_free( &handshake->peer_pubkey );
10417#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419#if defined(MBEDTLS_SSL_PROTO_DTLS)
10420 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010421 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010422 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010423#endif
10424
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010425 mbedtls_platform_zeroize( handshake,
10426 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010427}
10428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010430{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010431 if( session == NULL )
10432 return;
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010435 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010436#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010437
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010438#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010440#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010441
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010442 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010443}
10444
Paul Bakker5121ce52009-01-03 21:22:43 +000010445/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010446 * Serialize a full SSL context
10447 */
10448int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10449 unsigned char *buf,
10450 size_t buf_len,
10451 size_t *olen )
10452{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010453 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010454 (void) ssl;
10455
10456 if( buf != NULL )
10457 memset( buf, 0, buf_len );
10458
10459 *olen = 0;
10460
10461 return( 0 );
10462}
10463
10464/*
10465 * Deserialize a full SSL context
10466 */
10467int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10468 const unsigned char *buf,
10469 size_t len )
10470{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010471 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010472 (void) ssl;
10473 (void) buf;
10474 (void) len;
10475
10476 return( 0 );
10477}
10478
10479/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010480 * Free an SSL context
10481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010483{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010484 if( ssl == NULL )
10485 return;
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010488
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010489 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010490 {
Angus Grattond8213d02016-05-25 20:56:48 +100010491 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010493 }
10494
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010495 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010496 {
Angus Grattond8213d02016-05-25 20:56:48 +100010497 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010498 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010499 }
10500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010501#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010502 if( ssl->compress_buf != NULL )
10503 {
Angus Grattond8213d02016-05-25 20:56:48 +100010504 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010506 }
10507#endif
10508
Paul Bakker48916f92012-09-16 19:57:18 +000010509 if( ssl->transform )
10510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511 mbedtls_ssl_transform_free( ssl->transform );
10512 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010513 }
10514
10515 if( ssl->handshake )
10516 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010517 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010518 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10519 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010521 mbedtls_free( ssl->handshake );
10522 mbedtls_free( ssl->transform_negotiate );
10523 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010524 }
10525
Paul Bakkerc0463502013-02-14 11:19:38 +010010526 if( ssl->session )
10527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 mbedtls_ssl_session_free( ssl->session );
10529 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010530 }
10531
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010532#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010533 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010534 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010535 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010536 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010537 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010538#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10541 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10544 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010545 }
10546#endif
10547
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010548#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010549 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010550#endif
10551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010553
Paul Bakker86f04f42013-02-14 11:20:09 +010010554 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010555 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010556}
10557
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010558/*
10559 * Initialze mbedtls_ssl_config
10560 */
10561void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10562{
10563 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010564
10565#if !defined(MBEDTLS_SSL_PROTO_TLS)
10566 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10567#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010568}
10569
Simon Butcherc97b6972015-12-27 23:48:17 +000010570#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010571static int ssl_preset_default_hashes[] = {
10572#if defined(MBEDTLS_SHA512_C)
10573 MBEDTLS_MD_SHA512,
10574 MBEDTLS_MD_SHA384,
10575#endif
10576#if defined(MBEDTLS_SHA256_C)
10577 MBEDTLS_MD_SHA256,
10578 MBEDTLS_MD_SHA224,
10579#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010580#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010581 MBEDTLS_MD_SHA1,
10582#endif
10583 MBEDTLS_MD_NONE
10584};
Simon Butcherc97b6972015-12-27 23:48:17 +000010585#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010586
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010587static int ssl_preset_suiteb_ciphersuites[] = {
10588 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10589 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10590 0
10591};
10592
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010593#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010594static int ssl_preset_suiteb_hashes[] = {
10595 MBEDTLS_MD_SHA256,
10596 MBEDTLS_MD_SHA384,
10597 MBEDTLS_MD_NONE
10598};
10599#endif
10600
10601#if defined(MBEDTLS_ECP_C)
10602static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10603 MBEDTLS_ECP_DP_SECP256R1,
10604 MBEDTLS_ECP_DP_SECP384R1,
10605 MBEDTLS_ECP_DP_NONE
10606};
10607#endif
10608
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010609/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010610 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010612int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010613 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010614{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010615#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010616 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010617#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010618
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010619 /* Use the functions here so that they are covered in tests,
10620 * but otherwise access member directly for efficiency */
10621 mbedtls_ssl_conf_endpoint( conf, endpoint );
10622 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010623
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010624 /*
10625 * Things that are common to all presets
10626 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010627#if defined(MBEDTLS_SSL_CLI_C)
10628 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10629 {
10630 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10631#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10632 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10633#endif
10634 }
10635#endif
10636
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010637#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010638 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010639#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010640
10641#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10642 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10643#endif
10644
10645#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10646 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010647 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010648 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010649#endif
10650
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010651#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10652 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10653#endif
10654
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010655#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010656 conf->f_cookie_write = ssl_cookie_write_dummy;
10657 conf->f_cookie_check = ssl_cookie_check_dummy;
10658#endif
10659
10660#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10661 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10662#endif
10663
Janos Follath088ce432017-04-10 12:42:31 +010010664#if defined(MBEDTLS_SSL_SRV_C)
10665 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10666#endif
10667
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010668#if defined(MBEDTLS_SSL_PROTO_DTLS)
10669 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10670 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10671#endif
10672
10673#if defined(MBEDTLS_SSL_RENEGOTIATION)
10674 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010675 memset( conf->renego_period, 0x00, 2 );
10676 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010677#endif
10678
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010679#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10680 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10681 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010682 const unsigned char dhm_p[] =
10683 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10684 const unsigned char dhm_g[] =
10685 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10686
Hanno Beckera90658f2017-10-04 15:29:08 +010010687 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10688 dhm_p, sizeof( dhm_p ),
10689 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010690 {
10691 return( ret );
10692 }
10693 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010694#endif
10695
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010696 /*
10697 * Preset-specific defaults
10698 */
10699 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010700 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010701 /*
10702 * NSA Suite B
10703 */
10704 case MBEDTLS_SSL_PRESET_SUITEB:
10705 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10706 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10707 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10708 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10709
10710 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10711 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10712 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10713 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10714 ssl_preset_suiteb_ciphersuites;
10715
10716#if defined(MBEDTLS_X509_CRT_PARSE_C)
10717 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010718#endif
10719
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010720#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010721 conf->sig_hashes = ssl_preset_suiteb_hashes;
10722#endif
10723
10724#if defined(MBEDTLS_ECP_C)
10725 conf->curve_list = ssl_preset_suiteb_curves;
10726#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010727 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010728
10729 /*
10730 * Default
10731 */
10732 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010733 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10734 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10735 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10736 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10737 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10738 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10739 MBEDTLS_SSL_MIN_MINOR_VERSION :
10740 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010741 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10742 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10743
10744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010745 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010746 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10747#endif
10748
10749 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10750 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10751 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10752 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10753 mbedtls_ssl_list_ciphersuites();
10754
10755#if defined(MBEDTLS_X509_CRT_PARSE_C)
10756 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10757#endif
10758
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010759#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010760 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010761#endif
10762
10763#if defined(MBEDTLS_ECP_C)
10764 conf->curve_list = mbedtls_ecp_grp_id_list();
10765#endif
10766
10767#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10768 conf->dhm_min_bitlen = 1024;
10769#endif
10770 }
10771
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010772 return( 0 );
10773}
10774
10775/*
10776 * Free mbedtls_ssl_config
10777 */
10778void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10779{
10780#if defined(MBEDTLS_DHM_C)
10781 mbedtls_mpi_free( &conf->dhm_P );
10782 mbedtls_mpi_free( &conf->dhm_G );
10783#endif
10784
10785#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10786 if( conf->psk != NULL )
10787 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010788 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010789 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010790 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010791 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010792 }
10793
10794 if( conf->psk_identity != NULL )
10795 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010796 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010797 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010798 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010799 conf->psk_identity_len = 0;
10800 }
10801#endif
10802
10803#if defined(MBEDTLS_X509_CRT_PARSE_C)
10804 ssl_key_cert_free( conf->key_cert );
10805#endif
10806
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010807 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010808}
10809
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010810#if defined(MBEDTLS_PK_C) && \
10811 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010812/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010813 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010817#if defined(MBEDTLS_RSA_C)
10818 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10819 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010820#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821#if defined(MBEDTLS_ECDSA_C)
10822 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10823 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010826}
10827
Hanno Becker7e5437a2017-04-28 17:15:26 +010010828unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10829{
10830 switch( type ) {
10831 case MBEDTLS_PK_RSA:
10832 return( MBEDTLS_SSL_SIG_RSA );
10833 case MBEDTLS_PK_ECDSA:
10834 case MBEDTLS_PK_ECKEY:
10835 return( MBEDTLS_SSL_SIG_ECDSA );
10836 default:
10837 return( MBEDTLS_SSL_SIG_ANON );
10838 }
10839}
10840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010842{
10843 switch( sig )
10844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845#if defined(MBEDTLS_RSA_C)
10846 case MBEDTLS_SSL_SIG_RSA:
10847 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_ECDSA_C)
10850 case MBEDTLS_SSL_SIG_ECDSA:
10851 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010852#endif
10853 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010855 }
10856}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010857#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010858
Hanno Becker7e5437a2017-04-28 17:15:26 +010010859#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10860 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10861
10862/* Find an entry in a signature-hash set matching a given hash algorithm. */
10863mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10864 mbedtls_pk_type_t sig_alg )
10865{
10866 switch( sig_alg )
10867 {
10868 case MBEDTLS_PK_RSA:
10869 return( set->rsa );
10870 case MBEDTLS_PK_ECDSA:
10871 return( set->ecdsa );
10872 default:
10873 return( MBEDTLS_MD_NONE );
10874 }
10875}
10876
10877/* Add a signature-hash-pair to a signature-hash set */
10878void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10879 mbedtls_pk_type_t sig_alg,
10880 mbedtls_md_type_t md_alg )
10881{
10882 switch( sig_alg )
10883 {
10884 case MBEDTLS_PK_RSA:
10885 if( set->rsa == MBEDTLS_MD_NONE )
10886 set->rsa = md_alg;
10887 break;
10888
10889 case MBEDTLS_PK_ECDSA:
10890 if( set->ecdsa == MBEDTLS_MD_NONE )
10891 set->ecdsa = md_alg;
10892 break;
10893
10894 default:
10895 break;
10896 }
10897}
10898
10899/* Allow exactly one hash algorithm for each signature. */
10900void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10901 mbedtls_md_type_t md_alg )
10902{
10903 set->rsa = md_alg;
10904 set->ecdsa = md_alg;
10905}
10906
10907#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10908 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10909
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010910/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010911 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010914{
10915 switch( hash )
10916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_MD5_C)
10918 case MBEDTLS_SSL_HASH_MD5:
10919 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_SHA1_C)
10922 case MBEDTLS_SSL_HASH_SHA1:
10923 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010924#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925#if defined(MBEDTLS_SHA256_C)
10926 case MBEDTLS_SSL_HASH_SHA224:
10927 return( MBEDTLS_MD_SHA224 );
10928 case MBEDTLS_SSL_HASH_SHA256:
10929 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931#if defined(MBEDTLS_SHA512_C)
10932 case MBEDTLS_SSL_HASH_SHA384:
10933 return( MBEDTLS_MD_SHA384 );
10934 case MBEDTLS_SSL_HASH_SHA512:
10935 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010936#endif
10937 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010939 }
10940}
10941
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010942/*
10943 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10944 */
10945unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10946{
10947 switch( md )
10948 {
10949#if defined(MBEDTLS_MD5_C)
10950 case MBEDTLS_MD_MD5:
10951 return( MBEDTLS_SSL_HASH_MD5 );
10952#endif
10953#if defined(MBEDTLS_SHA1_C)
10954 case MBEDTLS_MD_SHA1:
10955 return( MBEDTLS_SSL_HASH_SHA1 );
10956#endif
10957#if defined(MBEDTLS_SHA256_C)
10958 case MBEDTLS_MD_SHA224:
10959 return( MBEDTLS_SSL_HASH_SHA224 );
10960 case MBEDTLS_MD_SHA256:
10961 return( MBEDTLS_SSL_HASH_SHA256 );
10962#endif
10963#if defined(MBEDTLS_SHA512_C)
10964 case MBEDTLS_MD_SHA384:
10965 return( MBEDTLS_SSL_HASH_SHA384 );
10966 case MBEDTLS_MD_SHA512:
10967 return( MBEDTLS_SSL_HASH_SHA512 );
10968#endif
10969 default:
10970 return( MBEDTLS_SSL_HASH_NONE );
10971 }
10972}
10973
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010974#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010975/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010976 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010977 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010978 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010979int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010980{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010982
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010983 if( ssl->conf->curve_list == NULL )
10984 return( -1 );
10985
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010986 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010987 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010988 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010989
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010990 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010991}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010992#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010993
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010994#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010995/*
10996 * Check if a hash proposed by the peer is in our list.
10997 * Return 0 if we're willing to use it, -1 otherwise.
10998 */
10999int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11000 mbedtls_md_type_t md )
11001{
11002 const int *cur;
11003
11004 if( ssl->conf->sig_hashes == NULL )
11005 return( -1 );
11006
11007 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11008 if( *cur == (int) md )
11009 return( 0 );
11010
11011 return( -1 );
11012}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011013#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#if defined(MBEDTLS_X509_CRT_PARSE_C)
11016int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11017 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011019 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011020{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011021 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011023 int usage = 0;
11024#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011026 const char *ext_oid;
11027 size_t ext_len;
11028#endif
11029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011030#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11031 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011032 ((void) cert);
11033 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011034 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011035#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11038 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011039 {
11040 /* Server part of the key exchange */
11041 switch( ciphersuite->key_exchange )
11042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 case MBEDTLS_KEY_EXCHANGE_RSA:
11044 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011045 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011046 break;
11047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11049 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11050 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11051 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011052 break;
11053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11055 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011056 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011057 break;
11058
11059 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011060 case MBEDTLS_KEY_EXCHANGE_NONE:
11061 case MBEDTLS_KEY_EXCHANGE_PSK:
11062 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11063 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011064 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011065 usage = 0;
11066 }
11067 }
11068 else
11069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11071 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011072 }
11073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011075 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011076 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011077 ret = -1;
11078 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011079#else
11080 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011081#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11084 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011086 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11087 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011088 }
11089 else
11090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11092 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011093 }
11094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011095 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011096 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011097 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011098 ret = -1;
11099 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011100#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011101
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011102 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011104#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011105
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011106/*
11107 * Convert version numbers to/from wire format
11108 * and, for DTLS, to/from TLS equivalent.
11109 *
11110 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011111 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011112 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11113 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011115void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011116 unsigned char ver[2] )
11117{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011118#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11119 ((void) transport);
11120#endif
11121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011123 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011125 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011126 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11127
11128 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11129 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11130 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011131 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011132#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011133#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011134 {
11135 ver[0] = (unsigned char) major;
11136 ver[1] = (unsigned char) minor;
11137 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011138#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011139}
11140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011142 const unsigned char ver[2] )
11143{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011144#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11145 ((void) transport);
11146#endif
11147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011149 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011150 {
11151 *major = 255 - ver[0] + 2;
11152 *minor = 255 - ver[1] + 1;
11153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011155 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11156 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011157 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011158#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011159#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011160 {
11161 *major = ver[0];
11162 *minor = ver[1];
11163 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011164#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011165}
11166
Simon Butcher99000142016-10-13 17:21:01 +010011167int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11168{
11169#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11170 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11171 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11172
11173 switch( md )
11174 {
11175#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11176#if defined(MBEDTLS_MD5_C)
11177 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011178 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011179#endif
11180#if defined(MBEDTLS_SHA1_C)
11181 case MBEDTLS_SSL_HASH_SHA1:
11182 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11183 break;
11184#endif
11185#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11186#if defined(MBEDTLS_SHA512_C)
11187 case MBEDTLS_SSL_HASH_SHA384:
11188 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11189 break;
11190#endif
11191#if defined(MBEDTLS_SHA256_C)
11192 case MBEDTLS_SSL_HASH_SHA256:
11193 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11194 break;
11195#endif
11196 default:
11197 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11198 }
11199
11200 return 0;
11201#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11202 (void) ssl;
11203 (void) md;
11204
11205 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11206#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11207}
11208
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11210 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11211int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11212 unsigned char *output,
11213 unsigned char *data, size_t data_len )
11214{
11215 int ret = 0;
11216 mbedtls_md5_context mbedtls_md5;
11217 mbedtls_sha1_context mbedtls_sha1;
11218
11219 mbedtls_md5_init( &mbedtls_md5 );
11220 mbedtls_sha1_init( &mbedtls_sha1 );
11221
11222 /*
11223 * digitally-signed struct {
11224 * opaque md5_hash[16];
11225 * opaque sha_hash[20];
11226 * };
11227 *
11228 * md5_hash
11229 * MD5(ClientHello.random + ServerHello.random
11230 * + ServerParams);
11231 * sha_hash
11232 * SHA(ClientHello.random + ServerHello.random
11233 * + ServerParams);
11234 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011235 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011236 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011238 goto exit;
11239 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011240 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011241 ssl->handshake->randbytes, 64 ) ) != 0 )
11242 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011244 goto exit;
11245 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011246 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011247 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011249 goto exit;
11250 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011251 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011252 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011253 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011254 goto exit;
11255 }
11256
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011257 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011258 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011259 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011260 goto exit;
11261 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011262 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011263 ssl->handshake->randbytes, 64 ) ) != 0 )
11264 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011266 goto exit;
11267 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011268 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011269 data_len ) ) != 0 )
11270 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011272 goto exit;
11273 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011274 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011275 output + 16 ) ) != 0 )
11276 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011278 goto exit;
11279 }
11280
11281exit:
11282 mbedtls_md5_free( &mbedtls_md5 );
11283 mbedtls_sha1_free( &mbedtls_sha1 );
11284
11285 if( ret != 0 )
11286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11287 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11288
11289 return( ret );
11290
11291}
11292#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11293 MBEDTLS_SSL_PROTO_TLS1_1 */
11294
11295#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11296 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11297int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011298 unsigned char *hash, size_t *hashlen,
11299 unsigned char *data, size_t data_len,
11300 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011301{
11302 int ret = 0;
11303 mbedtls_md_context_t ctx;
11304 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011305 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011306
11307 mbedtls_md_init( &ctx );
11308
11309 /*
11310 * digitally-signed struct {
11311 * opaque client_random[32];
11312 * opaque server_random[32];
11313 * ServerDHParams params;
11314 * };
11315 */
11316 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11317 {
11318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11319 goto exit;
11320 }
11321 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11322 {
11323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11324 goto exit;
11325 }
11326 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11327 {
11328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11329 goto exit;
11330 }
11331 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11332 {
11333 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11334 goto exit;
11335 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011336 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011337 {
11338 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11339 goto exit;
11340 }
11341
11342exit:
11343 mbedtls_md_free( &ctx );
11344
11345 if( ret != 0 )
11346 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11347 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11348
11349 return( ret );
11350}
11351#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11352 MBEDTLS_SSL_PROTO_TLS1_2 */
11353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011354#endif /* MBEDTLS_SSL_TLS_C */