blob: 471d3587a9b3fcb9123759296a66c97ae1315f97 [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)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 if( src->peer_cert != NULL )
379 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200380 int ret;
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200389 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 dst->peer_cert = NULL;
393 return( ret );
394 }
395 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000396
397#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
398 if( src->peer_cert_digest != NULL )
399 {
400 dst->peer_cert_digest_len = src->peer_cert_digest_len;
401 dst->peer_cert_digest =
402 mbedtls_calloc( 1, dst->peer_cert_digest_len );
403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
409 }
410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200415 if( src->ticket != NULL )
416 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200417 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 memcpy( dst->ticket, src->ticket, src->ticket_len );
422 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 return( 0 );
426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
429int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200430 const unsigned char *key_enc, const unsigned char *key_dec,
431 size_t keylen,
432 const unsigned char *iv_enc, const unsigned char *iv_dec,
433 size_t ivlen,
434 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200435 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
437int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
438int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Key material generation
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200447static int ssl3_prf( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char *dstbuf, size_t dlen )
451{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100452 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_context md5;
455 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 unsigned char padding[16];
457 unsigned char sha1sum[20];
458 ((void)label);
459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200460 mbedtls_md5_init( &md5 );
461 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200462
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 /*
464 * SSLv3:
465 * block =
466 * MD5( secret + SHA1( 'A' + secret + random ) ) +
467 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
468 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
469 * ...
470 */
471 for( i = 0; i < dlen / 16; i++ )
472 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200473 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000494 }
495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200497 mbedtls_md5_free( &md5 );
498 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500500 mbedtls_platform_zeroize( padding, sizeof( padding ) );
501 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int tls1_prf( const unsigned char *secret, size_t slen,
509 const char *label,
510 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t nb, hs;
514 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 unsigned char tmp[128];
517 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 const mbedtls_md_info_t *md_info;
519 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520 int ret;
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 hs = ( slen + 1 ) / 2;
528 S1 = secret;
529 S2 = secret + slen - hs;
530
531 nb = strlen( label );
532 memcpy( tmp + 20, label, nb );
533 memcpy( tmp + 20 + nb, random, rlen );
534 nb += rlen;
535
536 /*
537 * First compute P_md5(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 return( ret );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
546 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 for( i = 0; i < dlen; i += 16 )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
553 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
557 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
560
561 for( j = 0; j < k; j++ )
562 dstbuf[i + j] = h_i[j];
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 /*
568 * XOR out with P_sha1(secret,label+random)[0..dlen]
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574 return( ret );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
577 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
578 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 for( i = 0; i < dlen; i += 20 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
584 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_reset ( &md_ctx );
587 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
591
592 for( j = 0; j < k; j++ )
593 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500598 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
599 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 return( 0 );
602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
616 const mbedtls_md_info_t *md_info;
617 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618 int ret;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626
627 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100631 memcpy( tmp + md_len, label, nb );
632 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 nb += rlen;
634
635 /*
636 * Compute P_<hash>(secret, label + random)[0..dlen]
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100639 return( ret );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
642 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
643 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100644
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_reset ( &md_ctx );
648 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
649 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_md_hmac_reset ( &md_ctx );
652 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
653 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000654
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656
657 for( j = 0; j < k; j++ )
658 dstbuf[i + j] = h_i[j];
659 }
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100662
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500663 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
664 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000665
666 return( 0 );
667}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100670static int tls_prf_sha256( const unsigned char *secret, size_t slen,
671 const char *label,
672 const unsigned char *random, size_t rlen,
673 unsigned char *dstbuf, size_t dlen )
674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100676 label, random, rlen, dstbuf, dlen ) );
677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200681static int tls_prf_sha384( const unsigned char *secret, size_t slen,
682 const char *label,
683 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 unsigned char *dstbuf, size_t dlen )
685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SHA512_C */
690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
696static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697#endif
Paul Bakker380da532012-04-18 16:10:25 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200700static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200705static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
710#if defined(MBEDTLS_SHA256_C)
711static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200712static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SHA512_C)
717static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200718static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200723/* Type for the TLS PRF */
724typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
725 const unsigned char *, size_t,
726 unsigned char *, size_t);
727
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200728/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200729 * Populate a transform structure with session keys and all the other
730 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200731 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200732 * Parameters:
733 * - [in/out]: transform: structure to populate
734 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200735 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 * - [in] ciphersuite
737 * - [in] master
738 * - [in] encrypt_then_mac
739 * - [in] trunc_hmac
740 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200741 * - [in] tls_prf: pointer to PRF to use for key derivation
742 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200743 * - [in] minor_ver: SSL/TLS minor version
744 * - [in] endpoint: client or server
745 * - [in] ssl: optionally used for:
746 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
747 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
748 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200749 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200750static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200751 int ciphersuite,
752 const unsigned char master[48],
753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
754 int encrypt_then_mac,
755#endif
756#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
757 int trunc_hmac,
758#endif
759#if defined(MBEDTLS_ZLIB_SUPPORT)
760 int compression,
761#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200762 ssl_tls_prf_t tls_prf,
763 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200764 int minor_ver,
765 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200766 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200768 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 unsigned char keyblk[256];
770 unsigned char *key1;
771 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100772 unsigned char *mac_enc;
773 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000774 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200775 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000776 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 const mbedtls_cipher_info_t *cipher_info;
779 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100780
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200781#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
782 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
783 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200784 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200785 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000786#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000787
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200788 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200790 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200792 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200794 /*
795 * Get various info structures
796 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200797 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 if( ciphersuite_info == NULL )
799 {
800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
803 }
804
Hanno Becker8759e162017-12-27 21:34:08 +0000805 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100806 if( cipher_info == NULL )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000809 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100811 }
812
Hanno Becker8759e162017-12-27 21:34:08 +0000813 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100814 if( md_info == NULL )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000817 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100819 }
820
Hanno Beckera5a2b082019-05-15 14:03:01 +0100821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822 /* Copy own and peer's CID if the use of the CID
823 * extension has been negotiated. */
824 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
825 {
826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100827
Hanno Becker4932f9f2019-05-03 15:23:51 +0100828 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100830 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100831 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100832
833 transform->out_cid_len = ssl->handshake->peer_cid_len;
834 memcpy( transform->out_cid, ssl->handshake->peer_cid,
835 ssl->handshake->peer_cid_len );
836 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
837 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100838 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200842 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200844 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100845 if( ret != 0 )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100848 return( ret );
849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200852 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200853 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200854 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 /*
858 * Determine the appropriate key, IV and MAC length.
859 */
Paul Bakker68884e32013-01-07 18:20:04 +0100860
Hanno Beckere7f2df02017-12-27 08:17:40 +0000861 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200862
Hanno Beckerf1229442018-01-03 15:32:31 +0000863#if defined(MBEDTLS_GCM_C) || \
864 defined(MBEDTLS_CCM_C) || \
865 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 cipher_info->mode == MBEDTLS_MODE_CCM ||
868 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 {
Hanno Becker8759e162017-12-27 21:34:08 +0000870 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->taglen =
875 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200877 /* All modes haves 96-bit IVs;
878 * GCM and CCM has 4 implicit and 8 explicit bytes
879 * ChachaPoly has all 12 bytes implicit
880 */
Paul Bakker68884e32013-01-07 18:20:04 +0100881 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200882 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
883 transform->fixed_ivlen = 12;
884 else
885 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200886
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200887 /* Minimum length of encrypted record */
888 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000889 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
891 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000892#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
893#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
894 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
895 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100896 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200897 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
899 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200905 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 mac_key_len = mbedtls_md_get_size( md_info );
907 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910 /*
911 * If HMAC is to be truncated, we shall keep the leftmost bytes,
912 * (rfc 6066 page 13 or rfc 2104 section 4),
913 * so we only need to adjust the length here.
914 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200915 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000918
919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
920 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000921 * HMAC implementation which also truncates the key
922 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000923 mac_key_len = transform->maclen;
924#endif
925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927
928 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200931 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200933 transform->minlen = transform->maclen;
934 else
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200936 /*
937 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100938 * 1. if EtM is in use: one block plus MAC
939 * otherwise: * first multiple of blocklen greater than maclen
940 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200943 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100944 {
945 transform->minlen = transform->maclen
946 + cipher_info->block_size;
947 }
948 else
949#endif
950 {
951 transform->minlen = transform->maclen
952 + cipher_info->block_size
953 - transform->maclen % cipher_info->block_size;
954 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200957 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
958 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100960 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200963 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
964 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965 {
966 transform->minlen += transform->ivlen;
967 }
968 else
969#endif
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000976 else
977#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
978 {
979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Hanno Beckere7f2df02017-12-27 08:17:40 +0000983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
984 (unsigned) keylen,
985 (unsigned) transform->minlen,
986 (unsigned) transform->ivlen,
987 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 /*
990 * Finally setup the cipher contexts, IVs and MAC secrets.
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000996 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker68884e32013-01-07 18:20:04 +0100998 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1007 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_CLI_C */
1012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001013 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001015 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker81c7b182017-11-09 18:39:33 +00001018 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001019 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001021 /*
1022 * This is not used in TLS v1.1.
1023 */
Paul Bakker48916f92012-09-16 19:57:18 +00001024 iv_copy_len = ( transform->fixed_ivlen ) ?
1025 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001026 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1027 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Becker92231322018-01-03 15:32:51 +00001037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Hanno Becker92231322018-01-03 15:32:51 +00001041 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001045 }
1046
Hanno Becker81c7b182017-11-09 18:39:33 +00001047 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1048 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001049 }
1050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001054 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001055 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001056 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1057 For AEAD-based ciphersuites, there is nothing to do here. */
1058 if( mac_key_len != 0 )
1059 {
1060 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1061 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1062 }
Paul Bakker68884e32013-01-07 18:20:04 +01001063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001064 else
1065#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Hanno Becker92231322018-01-03 15:32:51 +00001070#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1073 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001074 {
1075 int ret = 0;
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001080 transform->iv_enc, transform->iv_dec,
1081 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001082 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001083 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1086 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001087 }
1088 }
Hanno Becker92231322018-01-03 15:32:51 +00001089#else
1090 ((void) mac_dec);
1091 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001093
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001094#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1095 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001096 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001097 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001098 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001099 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 iv_copy_len );
1101 }
1102#endif
1103
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001104 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001110
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 cipher_info ) ) != 0 )
1113 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115 return( ret );
1116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001119 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123 return( ret );
1124 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001127 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 return( ret );
1132 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_CIPHER_MODE_CBC)
1135 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1138 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001142 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1145 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001148 return( ret );
1149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001155 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001157 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001160
Paul Bakker48916f92012-09-16 19:57:18 +00001161 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1162 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001163
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 if( deflateInit( &transform->ctx_deflate,
1165 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001166 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1169 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001170 }
1171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 return( 0 );
1175}
1176
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001177/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001178 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001179 *
1180 * Inputs:
1181 * - SSL/TLS minor version
1182 * - hash associated with the ciphersuite (only used by TLS 1.2)
1183 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001184 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1186 */
1187static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1188 int minor_ver,
1189 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001190{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001191#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1192 (void) hash;
1193#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001196 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = ssl3_prf;
1199 handshake->calc_verify = ssl_calc_verify_ssl;
1200 handshake->calc_finished = ssl_calc_finished_ssl;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls1_prf;
1208 handshake->calc_verify = ssl_calc_verify_tls;
1209 handshake->calc_finished = ssl_calc_finished_tls;
1210 }
1211 else
1212#endif
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1214#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001215 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1216 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217 {
1218 handshake->tls_prf = tls_prf_sha384;
1219 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1220 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1221 }
1222 else
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001226 {
1227 handshake->tls_prf = tls_prf_sha256;
1228 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1229 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1230 }
1231 else
1232#endif
1233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1234 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 return( 0 );
1239}
1240
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001243 *
1244 * Parameters:
1245 * [in/out] handshake
1246 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1247 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001248 * [out] master
1249 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001251static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001253 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254{
1255 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256
1257#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001258 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001259 (void) ssl;
1260#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001263 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1265 return( 0 );
1266 }
1267
1268 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1269 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001270
1271#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001272 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273 {
1274 unsigned char session_hash[48];
1275 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001277 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001279 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1280 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001281
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001282 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283 "extended master secret",
1284 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001285 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 }
1287 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001289 {
1290 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1291 "master secret",
1292 handshake->randbytes, 64,
1293 master, 48 );
1294 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295 if( ret != 0 )
1296 {
1297 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1298 return( ret );
1299 }
1300
1301 mbedtls_platform_zeroize( handshake->premaster,
1302 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001303
1304 return( 0 );
1305}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001307int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1308{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001310 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1311 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001312
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1314
1315 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001316 ret = ssl_set_handshake_prfs( ssl->handshake,
1317 ssl->minor_ver,
1318 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 {
1321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001322 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001325 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001326 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ssl->session_negotiate->master,
1328 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001329 if( ret != 0 )
1330 {
1331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1332 return( ret );
1333 }
1334
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001335 /* Swap the client and server random values:
1336 * - MS derivation wanted client+server (RFC 5246 8.1)
1337 * - key derivation wants server+client (RFC 5246 6.3) */
1338 {
1339 unsigned char tmp[64];
1340 memcpy( tmp, ssl->handshake->randbytes, 64 );
1341 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1342 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1343 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1344 }
1345
1346 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001347 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001348 ssl->session_negotiate->ciphersuite,
1349 ssl->session_negotiate->master,
1350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1351 ssl->session_negotiate->encrypt_then_mac,
1352#endif
1353#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1354 ssl->session_negotiate->trunc_hmac,
1355#endif
1356#if defined(MBEDTLS_ZLIB_SUPPORT)
1357 ssl->session_negotiate->compression,
1358#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001359 ssl->handshake->tls_prf,
1360 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001361 ssl->minor_ver,
1362 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001363 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001364 if( ret != 0 )
1365 {
1366 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1367 return( ret );
1368 }
1369
1370 /* We no longer need Server/ClientHello.random values */
1371 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1372 sizeof( ssl->handshake->randbytes ) );
1373
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001374 /* Allocate compression buffer */
1375#if defined(MBEDTLS_ZLIB_SUPPORT)
1376 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1377 ssl->compress_buf == NULL )
1378 {
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1380 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1381 if( ssl->compress_buf == NULL )
1382 {
1383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001384 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1386 }
1387 }
1388#endif
1389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1391
1392 return( 0 );
1393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001396void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1397 unsigned char hash[36],
1398 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_context md5;
1401 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 unsigned char pad_1[48];
1403 unsigned char pad_2[48];
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001407 mbedtls_md5_init( &md5 );
1408 mbedtls_sha1_init( &sha1 );
1409
1410 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1411 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Paul Bakker380da532012-04-18 16:10:25 +00001413 memset( pad_1, 0x36, 48 );
1414 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001416 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1417 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1418 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001420 mbedtls_md5_starts_ret( &md5 );
1421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1423 mbedtls_md5_update_ret( &md5, hash, 16 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1427 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1428 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_starts_ret( &sha1 );
1431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1433 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001436 *hlen = 36;
1437
1438 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001441 mbedtls_md5_free( &md5 );
1442 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001443
Paul Bakker380da532012-04-18 16:10:25 +00001444 return;
1445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001449void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1450 unsigned char hash[36],
1451 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001452{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_md5_context md5;
1454 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001458 mbedtls_md5_init( &md5 );
1459 mbedtls_sha1_init( &sha1 );
1460
1461 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1462 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_md5_finish_ret( &md5, hash );
1465 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001467 *hlen = 36;
1468
1469 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001472 mbedtls_md5_free( &md5 );
1473 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001474
Paul Bakker380da532012-04-18 16:10:25 +00001475 return;
1476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1480#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001481void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1482 unsigned char hash[32],
1483 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001484{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001485 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001487 mbedtls_sha256_init( &sha256 );
1488
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001491 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001492 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001494 *hlen = 32;
1495
1496 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001499 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001500
Paul Bakker380da532012-04-18 16:10:25 +00001501 return;
1502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001506void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1507 unsigned char hash[48],
1508 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001509{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha512_init( &sha512 );
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001519 *hlen = 48;
1520
1521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA512_C */
1529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1532int 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 +02001533{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534 unsigned char *p = ssl->handshake->premaster;
1535 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001536 const unsigned char *psk = ssl->conf->psk;
1537 size_t psk_len = ssl->conf->psk_len;
1538
1539 /* If the psk callback was called, use its result */
1540 if( ssl->handshake->psk != NULL )
1541 {
1542 psk = ssl->handshake->psk;
1543 psk_len = ssl->handshake->psk_len;
1544 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001545
1546 /*
1547 * PMS = struct {
1548 * opaque other_secret<0..2^16-1>;
1549 * opaque psk<0..2^16-1>;
1550 * };
1551 * with "other_secret" depending on the particular key exchange
1552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1554 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001555 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001556 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001559 *(p++) = (unsigned char)( psk_len >> 8 );
1560 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001561
1562 if( end < p || (size_t)( end - p ) < psk_len )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
1565 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 }
1568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1570#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001572 {
1573 /*
1574 * other_secret already set by the ClientKeyExchange message,
1575 * and is 48 bytes long
1576 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001577 if( end - p < 2 )
1578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1579
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001580 *p++ = 0;
1581 *p++ = 48;
1582 p += 48;
1583 }
1584 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1586#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1587 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001589 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001590 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001591
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001592 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001594 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 return( ret );
1599 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001600 *(p++) = (unsigned char)( len >> 8 );
1601 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 p += len;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 }
1606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1608#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1609 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001611 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 size_t zlen;
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001615 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001616 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 return( ret );
1620 }
1621
1622 *(p++) = (unsigned char)( zlen >> 8 );
1623 *(p++) = (unsigned char)( zlen );
1624 p += zlen;
1625
Janos Follath3fbdada2018-08-15 10:26:53 +01001626 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1627 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 }
1629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635
1636 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001637 if( end - p < 2 )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001639
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001640 *(p++) = (unsigned char)( psk_len >> 8 );
1641 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001642
1643 if( end < p || (size_t)( end - p ) < psk_len )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 memcpy( p, psk, psk_len );
1647 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648
1649 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1650
1651 return( 0 );
1652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656/*
1657 * SSLv3.0 MAC functions
1658 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001659#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001660static void ssl_mac( mbedtls_md_context_t *md_ctx,
1661 const unsigned char *secret,
1662 const unsigned char *buf, size_t len,
1663 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001664 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665{
1666 unsigned char header[11];
1667 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001668 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1670 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001671
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001672 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001674 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 else
Paul Bakker68884e32013-01-07 18:20:04 +01001676 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 memcpy( header, ctr, 8 );
1679 header[ 8] = (unsigned char) type;
1680 header[ 9] = (unsigned char)( len >> 8 );
1681 header[10] = (unsigned char)( len );
1682
Paul Bakker68884e32013-01-07 18:20:04 +01001683 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_md_starts( md_ctx );
1685 mbedtls_md_update( md_ctx, secret, md_size );
1686 mbedtls_md_update( md_ctx, padding, padlen );
1687 mbedtls_md_update( md_ctx, header, 11 );
1688 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001689 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakker68884e32013-01-07 18:20:04 +01001691 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_md_starts( md_ctx );
1693 mbedtls_md_update( md_ctx, secret, md_size );
1694 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_update( md_ctx, out, md_size );
1696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001699
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001701 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001703 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1704 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1706/* This function makes sure every byte in the memory region is accessed
1707 * (in ascending addresses order) */
1708static void ssl_read_memory( unsigned char *p, size_t len )
1709{
1710 unsigned char acc = 0;
1711 volatile unsigned char force;
1712
1713 for( ; len != 0; p++, len-- )
1714 acc ^= *p;
1715
1716 force = acc;
1717 (void) force;
1718}
1719#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1720
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001721/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001723 */
Hanno Becker3307b532017-12-27 21:37:21 +00001724
Hanno Beckera5a2b082019-05-15 14:03:01 +01001725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001726/* This functions transforms a DTLS plaintext fragment and a record content
1727 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001728 *
1729 * struct {
1730 * opaque content[DTLSPlaintext.length];
1731 * ContentType real_type;
1732 * uint8 zeros[length_of_padding];
1733 * } DTLSInnerPlaintext;
1734 *
1735 * Input:
1736 * - `content`: The beginning of the buffer holding the
1737 * plaintext to be wrapped.
1738 * - `*content_size`: The length of the plaintext in Bytes.
1739 * - `max_len`: The number of Bytes available starting from
1740 * `content`. This must be `>= *content_size`.
1741 * - `rec_type`: The desired record content type.
1742 *
1743 * Output:
1744 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1745 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1746 *
1747 * Returns:
1748 * - `0` on success.
1749 * - A negative error code if `max_len` didn't offer enough space
1750 * for the expansion.
1751 */
1752static int ssl_cid_build_inner_plaintext( unsigned char *content,
1753 size_t *content_size,
1754 size_t remaining,
1755 uint8_t rec_type )
1756{
1757 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001758 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1759 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1760 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001761
1762 /* Write real content type */
1763 if( remaining == 0 )
1764 return( -1 );
1765 content[ len ] = rec_type;
1766 len++;
1767 remaining--;
1768
1769 if( remaining < pad )
1770 return( -1 );
1771 memset( content + len, 0, pad );
1772 len += pad;
1773 remaining -= pad;
1774
1775 *content_size = len;
1776 return( 0 );
1777}
1778
Hanno Becker7dc25772019-05-20 15:08:01 +01001779/* This function parses a DTLSInnerPlaintext structure.
1780 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1782 size_t *content_size,
1783 uint8_t *rec_type )
1784{
1785 size_t remaining = *content_size;
1786
1787 /* Determine length of padding by skipping zeroes from the back. */
1788 do
1789 {
1790 if( remaining == 0 )
1791 return( -1 );
1792 remaining--;
1793 } while( content[ remaining ] == 0 );
1794
1795 *content_size = remaining;
1796 *rec_type = content[ remaining ];
1797
1798 return( 0 );
1799}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001800#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001801
Hanno Becker99abf512019-05-20 14:50:53 +01001802/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001803 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001804static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001805 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001806 mbedtls_record *rec )
1807{
Hanno Becker99abf512019-05-20 14:50:53 +01001808 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001809 *
1810 * additional_data = seq_num + TLSCompressed.type +
1811 * TLSCompressed.version + TLSCompressed.length;
1812 *
Hanno Becker99abf512019-05-20 14:50:53 +01001813 * For the CID extension, this is extended as follows
1814 * (quoting draft-ietf-tls-dtls-connection-id-05,
1815 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + DTLSPlaintext.type +
1818 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * cid +
1820 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001821 * length_of_DTLSInnerPlaintext;
1822 */
1823
Hanno Becker3307b532017-12-27 21:37:21 +00001824 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1825 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001826 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001827
Hanno Beckera5a2b082019-05-15 14:03:01 +01001828#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001829 if( rec->cid_len != 0 )
1830 {
1831 memcpy( add_data + 11, rec->cid, rec->cid_len );
1832 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1833 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1834 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1835 *add_data_len = 13 + 1 + rec->cid_len;
1836 }
1837 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001838#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001839 {
1840 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13;
1843 }
Hanno Becker3307b532017-12-27 21:37:21 +00001844}
1845
Hanno Becker611a83b2018-01-03 14:27:32 +00001846int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1847 mbedtls_ssl_transform *transform,
1848 mbedtls_record *rec,
1849 int (*f_rng)(void *, unsigned char *, size_t),
1850 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001854 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001856 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001857 size_t post_avail;
1858
1859 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001860#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001861 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001862 ((void) ssl);
1863#endif
1864
1865 /* The PRNG is used for dynamic IV generation that's used
1866 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1867#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1868 ( defined(MBEDTLS_AES_C) || \
1869 defined(MBEDTLS_ARIA_C) || \
1870 defined(MBEDTLS_CAMELLIA_C) ) && \
1871 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1872 ((void) f_rng);
1873 ((void) p_rng);
1874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 {
Hanno Becker3307b532017-12-27 21:37:21 +00001880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1882 }
Hanno Becker505089d2019-05-01 09:45:57 +01001883 if( rec == NULL
1884 || rec->buf == NULL
1885 || rec->buf_len < rec->data_offset
1886 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001888 || rec->cid_len != 0
1889#endif
1890 )
Hanno Becker3307b532017-12-27 21:37:21 +00001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 }
1895
Hanno Becker3307b532017-12-27 21:37:21 +00001896 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001897 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001899 data, rec->data_len );
1900
1901 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1902
1903 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1904 {
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1906 (unsigned) rec->data_len,
1907 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1909 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001910
Hanno Beckera5a2b082019-05-15 14:03:01 +01001911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001912 /*
1913 * Add CID information
1914 */
1915 rec->cid_len = transform->out_cid_len;
1916 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1917 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001918
1919 if( rec->cid_len != 0 )
1920 {
1921 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001922 * Wrap plaintext into DTLSInnerPlaintext structure.
1923 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001924 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001925 * Note that this changes `rec->data_len`, and hence
1926 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001927 */
1928 if( ssl_cid_build_inner_plaintext( data,
1929 &rec->data_len,
1930 post_avail,
1931 rec->type ) != 0 )
1932 {
1933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1934 }
1935
1936 rec->type = MBEDTLS_SSL_MSG_CID;
1937 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001938#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001939
Hanno Becker92c930f2019-04-29 17:31:37 +01001940 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1941
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001943 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( mode == MBEDTLS_MODE_STREAM ||
1947 ( mode == MBEDTLS_MODE_CBC
1948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001949 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950#endif
1951 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 {
Hanno Becker3307b532017-12-27 21:37:21 +00001953 if( post_avail < transform->maclen )
1954 {
1955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001961 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001962 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001963 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1964 data, rec->data_len, rec->ctr, rec->type, mac );
1965 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 }
1967 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001971 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Hanno Becker992b6872017-11-09 18:57:39 +00001973 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1974
Hanno Beckere83efe62019-04-29 13:52:53 +01001975 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001978 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001979 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1980 data, rec->data_len );
1981 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1982 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1983
1984 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001985 }
1986 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001987#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992
Hanno Becker3307b532017-12-27 21:37:21 +00001993 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1994 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995
Hanno Becker3307b532017-12-27 21:37:21 +00001996 rec->data_len += transform->maclen;
1997 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001999 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002000#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002 /*
2003 * Encrypt
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2006 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002009 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002011 "including %d bytes of padding",
2012 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Hanno Becker3307b532017-12-27 21:37:21 +00002014 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2015 transform->iv_enc, transform->ivlen,
2016 data, rec->data_len,
2017 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002020 return( ret );
2021 }
2022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakker68884e32013-01-07 18:20:04 +01002029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002031
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002032#if defined(MBEDTLS_GCM_C) || \
2033 defined(MBEDTLS_CCM_C) || \
2034 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 mode == MBEDTLS_MODE_CCM ||
2037 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002038 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002039 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002041 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002042
Hanno Becker3307b532017-12-27 21:37:21 +00002043 /* Check that there's space for both the authentication tag
2044 * and the explicit IV before and after the record content. */
2045 if( post_avail < transform->taglen ||
2046 rec->data_offset < explicit_iv_len )
2047 {
2048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2050 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Paul Bakker68884e32013-01-07 18:20:04 +01002052 /*
2053 * Generate IV
2054 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002055 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2056 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002057 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002059 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2060 explicit_iv_len );
2061 /* Prefix record content with explicit IV. */
2062 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 }
2064 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 unsigned char i;
2068
2069 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2070
2071 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002072 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 }
2074 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002075 {
2076 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002079 }
2080
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002082
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002083 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2084 iv, transform->ivlen );
2085 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002086 data - explicit_iv_len, explicit_iv_len );
2087 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002092
Paul Bakker68884e32013-01-07 18:20:04 +01002093 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095 */
Hanno Becker3307b532017-12-27 21:37:21 +00002096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002098 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002099 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002100 data, rec->data_len, /* source */
2101 data, &rec->data_len, /* destination */
2102 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002105 return( ret );
2106 }
2107
Hanno Becker3307b532017-12-27 21:37:21 +00002108 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2109 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 rec->data_len += transform->taglen + explicit_iv_len;
2112 rec->data_offset -= explicit_iv_len;
2113 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2118#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002119 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002122 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002123 size_t padlen, i;
2124 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002125
Hanno Becker3307b532017-12-27 21:37:21 +00002126 /* Currently we're always using minimal padding
2127 * (up to 255 bytes would be allowed). */
2128 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2129 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 padlen = 0;
2131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Check there's enough space in the buffer for the padding. */
2133 if( post_avail < padlen + 1 )
2134 {
2135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2136 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2137 }
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002140 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker3307b532017-12-27 21:37:21 +00002142 rec->data_len += padlen + 1;
2143 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002146 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002147 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2148 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 */
Hanno Becker3307b532017-12-27 21:37:21 +00002150 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151 {
Hanno Becker3307b532017-12-27 21:37:21 +00002152 if( f_rng == NULL )
2153 {
2154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2156 }
2157
2158 if( rec->data_offset < transform->ivlen )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2161 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2162 }
2163
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002164 /*
2165 * Generate IV
2166 */
Hanno Becker3307b532017-12-27 21:37:21 +00002167 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002168 if( ret != 0 )
2169 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 memcpy( data - transform->ivlen, transform->iv_enc,
2172 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002179 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002180 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
Hanno Becker3307b532017-12-27 21:37:21 +00002182 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2183 transform->iv_enc,
2184 transform->ivlen,
2185 data, rec->data_len,
2186 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002189 return( ret );
2190 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002191
Hanno Becker3307b532017-12-27 21:37:21 +00002192 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
2201 /*
2202 * Save IV in SSL3 and TLS1
2203 */
Hanno Becker3307b532017-12-27 21:37:21 +00002204 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2205 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
Hanno Becker3307b532017-12-27 21:37:21 +00002207 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002208#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002209 {
2210 data -= transform->ivlen;
2211 rec->data_offset -= transform->ivlen;
2212 rec->data_len += transform->ivlen;
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002216 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002218 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2219
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220 /*
2221 * MAC(MAC_write_key, seq_num +
2222 * TLSCipherText.type +
2223 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002224 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 * IV + // except for TLS 1.0
2226 * ENC(content + padding + padding_length));
2227 */
Hanno Becker3307b532017-12-27 21:37:21 +00002228
2229 if( post_avail < transform->maclen)
2230 {
2231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2232 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2233 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Beckere83efe62019-04-29 13:52:53 +01002235 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002238 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002239 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240
Hanno Becker3307b532017-12-27 21:37:21 +00002241 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2244 data, rec->data_len );
2245 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2246 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 rec->data_len += transform->maclen;
2251 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002252 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002258 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 /* Make extra sure authentication was performed, exactly once */
2265 if( auth_done != 1 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 return( 0 );
2274}
2275
Hanno Becker611a83b2018-01-03 14:27:32 +00002276int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2277 mbedtls_ssl_transform *transform,
2278 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002282 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002283#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002284 size_t padlen = 0, correct = 1;
2285#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002287 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002288 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289
Hanno Becker611a83b2018-01-03 14:27:32 +00002290#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002291 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 ((void) ssl);
2293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2300 }
2301 if( rec == NULL ||
2302 rec->buf == NULL ||
2303 rec->buf_len < rec->data_offset ||
2304 rec->buf_len - rec->data_offset < rec->data_len )
2305 {
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002308 }
2309
Hanno Becker4c6876b2017-12-27 21:28:58 +00002310 data = rec->buf + rec->data_offset;
2311 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Hanno Beckera5a2b082019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002314 /*
2315 * Match record's CID with incoming CID.
2316 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002317 if( rec->cid_len != transform->in_cid_len ||
2318 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2319 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002320 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002321 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2325 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002326 {
2327 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002328 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2329 transform->iv_dec,
2330 transform->ivlen,
2331 data, rec->data_len,
2332 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002335 return( ret );
2336 }
2337
Hanno Becker4c6876b2017-12-27 21:28:58 +00002338 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Paul Bakker68884e32013-01-07 18:20:04 +01002344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346#if defined(MBEDTLS_GCM_C) || \
2347 defined(MBEDTLS_CCM_C) || \
2348 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 mode == MBEDTLS_MODE_CCM ||
2351 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 /*
2357 * Compute and update sizes
2358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002362 "+ taglen (%d)", rec->data_len,
2363 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002365 }
Paul Bakker68884e32013-01-07 18:20:04 +01002366
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002367 /*
2368 * Prepare IV
2369 */
2370 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2371 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002372 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 }
2377 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 unsigned char i;
2381
2382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2383
2384 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 }
2387 else
2388 {
2389 /* Reminder if we ever add an AEAD mode with a different size */
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2392 }
2393
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 data += explicit_iv_len;
2395 rec->data_offset += explicit_iv_len;
2396 rec->data_len -= explicit_iv_len + transform->taglen;
2397
Hanno Beckere83efe62019-04-29 13:52:53 +01002398 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002399 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401
2402 memcpy( transform->iv_dec + transform->fixed_ivlen,
2403 data - explicit_iv_len, explicit_iv_len );
2404
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002407 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002408
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002411 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002412 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2414 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002415 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002416 data, rec->data_len,
2417 data, &olen,
2418 data + rec->data_len,
2419 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002425
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002426 return( ret );
2427 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002434 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2438#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002439 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002442 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
Paul Bakker45829992013-01-03 14:52:21 +01002445 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002448 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2449 {
2450 /* The ciphertext is prefixed with the CBC IV. */
2451 minlen += transform->ivlen;
2452 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#endif
Paul Bakker45829992013-01-03 14:52:21 +01002454
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 /* Size considerations:
2456 *
2457 * - The CBC cipher text must not be empty and hence
2458 * at least of size transform->ivlen.
2459 *
2460 * Together with the potential IV-prefix, this explains
2461 * the first of the two checks below.
2462 *
2463 * - The record must contain a MAC, either in plain or
2464 * encrypted, depending on whether Encrypt-then-MAC
2465 * is used or not.
2466 * - If it is, the message contains the IV-prefix,
2467 * the CBC ciphertext, and the MAC.
2468 * - If it is not, the padded plaintext, and hence
2469 * the CBC ciphertext, has at least length maclen + 1
2470 * because there is at least the padding length byte.
2471 *
2472 * As the CBC ciphertext is not empty, both cases give the
2473 * lower bound minlen + maclen + 1 on the record size, which
2474 * we test for in the second check below.
2475 */
2476 if( rec->data_len < minlen + transform->ivlen ||
2477 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 "+ 1 ) ( + expl IV )", rec->data_len,
2481 transform->ivlen,
2482 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002484 }
2485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 /*
2487 * Authenticate before decrypt if enabled
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 {
Hanno Becker992b6872017-11-09 18:57:39 +00002492 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2497 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Beckere83efe62019-04-29 13:52:53 +01002499 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002500
Hanno Beckere83efe62019-04-29 13:52:53 +01002501 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2502 add_data_len );
2503 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2504 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2506 data, rec->data_len );
2507 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2508 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2511 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002512 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2516 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524
2525 /*
2526 * Check length sanity
2527 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002528 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002537 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002538 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002541 /* This is safe because data_len >= minlen + maclen + 1 initially,
2542 * and at this point we have at most subtracted maclen (note that
2543 * minlen == transform->ivlen here). */
2544 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 data += transform->ivlen;
2547 rec->data_offset += transform->ivlen;
2548 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2553 transform->iv_dec, transform->ivlen,
2554 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002557 return( ret );
2558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002559
Hanno Becker4c6876b2017-12-27 21:28:58 +00002560 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
2569 /*
2570 * Save IV in SSL3 and TLS1
2571 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2573 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577 /* Safe since data_len >= minlen + maclen + 1, so after having
2578 * subtracted at most minlen and maclen up to this point,
2579 * data_len > 0. */
2580 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002581
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582 if( auth_done == 1 )
2583 {
2584 correct *= ( rec->data_len >= padlen + 1 );
2585 padlen *= ( rec->data_len >= padlen + 1 );
2586 }
2587 else
Paul Bakker45829992013-01-03 14:52:21 +01002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590 if( rec->data_len < transform->maclen + padlen + 1 )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2593 rec->data_len,
2594 transform->maclen,
2595 padlen + 1 ) );
2596 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002598
2599 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2600 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker4c6876b2017-12-27 21:28:58 +00002603 padlen++;
2604
2605 /* Regardless of the validity of the padding,
2606 * we have data_len >= padlen here. */
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_DEBUG_ALL)
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 "should be no more than %d",
2616 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002617#endif
Paul Bakker45829992013-01-03 14:52:21 +01002618 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
2621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002625 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002627 /* The padding check involves a series of up to 256
2628 * consecutive memory reads at the end of the record
2629 * plaintext buffer. In order to hide the length and
2630 * validity of the padding, always perform exactly
2631 * `min(256,plaintext_len)` reads (but take into account
2632 * only the last `padlen` bytes for the padding check). */
2633 size_t pad_count = 0;
2634 size_t real_count = 0;
2635 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002636
Hanno Becker4c6876b2017-12-27 21:28:58 +00002637 /* Index of first padding byte; it has been ensured above
2638 * that the subtraction is safe. */
2639 size_t const padding_idx = rec->data_len - padlen;
2640 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2641 size_t const start_idx = rec->data_len - num_checks;
2642 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002645 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 real_count |= ( idx >= padding_idx );
2647 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002648 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002652 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002654#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002655 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2659 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002663 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664
Hanno Becker4c6876b2017-12-27 21:28:58 +00002665 /* If the padding was found to be invalid, padlen == 0
2666 * and the subtraction is safe. If the padding was found valid,
2667 * padlen hasn't been changed and the previous assertion
2668 * data_len >= padlen still holds. */
2669 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002671 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002673 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002679#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002681 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002685 * Authenticate if not done yet.
2686 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002688#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002689 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002690 {
Hanno Becker992b6872017-11-09 18:57:39 +00002691 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002692
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 /* If the initial value of padlen was such that
2694 * data_len < maclen + padlen + 1, then padlen
2695 * got reset to 1, and the initial check
2696 * data_len >= minlen + maclen + 1
2697 * guarantees that at this point we still
2698 * have at least data_len >= maclen.
2699 *
2700 * If the initial value of padlen was such that
2701 * data_len >= maclen + padlen + 1, then we have
2702 * subtracted either padlen + 1 (if the padding was correct)
2703 * or 0 (if the padding was incorrect) since then,
2704 * hence data_len >= maclen in any case.
2705 */
2706 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Beckere83efe62019-04-29 13:52:53 +01002708 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002711 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002713 ssl_mac( &transform->md_ctx_dec,
2714 transform->mac_dec,
2715 data, rec->data_len,
2716 rec->ctr, rec->type,
2717 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 }
2719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2722 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002723 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 {
2725 /*
2726 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002727 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002728 *
2729 * Known timing attacks:
2730 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2731 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002732 * To compensate for different timings for the MAC calculation
2733 * depending on how much padding was removed (which is determined
2734 * by padlen), process extra_run more blocks through the hash
2735 * function.
2736 *
2737 * The formula in the paper is
2738 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2739 * where L1 is the size of the header plus the decrypted message
2740 * plus CBC padding and L2 is the size of the header plus the
2741 * decrypted message. This is for an underlying hash function
2742 * with 64-byte blocks.
2743 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2744 * correctly. We round down instead of up, so -56 is the correct
2745 * value for our calculations instead of -55.
2746 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002747 * Repeat the formula rather than defining a block_size variable.
2748 * This avoids requiring division by a variable at runtime
2749 * (which would be marginally less efficient and would require
2750 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002751 */
2752 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002754
2755 /*
2756 * The next two sizes are the minimum and maximum values of
2757 * in_msglen over all padlen values.
2758 *
2759 * They're independent of padlen, since we previously did
2760 * in_msglen -= padlen.
2761 *
2762 * Note that max_len + maclen is never more than the buffer
2763 * length, as we previously did in_msglen -= maclen too.
2764 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002765 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002766 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2767
Hanno Becker4c6876b2017-12-27 21:28:58 +00002768 memset( tmp, 0, sizeof( tmp ) );
2769
2770 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002771 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002772#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2773 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 case MBEDTLS_MD_MD5:
2775 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002776 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 extra_run =
2779 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2780 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 break;
2782#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002783#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002786 extra_run =
2787 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2788 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002789 break;
2790#endif
2791 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2794 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002795
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002796 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Beckere83efe62019-04-29 13:52:53 +01002798 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2799 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002800 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2801 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002802 /* Make sure we access everything even when padlen > 0. This
2803 * makes the synchronisation requirements for just-in-time
2804 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002805 ssl_read_memory( data + rec->data_len, padlen );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002807
2808 /* Call mbedtls_md_process at least once due to cache attacks
2809 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002810 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002812
Hanno Becker4c6876b2017-12-27 21:28:58 +00002813 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Make sure we access all the memory that could contain the MAC,
2816 * before we check it in the next code block. This makes the
2817 * synchronisation requirements for just-in-time Prime+Probe
2818 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 ssl_read_memory( data + min_len,
2820 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 }
2822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2824 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002830#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2832 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker4c6876b2017-12-27 21:28:58 +00002835 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2836 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_DEBUG_ALL)
2839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002840#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002841 correct = 0;
2842 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002845
2846 /*
2847 * Finally check the correct flag
2848 */
2849 if( correct == 0 )
2850 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852
2853 /* Make extra sure authentication was performed, exactly once */
2854 if( auth_done != 1 )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002861 if( rec->cid_len != 0 )
2862 {
2863 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2864 &rec->type );
2865 if( ret != 0 )
2866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002875#undef MAC_NONE
2876#undef MAC_PLAINTEXT
2877#undef MAC_CIPHERTEXT
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880/*
2881 * Compression/decompression functions
2882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884{
2885 int ret;
2886 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002887 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002889 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002893 if( len_pre == 0 )
2894 return( 0 );
2895
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896 memcpy( msg_pre, ssl->out_msg, len_pre );
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 ssl->out_msglen ) );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msg, ssl->out_msglen );
2903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2905 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2906 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910 if( ret != Z_OK )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2913 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 }
2915
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002917 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 ssl->out_msglen ) );
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 ssl->out_msg, ssl->out_msglen );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926
2927 return( 0 );
2928}
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931{
2932 int ret;
2933 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002934 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002936 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002940 if( len_pre == 0 )
2941 return( 0 );
2942
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 memcpy( msg_pre, ssl->in_msg, len_pre );
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946 ssl->in_msglen ) );
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 ssl->in_msg, ssl->in_msglen );
2950
Paul Bakker48916f92012-09-16 19:57:18 +00002951 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2952 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2953 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002954 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002955 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 if( ret != Z_OK )
2959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2961 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962 }
2963
Angus Grattond8213d02016-05-25 20:56:48 +10002964 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002965 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 ssl->in_msglen ) );
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 ssl->in_msg, ssl->in_msglen );
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974
2975 return( 0 );
2976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2980static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_PROTO_DTLS)
2983static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002984{
2985 /* If renegotiation is not enforced, retransmit until we would reach max
2986 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002987 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990 unsigned char doublings = 1;
2991
2992 while( ratio != 0 )
2993 {
2994 ++doublings;
2995 ratio >>= 1;
2996 }
2997
2998 if( ++ssl->renego_records_seen > doublings )
2999 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003001 return( 0 );
3002 }
3003 }
3004
3005 return( ssl_write_hello_request( ssl ) );
3006}
3007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003011 * Fill the input message buffer by appending data to it.
3012 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003013 *
3014 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3015 * available (from this read and/or a previous one). Otherwise, an error code
3016 * is returned (possibly EOF or WANT_READ).
3017 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3019 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3020 * since we always read a whole datagram at once.
3021 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003022 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003023 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026{
Paul Bakker23986e52011-04-24 08:57:21 +00003027 int ret;
3028 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003032 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003035 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003037 }
3038
Angus Grattond8213d02016-05-25 20:56:48 +10003039 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003043 }
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003046 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003048 uint32_t timeout;
3049
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003050 /* Just to be sure */
3051 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3052 {
3053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3054 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3056 }
3057
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 /*
3059 * The point is, we need to always read a full datagram at once, so we
3060 * sometimes read more then requested, and handle the additional data.
3061 * It could be the rest of the current record (while fetching the
3062 * header) and/or some other records in the same datagram.
3063 */
3064
3065 /*
3066 * Move to the next record in the already read datagram if applicable
3067 */
3068 if( ssl->next_record_offset != 0 )
3069 {
3070 if( ssl->in_left < ssl->next_record_offset )
3071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 }
3075
3076 ssl->in_left -= ssl->next_record_offset;
3077
3078 if( ssl->in_left != 0 )
3079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 ssl->next_record_offset ) );
3082 memmove( ssl->in_hdr,
3083 ssl->in_hdr + ssl->next_record_offset,
3084 ssl->in_left );
3085 }
3086
3087 ssl->next_record_offset = 0;
3088 }
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003092
3093 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003094 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 */
3096 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
3103 * A record can't be split accross datagrams. If we need to read but
3104 * are not at the beginning of a new record, the caller did something
3105 * wrong.
3106 */
3107 if( ssl->in_left != 0 )
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003111 }
3112
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 /*
3114 * Don't even try to read if time's out already.
3115 * This avoids by-passing the timer when repeatedly receiving messages
3116 * that will end up being dropped.
3117 */
3118 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003119 {
3120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003121 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003122 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003123 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003124 {
Angus Grattond8213d02016-05-25 20:56:48 +10003125 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 timeout = ssl->handshake->retransmit_timeout;
3129 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003130 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003134 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3136 timeout );
3137 else
3138 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141
3142 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 }
3145
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003146 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003149 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003153 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003156 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 return( ret );
3163 }
3164
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003168 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003170 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003171 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003174 return( ret );
3175 }
3176
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003177 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 }
3181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ret < 0 )
3183 return( ret );
3184
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003185 ssl->in_left = ret;
3186 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003187 MBEDTLS_SSL_TRANSPORT_ELSE
3188#endif /* MBEDTLS_SSL_PROTO_DTLS */
3189#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003192 ssl->in_left, nb_want ) );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 while( ssl->in_left < nb_want )
3195 {
3196 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003197
3198 if( ssl_check_timer( ssl ) != 0 )
3199 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3200 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 {
3202 if( ssl->f_recv_timeout != NULL )
3203 {
3204 ret = ssl->f_recv_timeout( ssl->p_bio,
3205 ssl->in_hdr + ssl->in_left, len,
3206 ssl->conf->read_timeout );
3207 }
3208 else
3209 {
3210 ret = ssl->f_recv( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len );
3212 }
3213 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003216 ssl->in_left, nb_want ) );
3217 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218
3219 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221
3222 if( ret < 0 )
3223 return( ret );
3224
mohammad160352aecb92018-03-28 23:41:40 -07003225 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003226 {
Darryl Green11999bb2018-03-13 15:22:58 +00003227 MBEDTLS_SSL_DEBUG_MSG( 1,
3228 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003229 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3231 }
3232
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003233 ssl->in_left += ret;
3234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003236#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 return( 0 );
3241}
3242
3243/*
3244 * Flush any data not yet written
3245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003247{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003248 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003249 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003253 if( ssl->f_send == NULL )
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003256 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 }
3259
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003260 /* Avoid incrementing counter if data is flushed */
3261 if( ssl->out_left == 0 )
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003264 return( 0 );
3265 }
3266
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 while( ssl->out_left > 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003270 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003273 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 if( ret <= 0 )
3278 return( ret );
3279
mohammad160352aecb92018-03-28 23:41:40 -07003280 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003281 {
Darryl Green11999bb2018-03-13 15:22:58 +00003282 MBEDTLS_SSL_DEBUG_MSG( 1,
3283 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003284 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3286 }
3287
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 ssl->out_left -= ret;
3289 }
3290
Hanno Becker2b1e3542018-08-06 11:19:13 +01003291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003292 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003293 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003294 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003295 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003296 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003299 {
3300 ssl->out_hdr = ssl->out_buf + 8;
3301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 return( 0 );
3308}
3309
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310/*
3311 * Functions to handle the DTLS retransmission state machine
3312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314/*
3315 * Append current handshake message to current outgoing flight
3316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3321 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3322 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323
3324 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003325 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003329 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 }
3331
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
3339 /* Copy current handshake message with headers */
3340 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3341 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003342 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 msg->next = NULL;
3344
3345 /* Append to the current flight */
3346 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003347 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348 else
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351 while( cur->next != NULL )
3352 cur = cur->next;
3353 cur->next = msg;
3354 }
3355
Hanno Becker3b235902018-08-06 09:54:53 +01003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 return( 0 );
3358}
3359
3360/*
3361 * Free the current flight of handshake messages
3362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ssl_flight_item *cur = flight;
3366 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367
3368 while( cur != NULL )
3369 {
3370 next = cur->next;
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cur->p );
3373 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 cur = next;
3376 }
3377}
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3380static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003381#endif
3382
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003384 * Swap transform_out and out_ctr with the alternative ones
3385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003389 unsigned char tmp_out_ctr[8];
3390
3391 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394 return;
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 tmp_transform = ssl->transform_out;
3401 ssl->transform_out = ssl->handshake->alt_transform_out;
3402 ssl->handshake->alt_transform_out = tmp_transform;
3403
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003405 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3406 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408
3409 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003410 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3413 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003419 }
3420 }
3421#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003422}
3423
3424/*
3425 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 */
3427int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3428{
3429 int ret = 0;
3430
3431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3432
3433 ret = mbedtls_ssl_flight_transmit( ssl );
3434
3435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3436
3437 return( ret );
3438}
3439
3440/*
3441 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003442 *
3443 * Need to remember the current message in case flush_output returns
3444 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003447int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003453 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003455
3456 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003457 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003458 ssl_swap_epochs( ssl );
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003462
3463 while( ssl->handshake->cur_msg != NULL )
3464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003467
Hanno Beckere1dcb032018-08-17 16:47:58 +01003468 int const is_finished =
3469 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3470 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3471
Hanno Becker04da1892018-08-14 13:22:10 +01003472 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3473 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3474
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003475 /* Swap epochs before sending Finished: we can't do it after
3476 * sending ChangeCipherSpec, in case write returns WANT_READ.
3477 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003478 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003479 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 ssl_swap_epochs( ssl );
3482 }
3483
Hanno Becker67bc7c32018-08-06 11:33:50 +01003484 ret = ssl_get_remaining_payload_in_datagram( ssl );
3485 if( ret < 0 )
3486 return( ret );
3487 max_frag_len = (size_t) ret;
3488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* CCS is copied as is, while HS messages may need fragmentation */
3490 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3491 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492 if( max_frag_len == 0 )
3493 {
3494 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3495 return( ret );
3496
3497 continue;
3498 }
3499
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003500 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003502 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003504 /* Update position inside current message */
3505 ssl->handshake->cur_msg_p += cur->len;
3506 }
3507 else
3508 {
3509 const unsigned char * const p = ssl->handshake->cur_msg_p;
3510 const size_t hs_len = cur->len - 12;
3511 const size_t frag_off = p - ( cur->p + 12 );
3512 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003513 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Hanno Beckere1dcb032018-08-17 16:47:58 +01003515 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003516 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003517 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3521 return( ret );
3522
3523 continue;
3524 }
3525 max_hs_frag_len = max_frag_len - 12;
3526
3527 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3528 max_hs_frag_len : rem_len;
3529
3530 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003531 {
3532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533 (unsigned) cur_hs_frag_len,
3534 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003535 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003536
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 /* Messages are stored with handshake headers as if not fragmented,
3538 * copy beginning of headers then fill fragmentation fields.
3539 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3540 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003542 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3543 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3544 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3547 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3548 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549
3550 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3551
Hanno Becker3f7b9732018-08-28 09:53:25 +01003552 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3554 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555 ssl->out_msgtype = cur->type;
3556
3557 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003559 }
3560
3561 /* If done with the current message move to the next one if any */
3562 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3563 {
3564 if( cur->next != NULL )
3565 {
3566 ssl->handshake->cur_msg = cur->next;
3567 ssl->handshake->cur_msg_p = cur->next->p + 12;
3568 }
3569 else
3570 {
3571 ssl->handshake->cur_msg = NULL;
3572 ssl->handshake->cur_msg_p = NULL;
3573 }
3574 }
3575
3576 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003577 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 return( ret );
3581 }
3582 }
3583
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3585 return( ret );
3586
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003587 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3589 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003590 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3594 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003595
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597
3598 return( 0 );
3599}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600
3601/*
3602 * To be called when the last message of an incoming flight is received.
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605{
3606 /* We won't need to resend that one any more */
3607 ssl_flight_free( ssl->handshake->flight );
3608 ssl->handshake->flight = NULL;
3609 ssl->handshake->cur_msg = NULL;
3610
3611 /* The next incoming flight will start with this msg_seq */
3612 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003614 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003615 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003616
Hanno Becker0271f962018-08-16 13:23:47 +01003617 /* Clear future message buffering structure. */
3618 ssl_buffering_free( ssl );
3619
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003620 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621 ssl_set_timer( ssl, 0 );
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3624 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003627 }
3628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003630}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003631
3632/*
3633 * To be called when the last message of an outgoing flight is send.
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003636{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003637 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003638 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3641 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003644 }
3645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
Paul Bakker5121ce52009-01-03 21:22:43 +00003650/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653
3654/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003655 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656 *
3657 * - fill in handshake headers
3658 * - update handshake checksum
3659 * - DTLS: save message for resending
3660 * - then pass to the record layer
3661 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * DTLS: except for HelloRequest, messages are only queued, and will only be
3663 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003664 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 * Inputs:
3666 * - ssl->out_msglen: 4 + actual handshake message len
3667 * (4 is the size of handshake headers for TLS)
3668 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3669 * - ssl->out_msg + 4: the handshake message body
3670 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003671 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * - ssl->out_msglen: the length of the record contents
3673 * (including handshake headers but excluding record headers)
3674 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003677{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003678 int ret;
3679 const size_t hs_len = ssl->out_msglen - 4;
3680 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3683
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 /*
3685 * Sanity checks
3686 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003687 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3689 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003690 /* In SSLv3, the client might send a NoCertificate alert. */
3691#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3692 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3693 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3694 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3695#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3696 {
3697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3699 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003702 /* Whenever we send anything different from a
3703 * HelloRequest we should be in a handshake - double check. */
3704 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3705 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706 ssl->handshake == NULL )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003721
Hanno Beckerb50a2532018-08-06 11:52:54 +01003722 /* Double-check that we did not exceed the bounds
3723 * of the outgoing record buffer.
3724 * This should never fail as the various message
3725 * writing functions must obey the bounds of the
3726 * outgoing record buffer, but better be safe.
3727 *
3728 * Note: We deliberately do not check for the MTU or MFL here.
3729 */
3730 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3731 {
3732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3733 "size %u, maximum %u",
3734 (unsigned) ssl->out_msglen,
3735 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3737 }
3738
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 /*
3740 * Fill handshake headers
3741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3745 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3746 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003748 /*
3749 * DTLS has additional fields in the Handshake layer,
3750 * between the length field and the actual payload:
3751 * uint16 message_seq;
3752 * uint24 fragment_offset;
3753 * uint24 fragment_length;
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003758 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003759 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003760 {
3761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3762 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003763 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003764 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3766 }
3767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003769 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003771 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003773 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003774 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3775 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3776 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 }
3778 else
3779 {
3780 ssl->out_msg[4] = 0;
3781 ssl->out_msg[5] = 0;
3782 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003783
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784 /* Handshake hashes are computed without fragmentation,
3785 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003786 memset( ssl->out_msg + 6, 0x00, 3 );
3787 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003790
Hanno Becker0207e532018-08-28 10:28:28 +01003791 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003792 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3793 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003798 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003799 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3800 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 {
3802 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 return( ret );
3806 }
3807 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003808 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003809#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003810 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003812 {
3813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003817
3818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3819
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003820 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821}
3822
3823/*
3824 * Record layer functions
3825 */
3826
3827/*
3828 * Write current record.
3829 *
3830 * Uses:
3831 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3832 * - ssl->out_msglen: length of the record content (excl headers)
3833 * - ssl->out_msg: record content
3834 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003836{
3837 int ret, done = 0;
3838 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840
3841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003846 {
3847 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003850 return( ret );
3851 }
3852
3853 len = ssl->out_msglen;
3854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3858 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 ret = mbedtls_ssl_hw_record_write( ssl );
3863 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003867 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003868
3869 if( ret == 0 )
3870 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 if( !done )
3874 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003875 unsigned i;
3876 size_t protected_record_size;
3877
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003878 /* Skip writing the record content type to after the encryption,
3879 * as it may change when using the CID extension. */
3880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003883
Hanno Becker19859472018-08-06 09:40:20 +01003884 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003885 ssl->out_len[0] = (unsigned char)( len >> 8 );
3886 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887
Paul Bakker48916f92012-09-16 19:57:18 +00003888 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003889 {
Hanno Becker3307b532017-12-27 21:37:21 +00003890 mbedtls_record rec;
3891
3892 rec.buf = ssl->out_iv;
3893 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3894 ( ssl->out_iv - ssl->out_buf );
3895 rec.data_len = ssl->out_msglen;
3896 rec.data_offset = ssl->out_msg - rec.buf;
3897
3898 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3899 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3900 ssl->conf->transport, rec.ver );
3901 rec.type = ssl->out_msgtype;
3902
Hanno Beckera5a2b082019-05-15 14:03:01 +01003903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003904 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003905 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003907
Hanno Becker611a83b2018-01-03 14:27:32 +00003908 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003909 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912 return( ret );
3913 }
3914
Hanno Becker3307b532017-12-27 21:37:21 +00003915 if( rec.data_offset != 0 )
3916 {
3917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3919 }
3920
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003921 /* Update the record content type and CID. */
3922 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003924 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003926 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003927 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3928 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
3930
Hanno Becker43395762019-05-03 14:46:38 +01003931 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003932
3933#if defined(MBEDTLS_SSL_PROTO_DTLS)
3934 /* In case of DTLS, double-check that we don't exceed
3935 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003936 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003937 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003938 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939 if( ret < 0 )
3940 return( ret );
3941
3942 if( protected_record_size > (size_t) ret )
3943 {
3944 /* Should never happen */
3945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3946 }
3947 }
3948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003949
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003950 /* Now write the potentially updated record content type. */
3951 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003954 "version = [%d:%d], msglen = %d",
3955 ssl->out_hdr[0], ssl->out_hdr[1],
3956 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003959 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003960
3961 ssl->out_left += protected_record_size;
3962 ssl->out_hdr += protected_record_size;
3963 ssl_update_out_pointers( ssl, ssl->transform_out );
3964
Hanno Becker04484622018-08-06 09:49:38 +01003965 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3966 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3967 break;
3968
3969 /* The loop goes to its end iff the counter is wrapping */
3970 if( i == ssl_ep_len( ssl ) )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3973 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003978 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003979 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003981 size_t remaining;
3982 ret = ssl_get_remaining_payload_in_datagram( ssl );
3983 if( ret < 0 )
3984 {
3985 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3986 ret );
3987 return( ret );
3988 }
3989
3990 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003992 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003994 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003995 else
3996 {
Hanno Becker513815a2018-08-20 11:56:09 +01003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 }
3999 }
4000#endif /* MBEDTLS_SSL_PROTO_DTLS */
4001
4002 if( ( flush == SSL_FORCE_FLUSH ) &&
4003 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 return( ret );
4007 }
4008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
4011 return( 0 );
4012}
4013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004015
4016static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4017{
4018 if( ssl->in_msglen < ssl->in_hslen ||
4019 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4020 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4021 {
4022 return( 1 );
4023 }
4024 return( 0 );
4025}
Hanno Becker44650b72018-08-16 12:51:11 +01004026
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004027static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004028{
4029 return( ( ssl->in_msg[9] << 16 ) |
4030 ( ssl->in_msg[10] << 8 ) |
4031 ssl->in_msg[11] );
4032}
4033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[6] << 16 ) |
4037 ( ssl->in_msg[7] << 8 ) |
4038 ssl->in_msg[8] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 uint32_t msg_len, frag_off, frag_len;
4044
4045 msg_len = ssl_get_hs_total_len( ssl );
4046 frag_off = ssl_get_hs_frag_off( ssl );
4047 frag_len = ssl_get_hs_frag_len( ssl );
4048
4049 if( frag_off > msg_len )
4050 return( -1 );
4051
4052 if( frag_len > msg_len - frag_off )
4053 return( -1 );
4054
4055 if( frag_len + 12 > ssl->in_msglen )
4056 return( -1 );
4057
4058 return( 0 );
4059}
4060
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004061/*
4062 * Mark bits in bitmask (used for DTLS HS reassembly)
4063 */
4064static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4065{
4066 unsigned int start_bits, end_bits;
4067
4068 start_bits = 8 - ( offset % 8 );
4069 if( start_bits != 8 )
4070 {
4071 size_t first_byte_idx = offset / 8;
4072
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004073 /* Special case */
4074 if( len <= start_bits )
4075 {
4076 for( ; len != 0; len-- )
4077 mask[first_byte_idx] |= 1 << ( start_bits - len );
4078
4079 /* Avoid potential issues with offset or len becoming invalid */
4080 return;
4081 }
4082
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004083 offset += start_bits; /* Now offset % 8 == 0 */
4084 len -= start_bits;
4085
4086 for( ; start_bits != 0; start_bits-- )
4087 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4088 }
4089
4090 end_bits = len % 8;
4091 if( end_bits != 0 )
4092 {
4093 size_t last_byte_idx = ( offset + len ) / 8;
4094
4095 len -= end_bits; /* Now len % 8 == 0 */
4096
4097 for( ; end_bits != 0; end_bits-- )
4098 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4099 }
4100
4101 memset( mask + offset / 8, 0xFF, len / 8 );
4102}
4103
4104/*
4105 * Check that bitmask is full
4106 */
4107static int ssl_bitmask_check( unsigned char *mask, size_t len )
4108{
4109 size_t i;
4110
4111 for( i = 0; i < len / 8; i++ )
4112 if( mask[i] != 0xFF )
4113 return( -1 );
4114
4115 for( i = 0; i < len % 8; i++ )
4116 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4117 return( -1 );
4118
4119 return( 0 );
4120}
4121
Hanno Becker56e205e2018-08-16 09:06:12 +01004122/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004123static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004124 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004125{
Hanno Becker56e205e2018-08-16 09:06:12 +01004126 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128 alloc_len = 12; /* Handshake header */
4129 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004130
Hanno Beckerd07df862018-08-16 09:14:58 +01004131 if( add_bitmap )
4132 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004134 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004135}
Hanno Becker56e205e2018-08-16 09:06:12 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004138
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004139static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004140{
4141 return( ( ssl->in_msg[1] << 16 ) |
4142 ( ssl->in_msg[2] << 8 ) |
4143 ssl->in_msg[3] );
4144}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004145
Simon Butcher99000142016-10-13 17:21:01 +01004146int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004151 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004153 }
4154
Hanno Becker12555c62018-08-16 12:47:53 +01004155 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004159 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004164 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004165 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004166
Hanno Becker44650b72018-08-16 12:51:11 +01004167 if( ssl_check_hs_header( ssl ) != 0 )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4171 }
4172
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004173 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004174 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4175 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4176 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4177 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004178 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004179 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4182 recv_msg_seq,
4183 ssl->handshake->in_msg_seq ) );
4184 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4185 }
4186
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004187 /* Retransmit only on last message from previous flight, to avoid
4188 * too many retransmissions.
4189 * Besides, No sane server ever retransmits HelloVerifyRequest */
4190 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004194 "message_seq = %d, start_of_flight = %d",
4195 recv_msg_seq,
4196 ssl->handshake->in_flight_start_seq ) );
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 return( ret );
4202 }
4203 }
4204 else
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 "message_seq = %d, expected = %d",
4208 recv_msg_seq,
4209 ssl->handshake->in_msg_seq ) );
4210 }
4211
Hanno Becker90333da2017-10-10 11:27:13 +01004212 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004213 }
4214 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004215
Hanno Becker6d97ef52018-08-16 13:09:04 +01004216 /* Message reassembly is handled alongside buffering of future
4217 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004218 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004219 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004220 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224 }
4225 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004226 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004228#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004229 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004230 /* With TLS we don't handle fragmentation (for now) */
4231 if( ssl->in_msglen < ssl->in_hslen )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4234 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4235 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004236 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004237#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004238
Simon Butcher99000142016-10-13 17:21:01 +01004239 return( 0 );
4240}
4241
4242void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4243{
Hanno Becker0271f962018-08-16 13:23:47 +01004244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004245
Hanno Becker0271f962018-08-16 13:23:47 +01004246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004247 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004248 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004249 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004250
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004253 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004254 ssl->handshake != NULL )
4255 {
Hanno Becker0271f962018-08-16 13:23:47 +01004256 unsigned offset;
4257 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004258
Hanno Becker0271f962018-08-16 13:23:47 +01004259 /* Increment handshake sequence number */
4260 hs->in_msg_seq++;
4261
4262 /*
4263 * Clear up handshake buffering and reassembly structure.
4264 */
4265
4266 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004267 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004268
4269 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004270 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4271 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004272 offset++, hs_buf++ )
4273 {
4274 *hs_buf = *(hs_buf + 1);
4275 }
4276
4277 /* Create a fresh last entry */
4278 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004279 }
4280#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281}
4282
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004284 * DTLS anti-replay: RFC 6347 4.1.2.6
4285 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004286 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4287 * Bit n is set iff record number in_window_top - n has been seen.
4288 *
4289 * Usually, in_window_top is the last record number seen and the lsb of
4290 * in_window is set. The only exception is the initial state (record number 0
4291 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4294static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004295{
4296 ssl->in_window_top = 0;
4297 ssl->in_window = 0;
4298}
4299
4300static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4301{
4302 return( ( (uint64_t) buf[0] << 40 ) |
4303 ( (uint64_t) buf[1] << 32 ) |
4304 ( (uint64_t) buf[2] << 24 ) |
4305 ( (uint64_t) buf[3] << 16 ) |
4306 ( (uint64_t) buf[4] << 8 ) |
4307 ( (uint64_t) buf[5] ) );
4308}
4309
4310/*
4311 * Return 0 if sequence number is acceptable, -1 otherwise
4312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004314{
4315 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4316 uint64_t bit;
4317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004319 return( 0 );
4320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321 if( rec_seqnum > ssl->in_window_top )
4322 return( 0 );
4323
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004324 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004325
4326 if( bit >= 64 )
4327 return( -1 );
4328
4329 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4330 return( -1 );
4331
4332 return( 0 );
4333}
4334
4335/*
4336 * Update replay window on new validated record
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004342 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004343 return;
4344
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 if( rec_seqnum > ssl->in_window_top )
4346 {
4347 /* Update window_top and the contents of the window */
4348 uint64_t shift = rec_seqnum - ssl->in_window_top;
4349
4350 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004351 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004353 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004355 ssl->in_window |= 1;
4356 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004357
4358 ssl->in_window_top = rec_seqnum;
4359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 else
4361 {
4362 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004363 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 if( bit < 64 ) /* Always true, but be extra sure */
4366 ssl->in_window |= (uint64_t) 1 << bit;
4367 }
4368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004372/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004373static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4374
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004375/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004376 * Without any SSL context, check if a datagram looks like a ClientHello with
4377 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004378 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379 *
4380 * - if cookie is valid, return 0
4381 * - if ClientHello looks superficially valid but cookie is not,
4382 * fill obuf and set olen, then
4383 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4384 * - otherwise return a specific error code
4385 */
4386static int ssl_check_dtls_clihlo_cookie(
4387 mbedtls_ssl_cookie_write_t *f_cookie_write,
4388 mbedtls_ssl_cookie_check_t *f_cookie_check,
4389 void *p_cookie,
4390 const unsigned char *cli_id, size_t cli_id_len,
4391 const unsigned char *in, size_t in_len,
4392 unsigned char *obuf, size_t buf_len, size_t *olen )
4393{
4394 size_t sid_len, cookie_len;
4395 unsigned char *p;
4396
4397 if( f_cookie_write == NULL || f_cookie_check == NULL )
4398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4399
4400 /*
4401 * Structure of ClientHello with record and handshake headers,
4402 * and expected values. We don't need to check a lot, more checks will be
4403 * done when actually parsing the ClientHello - skipping those checks
4404 * avoids code duplication and does not make cookie forging any easier.
4405 *
4406 * 0-0 ContentType type; copied, must be handshake
4407 * 1-2 ProtocolVersion version; copied
4408 * 3-4 uint16 epoch; copied, must be 0
4409 * 5-10 uint48 sequence_number; copied
4410 * 11-12 uint16 length; (ignored)
4411 *
4412 * 13-13 HandshakeType msg_type; (ignored)
4413 * 14-16 uint24 length; (ignored)
4414 * 17-18 uint16 message_seq; copied
4415 * 19-21 uint24 fragment_offset; copied, must be 0
4416 * 22-24 uint24 fragment_length; (ignored)
4417 *
4418 * 25-26 ProtocolVersion client_version; (ignored)
4419 * 27-58 Random random; (ignored)
4420 * 59-xx SessionID session_id; 1 byte len + sid_len content
4421 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4422 * ...
4423 *
4424 * Minimum length is 61 bytes.
4425 */
4426 if( in_len < 61 ||
4427 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4428 in[3] != 0 || in[4] != 0 ||
4429 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4430 {
4431 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4432 }
4433
4434 sid_len = in[59];
4435 if( sid_len > in_len - 61 )
4436 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4437
4438 cookie_len = in[60 + sid_len];
4439 if( cookie_len > in_len - 60 )
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441
4442 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4443 cli_id, cli_id_len ) == 0 )
4444 {
4445 /* Valid cookie */
4446 return( 0 );
4447 }
4448
4449 /*
4450 * If we get here, we've got an invalid cookie, let's prepare HVR.
4451 *
4452 * 0-0 ContentType type; copied
4453 * 1-2 ProtocolVersion version; copied
4454 * 3-4 uint16 epoch; copied
4455 * 5-10 uint48 sequence_number; copied
4456 * 11-12 uint16 length; olen - 13
4457 *
4458 * 13-13 HandshakeType msg_type; hello_verify_request
4459 * 14-16 uint24 length; olen - 25
4460 * 17-18 uint16 message_seq; copied
4461 * 19-21 uint24 fragment_offset; copied
4462 * 22-24 uint24 fragment_length; olen - 25
4463 *
4464 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4465 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4466 *
4467 * Minimum length is 28.
4468 */
4469 if( buf_len < 28 )
4470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4471
4472 /* Copy most fields and adapt others */
4473 memcpy( obuf, in, 25 );
4474 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4475 obuf[25] = 0xfe;
4476 obuf[26] = 0xff;
4477
4478 /* Generate and write actual cookie */
4479 p = obuf + 28;
4480 if( f_cookie_write( p_cookie,
4481 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4482 {
4483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4484 }
4485
4486 *olen = p - obuf;
4487
4488 /* Go back and fill length fields */
4489 obuf[27] = (unsigned char)( *olen - 28 );
4490
4491 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4492 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4493 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4494
4495 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4496 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4497
4498 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4499}
4500
4501/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004502 * Handle possible client reconnect with the same UDP quadruplet
4503 * (RFC 6347 Section 4.2.8).
4504 *
4505 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4506 * that looks like a ClientHello.
4507 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * - if the input looks like a ClientHello without cookies,
4509 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * - if the input looks like a ClientHello with a valid cookie,
4512 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004513 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004514 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004517 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4518 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004519 */
4520static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4521{
4522 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 ret = ssl_check_dtls_clihlo_cookie(
4526 ssl->conf->f_cookie_write,
4527 ssl->conf->f_cookie_check,
4528 ssl->conf->p_cookie,
4529 ssl->cli_id, ssl->cli_id_len,
4530 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004531 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004533 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4534
4535 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004536 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004537 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004538 * If the error is permanent we'll catch it later,
4539 * if it's not, then hopefully it'll work next time. */
4540 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4541
4542 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 }
4544
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 /* Got a valid cookie, partially reset context */
4548 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4549 {
4550 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4551 return( ret );
4552 }
4553
4554 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 }
4556
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004557 return( ret );
4558}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004559#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004560
Hanno Becker46483f12019-05-03 13:25:54 +01004561static int ssl_check_record_type( uint8_t record_type )
4562{
4563 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4564 record_type != MBEDTLS_SSL_MSG_ALERT &&
4565 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4566 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4567 {
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
4571 return( 0 );
4572}
4573
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004574/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004575 * ContentType type;
4576 * ProtocolVersion version;
4577 * uint16 epoch; // DTLS only
4578 * uint48 sequence_number; // DTLS only
4579 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004580 *
4581 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004582 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004583 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4584 *
4585 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004586 * 1. proceed with the record if this function returns 0
4587 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4588 * 3. return CLIENT_RECONNECT if this function return that value
4589 * 4. drop the whole datagram if this function returns anything else.
4590 * Point 2 is needed when the peer is resending, and we have already received
4591 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004595 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004597
Hanno Becker8b09b732019-05-08 12:03:28 +01004598 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004599
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004601 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004603 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004606 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4607 ssl->conf->cid_len != 0 )
4608 {
4609 /* Shift pointers to account for record header including CID
4610 * struct {
4611 * ContentType special_type = tls12_cid;
4612 * ProtocolVersion version;
4613 * uint16 epoch;
4614 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004615 * opaque cid[cid_length]; // Additional field compared to
4616 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004617 * uint16 length;
4618 * opaque enc_content[DTLSCiphertext.length];
4619 * } DTLSCiphertext;
4620 */
4621
4622 /* So far, we only support static CID lengths
4623 * fixed in the configuration. */
4624 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4625 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4626 }
4627 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004628#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004629 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004632
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004633#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004634 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004635 * Section 4.1.2.7, that is, send alert only with TLS */
4636 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4637 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4639 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004640 }
4641#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004644 }
4645
4646 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004647 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004653 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Hanno Becker8b09b732019-05-08 12:03:28 +01004659 /* Now that the total length of the record header is known, ensure
4660 * that the current datagram is large enough to hold it.
4661 * This would fail, for example, if we received a datagram of
4662 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4663 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4664 if( ret != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4667 return( ret );
4668 }
4669 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4670
4671 /* Parse and validate record length
4672 * This must happen after the CID parsing because
4673 * its position in the record header depends on
4674 * the presence of a CID. */
4675
4676 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004677 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004678 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4681 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004682 }
4683
Hanno Becker8b09b732019-05-08 12:03:28 +01004684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004685 "version = [%d:%d], msglen = %d",
4686 ssl->in_msgtype,
4687 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004688
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004689 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004690 * DTLS-related tests.
4691 * Check epoch before checking length constraint because
4692 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4693 * message gets duplicated before the corresponding Finished message,
4694 * the second ChangeCipherSpec should be discarded because it belongs
4695 * to an old epoch, but not because its length is shorter than
4696 * the minimum record length for packets using the new record transform.
4697 * Note that these two kinds of failures are handled differently,
4698 * as an unexpected record is silently skipped but an invalid
4699 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004700 */
4701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004702 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004703 {
4704 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4705
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 /* Check epoch (and sequence number) with DTLS */
4707 if( rec_epoch != ssl->in_epoch )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4710 "expected %d, received %d",
4711 ssl->in_epoch, rec_epoch ) );
4712
4713#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4714 /*
4715 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4716 * access the first byte of record content (handshake type), as we
4717 * have an active transform (possibly iv_len != 0), so use the
4718 * fact that the record header len is 13 instead.
4719 */
4720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4721 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4722 rec_epoch == 0 &&
4723 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4724 ssl->in_left > 13 &&
4725 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4728 "from the same port" ) );
4729 return( ssl_handle_possible_reconnect( ssl ) );
4730 }
4731 else
4732#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004733 {
4734 /* Consider buffering the record. */
4735 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4738 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4739 }
4740
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004741 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004743 }
4744
4745#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4746 /* Replay detection only works for the current epoch */
4747 if( rec_epoch == ssl->in_epoch &&
4748 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4751 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4752 }
4753#endif
4754 }
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
Hanno Becker52c6dc62017-05-26 16:07:36 +01004757
4758 /* Check length against bounds of the current transform and version */
4759 if( ssl->transform_in == NULL )
4760 {
4761 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004762 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767 }
4768 else
4769 {
4770 if( ssl->in_msglen < ssl->transform_in->minlen )
4771 {
4772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4774 }
4775
4776#if defined(MBEDTLS_SSL_PROTO_SSL3)
4777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004778 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4781 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4782 }
4783#endif
4784#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4785 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4786 /*
4787 * TLS encrypted messages can have up to 256 bytes of padding
4788 */
4789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4790 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004791 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4794 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4795 }
4796#endif
4797 }
4798
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004799 return( 0 );
4800}
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802/*
4803 * If applicable, decrypt (and decompress) record content
4804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806{
4807 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004810 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4813 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 ret = mbedtls_ssl_hw_record_read( ssl );
4818 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004823
4824 if( ret == 0 )
4825 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004828 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004830 mbedtls_record rec;
4831
4832 rec.buf = ssl->in_iv;
4833 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4834 - ( ssl->in_iv - ssl->in_buf );
4835 rec.data_len = ssl->in_msglen;
4836 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004838 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004839 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004841
4842 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4844 ssl->conf->transport, rec.ver );
4845 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004846 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4847 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004850
Hanno Beckera5a2b082019-05-15 14:03:01 +01004851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004852 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4853 ssl->conf->ignore_unexpected_cid
4854 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4855 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004857 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004859#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004860
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 return( ret );
4862 }
4863
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004864 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004865 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004866 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4867 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 /* The record content type may change during decryption,
4871 * so re-read it. */
4872 ssl->in_msgtype = rec.type;
4873 /* Also update the input buffer, because unfortunately
4874 * the server-side ssl_parse_client_hello() reparses the
4875 * record header when receiving a ClientHello initiating
4876 * a renegotiation. */
4877 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004878 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004879 ssl->in_msglen = rec.data_len;
4880 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4881 ssl->in_len[1] = (unsigned char)( rec.data_len );
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4884 ssl->in_msg, ssl->in_msglen );
4885
Hanno Beckera5a2b082019-05-15 14:03:01 +01004886#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004887 /* We have already checked the record content type
4888 * in ssl_parse_record_header(), failing or silently
4889 * dropping the record in the case of an unknown type.
4890 *
4891 * Since with the use of CIDs, the record content type
4892 * might change during decryption, re-check the record
4893 * content type, but treat a failure as fatal this time. */
4894 if( ssl_check_record_type( ssl->in_msgtype ) )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4898 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004900
Angus Grattond8213d02016-05-25 20:56:48 +10004901 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004906 else if( ssl->in_msglen == 0 )
4907 {
4908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4910 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4911 {
4912 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4915 }
4916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4917
4918 ssl->nb_zero++;
4919
4920 /*
4921 * Three or more empty messages may be a DoS attack
4922 * (excessive CPU consumption).
4923 */
4924 if( ssl->nb_zero > 3 )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004927 "messages, possible DoS attack" ) );
4928 /* Treat the records as if they were not properly authenticated,
4929 * thereby failing the connection if we see more than allowed
4930 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4932 }
4933 }
4934 else
4935 ssl->nb_zero = 0;
4936
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4938#if defined(MBEDTLS_SSL_PROTO_TLS)
4939 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 {
4941 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004943 if( ++ssl->in_ctr[i - 1] != 0 )
4944 break;
4945
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004946 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004947 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004948 {
4949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4950 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4951 }
4952 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
4956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004958 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004960 {
4961 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004964 return( ret );
4965 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004970 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004973 }
4974#endif
4975
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004976 return( 0 );
4977}
4978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004980
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004981/*
4982 * Read a record.
4983 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004984 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4985 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4986 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987 */
Hanno Becker1097b342018-08-15 14:09:41 +01004988
4989/* Helper functions for mbedtls_ssl_read_record(). */
4990static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004991static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4992static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004993
Hanno Becker327c93b2018-08-15 13:56:18 +01004994int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004995 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996{
4997 int ret;
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005001 if( ssl->keep_current_message == 0 )
5002 {
5003 do {
Simon Butcher99000142016-10-13 17:21:01 +01005004
Hanno Becker26994592018-08-15 14:14:59 +01005005 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005006 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005008
Hanno Beckere74d5562018-08-15 14:26:08 +01005009 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 {
Hanno Becker40f50842018-08-15 14:48:01 +01005011#if defined(MBEDTLS_SSL_PROTO_DTLS)
5012 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005013
Hanno Becker40f50842018-08-15 14:48:01 +01005014 /* We only check for buffered messages if the
5015 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005016 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005017 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 {
Hanno Becker40f50842018-08-15 14:48:01 +01005019 if( ssl_load_buffered_message( ssl ) == 0 )
5020 have_buffered = 1;
5021 }
5022
5023 if( have_buffered == 0 )
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025 {
5026 ret = ssl_get_next_record( ssl );
5027 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5028 continue;
5029
5030 if( ret != 0 )
5031 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005032 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005033 return( ret );
5034 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005035 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 }
5037
5038 ret = mbedtls_ssl_handle_message_type( ssl );
5039
Hanno Becker40f50842018-08-15 14:48:01 +01005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
5041 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5042 {
5043 /* Buffer future message */
5044 ret = ssl_buffer_message( ssl );
5045 if( ret != 0 )
5046 return( ret );
5047
5048 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5049 }
5050#endif /* MBEDTLS_SSL_PROTO_DTLS */
5051
Hanno Becker90333da2017-10-10 11:27:13 +01005052 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5053 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005054
5055 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005056 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005057 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005058 return( ret );
5059 }
5060
Hanno Becker327c93b2018-08-15 13:56:18 +01005061 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005062 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063 {
5064 mbedtls_ssl_update_handshake_status( ssl );
5065 }
Simon Butcher99000142016-10-13 17:21:01 +01005066 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067 else
Simon Butcher99000142016-10-13 17:21:01 +01005068 {
Hanno Becker02f59072018-08-15 14:00:24 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005070 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005071 }
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5074
5075 return( 0 );
5076}
5077
Hanno Becker40f50842018-08-15 14:48:01 +01005078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005079static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005080{
Hanno Becker40f50842018-08-15 14:48:01 +01005081 if( ssl->in_left > ssl->next_record_offset )
5082 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005083
Hanno Becker40f50842018-08-15 14:48:01 +01005084 return( 0 );
5085}
5086
5087static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5088{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005091 int ret = 0;
5092
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 if( hs == NULL )
5094 return( -1 );
5095
Hanno Beckere00ae372018-08-20 09:39:42 +01005096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5097
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5099 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5100 {
5101 /* Check if we have seen a ChangeCipherSpec before.
5102 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005103 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5106 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005107 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005108 }
5109
Hanno Becker39b8bc92018-08-28 17:17:13 +01005110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5112 ssl->in_msglen = 1;
5113 ssl->in_msg[0] = 1;
5114
5115 /* As long as they are equal, the exact value doesn't matter. */
5116 ssl->in_left = 0;
5117 ssl->next_record_offset = 0;
5118
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005119 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 goto exit;
5121 }
Hanno Becker37f95322018-08-16 13:55:32 +01005122
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005124 /* Debug only */
5125 {
5126 unsigned offset;
5127 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5128 {
5129 hs_buf = &hs->buffering.hs[offset];
5130 if( hs_buf->is_valid == 1 )
5131 {
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5133 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005134 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005135 }
5136 }
5137 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005138#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005139
5140 /* Check if we have buffered and/or fully reassembled the
5141 * next handshake message. */
5142 hs_buf = &hs->buffering.hs[0];
5143 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5144 {
5145 /* Synthesize a record containing the buffered HS message. */
5146 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5147 ( hs_buf->data[2] << 8 ) |
5148 hs_buf->data[3];
5149
5150 /* Double-check that we haven't accidentally buffered
5151 * a message that doesn't fit into the input buffer. */
5152 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5156 }
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5159 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5160 hs_buf->data, msg_len + 12 );
5161
5162 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5163 ssl->in_hslen = msg_len + 12;
5164 ssl->in_msglen = msg_len + 12;
5165 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5166
5167 ret = 0;
5168 goto exit;
5169 }
5170 else
5171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5173 hs->in_msg_seq ) );
5174 }
5175
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 ret = -1;
5177
5178exit:
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5181 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005182}
5183
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5185 size_t desired )
5186{
5187 int offset;
5188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5190 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191
Hanno Becker01315ea2018-08-21 17:22:17 +01005192 /* Get rid of future records epoch first, if such exist. */
5193 ssl_free_buffered_record( ssl );
5194
5195 /* Check if we have enough space available now. */
5196 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5197 hs->buffering.total_bytes_buffered ) )
5198 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005200 return( 0 );
5201 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005202
Hanno Becker4f432ad2018-08-28 10:02:32 +01005203 /* We don't have enough space to buffer the next expected handshake
5204 * message. Remove buffers used for future messages to gain space,
5205 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005206 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5207 offset >= 0; offset-- )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5210 offset ) );
5211
Hanno Beckerb309b922018-08-23 13:18:05 +01005212 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005213
5214 /* Check if we have enough space available now. */
5215 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5216 hs->buffering.total_bytes_buffered ) )
5217 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219 return( 0 );
5220 }
5221 }
5222
5223 return( -1 );
5224}
5225
Hanno Becker40f50842018-08-15 14:48:01 +01005226static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5227{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005228 int ret = 0;
5229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5230
5231 if( hs == NULL )
5232 return( 0 );
5233
5234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5235
5236 switch( ssl->in_msgtype )
5237 {
5238 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005240
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005241 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005242 break;
5243
5244 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005245 {
5246 unsigned recv_msg_seq_offset;
5247 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5248 mbedtls_ssl_hs_buffer *hs_buf;
5249 size_t msg_len = ssl->in_hslen - 12;
5250
5251 /* We should never receive an old handshake
5252 * message - double-check nonetheless. */
5253 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5257 }
5258
5259 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5260 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5261 {
5262 /* Silently ignore -- message too far in the future */
5263 MBEDTLS_SSL_DEBUG_MSG( 2,
5264 ( "Ignore future HS message with sequence number %u, "
5265 "buffering window %u - %u",
5266 recv_msg_seq, ssl->handshake->in_msg_seq,
5267 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5268
5269 goto exit;
5270 }
5271
5272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5273 recv_msg_seq, recv_msg_seq_offset ) );
5274
5275 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5276
5277 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005278 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005279 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005280 size_t reassembly_buf_sz;
5281
Hanno Becker37f95322018-08-16 13:55:32 +01005282 hs_buf->is_fragmented =
5283 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5284
5285 /* We copy the message back into the input buffer
5286 * after reassembly, so check that it's not too large.
5287 * This is an implementation-specific limitation
5288 * and not one from the standard, hence it is not
5289 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005290 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005291 {
5292 /* Ignore message */
5293 goto exit;
5294 }
5295
Hanno Beckere0b150f2018-08-21 15:51:03 +01005296 /* Check if we have enough space to buffer the message. */
5297 if( hs->buffering.total_bytes_buffered >
5298 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5302 }
5303
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005304 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5305 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005306
5307 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5308 hs->buffering.total_bytes_buffered ) )
5309 {
5310 if( recv_msg_seq_offset > 0 )
5311 {
5312 /* If we can't buffer a future message because
5313 * of space limitations -- ignore. */
5314 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",
5315 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5316 (unsigned) hs->buffering.total_bytes_buffered ) );
5317 goto exit;
5318 }
Hanno Beckere1801392018-08-21 16:51:05 +01005319 else
5320 {
5321 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",
5322 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5323 (unsigned) hs->buffering.total_bytes_buffered ) );
5324 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005325
Hanno Beckera02b0b42018-08-21 17:20:27 +01005326 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005327 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005328 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",
5329 (unsigned) msg_len,
5330 (unsigned) reassembly_buf_sz,
5331 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5334 goto exit;
5335 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005336 }
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5339 msg_len ) );
5340
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005341 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5342 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005343 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005344 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005345 goto exit;
5346 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005347 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005348
5349 /* Prepare final header: copy msg_type, length and message_seq,
5350 * then add standardised fragment_offset and fragment_length */
5351 memcpy( hs_buf->data, ssl->in_msg, 6 );
5352 memset( hs_buf->data + 6, 0, 3 );
5353 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5354
5355 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356
5357 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005358 }
5359 else
5360 {
5361 /* Make sure msg_type and length are consistent */
5362 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5365 /* Ignore */
5366 goto exit;
5367 }
5368 }
5369
Hanno Becker4422bbb2018-08-20 09:40:19 +01005370 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005371 {
5372 size_t frag_len, frag_off;
5373 unsigned char * const msg = hs_buf->data + 12;
5374
5375 /*
5376 * Check and copy current fragment
5377 */
5378
5379 /* Validation of header fields already done in
5380 * mbedtls_ssl_prepare_handshake_record(). */
5381 frag_off = ssl_get_hs_frag_off( ssl );
5382 frag_len = ssl_get_hs_frag_len( ssl );
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5385 frag_off, frag_len ) );
5386 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5387
5388 if( hs_buf->is_fragmented )
5389 {
5390 unsigned char * const bitmask = msg + msg_len;
5391 ssl_bitmask_set( bitmask, frag_off, frag_len );
5392 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5393 msg_len ) == 0 );
5394 }
5395 else
5396 {
5397 hs_buf->is_complete = 1;
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5401 hs_buf->is_complete ? "" : "not yet " ) );
5402 }
5403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005404 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005405 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005406
5407 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005408 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005409 break;
5410 }
5411
5412exit:
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5415 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005416}
5417#endif /* MBEDTLS_SSL_PROTO_DTLS */
5418
Hanno Becker1097b342018-08-15 14:09:41 +01005419static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005421 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 * Consume last content-layer message and potentially
5423 * update in_msglen which keeps track of the contents'
5424 * consumption state.
5425 *
5426 * (1) Handshake messages:
5427 * Remove last handshake message, move content
5428 * and adapt in_msglen.
5429 *
5430 * (2) Alert messages:
5431 * Consume whole record content, in_msglen = 0.
5432 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005433 * (3) Change cipher spec:
5434 * Consume whole record content, in_msglen = 0.
5435 *
5436 * (4) Application data:
5437 * Don't do anything - the record layer provides
5438 * the application data as a stream transport
5439 * and consumes through mbedtls_ssl_read only.
5440 *
5441 */
5442
5443 /* Case (1): Handshake messages */
5444 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005446 /* Hard assertion to be sure that no application data
5447 * is in flight, as corrupting ssl->in_msglen during
5448 * ssl->in_offt != NULL is fatal. */
5449 if( ssl->in_offt != NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5453 }
5454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005455 /*
5456 * Get next Handshake message in the current record
5457 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005460 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005461 * current handshake content: If DTLS handshake
5462 * fragmentation is used, that's the fragment
5463 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005464 * size here is faulty and should be changed at
5465 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 * (2) While it doesn't seem to cause problems, one
5467 * has to be very careful not to assume that in_hslen
5468 * is always <= in_msglen in a sensible communication.
5469 * Again, it's wrong for DTLS handshake fragmentation.
5470 * The following check is therefore mandatory, and
5471 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005472 * Additionally, ssl->in_hslen might be arbitrarily out of
5473 * bounds after handling a DTLS message with an unexpected
5474 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 */
5476 if( ssl->in_hslen < ssl->in_msglen )
5477 {
5478 ssl->in_msglen -= ssl->in_hslen;
5479 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5480 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005481
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5483 ssl->in_msg, ssl->in_msglen );
5484 }
5485 else
5486 {
5487 ssl->in_msglen = 0;
5488 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005489
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 ssl->in_hslen = 0;
5491 }
5492 /* Case (4): Application data */
5493 else if( ssl->in_offt != NULL )
5494 {
5495 return( 0 );
5496 }
5497 /* Everything else (CCS & Alerts) */
5498 else
5499 {
5500 ssl->in_msglen = 0;
5501 }
5502
Hanno Becker1097b342018-08-15 14:09:41 +01005503 return( 0 );
5504}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505
Hanno Beckere74d5562018-08-15 14:26:08 +01005506static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5507{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005509 return( 1 );
5510
5511 return( 0 );
5512}
5513
Hanno Becker5f066e72018-08-16 14:56:31 +01005514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515
5516static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5517{
5518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5519 if( hs == NULL )
5520 return;
5521
Hanno Becker01315ea2018-08-21 17:22:17 +01005522 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005524 hs->buffering.total_bytes_buffered -=
5525 hs->buffering.future_record.len;
5526
5527 mbedtls_free( hs->buffering.future_record.data );
5528 hs->buffering.future_record.data = NULL;
5529 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005530}
5531
5532static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5533{
5534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5535 unsigned char * rec;
5536 size_t rec_len;
5537 unsigned rec_epoch;
5538
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005539 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005540 return( 0 );
5541
5542 if( hs == NULL )
5543 return( 0 );
5544
Hanno Becker5f066e72018-08-16 14:56:31 +01005545 rec = hs->buffering.future_record.data;
5546 rec_len = hs->buffering.future_record.len;
5547 rec_epoch = hs->buffering.future_record.epoch;
5548
5549 if( rec == NULL )
5550 return( 0 );
5551
Hanno Becker4cb782d2018-08-20 11:19:05 +01005552 /* Only consider loading future records if the
5553 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005554 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005555 return( 0 );
5556
Hanno Becker5f066e72018-08-16 14:56:31 +01005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5558
5559 if( rec_epoch != ssl->in_epoch )
5560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5562 goto exit;
5563 }
5564
5565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5566
5567 /* Double-check that the record is not too large */
5568 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5569 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5573 }
5574
5575 memcpy( ssl->in_hdr, rec, rec_len );
5576 ssl->in_left = rec_len;
5577 ssl->next_record_offset = 0;
5578
5579 ssl_free_buffered_record( ssl );
5580
5581exit:
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5583 return( 0 );
5584}
5585
5586static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5587{
5588 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5589 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005591
5592 /* Don't buffer future records outside handshakes. */
5593 if( hs == NULL )
5594 return( 0 );
5595
5596 /* Only buffer handshake records (we are only interested
5597 * in Finished messages). */
5598 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5599 return( 0 );
5600
5601 /* Don't buffer more than one future epoch record. */
5602 if( hs->buffering.future_record.data != NULL )
5603 return( 0 );
5604
Hanno Becker01315ea2018-08-21 17:22:17 +01005605 /* Don't buffer record if there's not enough buffering space remaining. */
5606 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5607 hs->buffering.total_bytes_buffered ) )
5608 {
5609 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",
5610 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5611 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005612 return( 0 );
5613 }
5614
Hanno Becker5f066e72018-08-16 14:56:31 +01005615 /* Buffer record */
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5617 ssl->in_epoch + 1 ) );
5618 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5619 rec_hdr_len + ssl->in_msglen );
5620
5621 /* ssl_parse_record_header() only considers records
5622 * of the next epoch as candidates for buffering. */
5623 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005624 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005625
5626 hs->buffering.future_record.data =
5627 mbedtls_calloc( 1, hs->buffering.future_record.len );
5628 if( hs->buffering.future_record.data == NULL )
5629 {
5630 /* If we run out of RAM trying to buffer a
5631 * record from the next epoch, just ignore. */
5632 return( 0 );
5633 }
5634
Hanno Becker01315ea2018-08-21 17:22:17 +01005635 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005636
Hanno Becker01315ea2018-08-21 17:22:17 +01005637 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005638 return( 0 );
5639}
5640
5641#endif /* MBEDTLS_SSL_PROTO_DTLS */
5642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005644{
5645 int ret;
5646
Hanno Becker5f066e72018-08-16 14:56:31 +01005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
5648 /* We might have buffered a future record; if so,
5649 * and if the epoch matches now, load it.
5650 * On success, this call will set ssl->in_left to
5651 * the length of the buffered record, so that
5652 * the calls to ssl_fetch_input() below will
5653 * essentially be no-ops. */
5654 ret = ssl_load_buffered_record( ssl );
5655 if( ret != 0 )
5656 return( ret );
5657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005658
Hanno Becker8b09b732019-05-08 12:03:28 +01005659 /* Reset in pointers to default state for TLS/DTLS records,
5660 * assuming no CID and no offset between record content and
5661 * record plaintext. */
5662 ssl_update_in_pointers( ssl );
5663
5664 /* Ensure that we have enough space available for the default form
5665 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5666 * with no space for CIDs counted in). */
5667 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5668 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005671 return( ret );
5672 }
5673
5674 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005678 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005679 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5681 {
5682 ret = ssl_buffer_future_record( ssl );
5683 if( ret != 0 )
5684 return( ret );
5685
5686 /* Fall through to handling of unexpected records */
5687 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5688 }
5689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005690 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5691 {
5692 /* Skip unexpected record (but not whole datagram) */
5693 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005694 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5697 "(header)" ) );
5698 }
5699 else
5700 {
5701 /* Skip invalid record and the rest of the datagram */
5702 ssl->next_record_offset = 0;
5703 ssl->in_left = 0;
5704
5705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5706 "(header)" ) );
5707 }
5708
5709 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005710 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005711 }
5712#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715
5716 /*
5717 * Read and optionally decrypt the message contents
5718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005720 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723 return( ret );
5724 }
5725
5726 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005729 {
Hanno Becker43395762019-05-03 14:46:38 +01005730 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005731 if( ssl->next_record_offset < ssl->in_left )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5734 }
5735 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005736 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005737#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005738#if defined(MBEDTLS_SSL_PROTO_TLS)
5739 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005741 }
5742#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743
5744 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 {
5749 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005750 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005752 /* Except when waiting for Finished as a bad mac here
5753 * probably means something went wrong in the handshake
5754 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5755 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5756 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5757 {
5758#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5760 {
5761 mbedtls_ssl_send_alert_message( ssl,
5762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5763 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5764 }
5765#endif
5766 return( ret );
5767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005770 if( ssl->conf->badmac_limit != 0 &&
5771 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005775 }
5776#endif
5777
Hanno Becker4a810fb2017-05-24 16:27:30 +01005778 /* As above, invalid records cause
5779 * dismissal of the whole datagram. */
5780
5781 ssl->next_record_offset = 0;
5782 ssl->in_left = 0;
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005785 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790 MBEDTLS_SSL_TRANSPORT_ELSE
5791#endif /* MBEDTLS_SSL_PROTO_DTLS */
5792#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005793 {
5794 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5796 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 mbedtls_ssl_send_alert_message( ssl,
5799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005801 }
5802#endif
5803 return( ret );
5804 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005805#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005807
Simon Butcher99000142016-10-13 17:21:01 +01005808 return( 0 );
5809}
5810
5811int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5812{
5813 int ret;
5814
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005815 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816 * Handle particular types of records
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 {
Simon Butcher99000142016-10-13 17:21:01 +01005820 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5821 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005822 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 }
5825
Hanno Beckere678eaa2018-08-21 14:57:46 +01005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005827 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005828 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005829 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5831 ssl->in_msglen ) );
5832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msg[0] != 1 )
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5838 ssl->in_msg[0] ) );
5839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5840 }
5841
5842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5845 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5846 {
5847 if( ssl->handshake == NULL )
5848 {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5850 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5851 }
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5854 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5855 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005861 if( ssl->in_msglen != 2 )
5862 {
5863 /* Note: Standard allows for more than one 2 byte alert
5864 to be packed in a single message, but Mbed TLS doesn't
5865 currently support this. */
5866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5867 ssl->in_msglen ) );
5868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5869 }
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 ssl->in_msg[0], ssl->in_msg[1] ) );
5873
5874 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005875 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005880 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5885 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5888 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005890
5891#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5892 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5893 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5894 {
Hanno Becker90333da2017-10-10 11:27:13 +01005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896 /* Will be handled when trying to parse ServerHello */
5897 return( 0 );
5898 }
5899#endif
5900
5901#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5903 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5904 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5905 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5908 /* Will be handled in mbedtls_ssl_parse_certificate() */
5909 return( 0 );
5910 }
5911#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5912
5913 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005914 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 }
5916
Hanno Beckerc76c6192017-06-06 10:03:17 +01005917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005919 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005920 /* Drop unexpected ApplicationData records,
5921 * except at the beginning of renegotiations */
5922 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5923 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5924#if defined(MBEDTLS_SSL_RENEGOTIATION)
5925 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5926 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005927#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005928 )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5931 return( MBEDTLS_ERR_SSL_NON_FATAL );
5932 }
5933
5934 if( ssl->handshake != NULL &&
5935 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5936 {
5937 ssl_handshake_wrapup_free_hs_transform( ssl );
5938 }
5939 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005941
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 return( 0 );
5943}
5944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005946{
5947 int ret;
5948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5950 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952 {
5953 return( ret );
5954 }
5955
5956 return( 0 );
5957}
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005960 unsigned char level,
5961 unsigned char message )
5962{
5963 int ret;
5964
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005965 if( ssl == NULL || ssl->conf == NULL )
5966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005972 ssl->out_msglen = 2;
5973 ssl->out_msg[0] = level;
5974 ssl->out_msg[1] = message;
5975
Hanno Becker67bc7c32018-08-06 11:33:50 +01005976 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005979 return( ret );
5980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005982
5983 return( 0 );
5984}
5985
Paul Bakker5121ce52009-01-03 21:22:43 +00005986/*
5987 * Handshake functions
5988 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005989#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005990/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Hanno Becker8759e162017-12-27 21:34:08 +00005993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Hanno Becker5097cba2019-02-05 13:36:46 +00005997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006000 ssl->state++;
6001 return( 0 );
6002 }
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006}
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009{
Hanno Becker8759e162017-12-27 21:34:08 +00006010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Hanno Becker5097cba2019-02-05 13:36:46 +00006014 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017 ssl->state++;
6018 return( 0 );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006023}
Gilles Peskinef9828522017-05-03 12:28:43 +02006024
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006025#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006026/* Some certificate support -> implement write and parse */
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006033 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006036
Hanno Becker5097cba2019-02-05 13:36:46 +00006037 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
6047 if( ssl->client_auth == 0 )
6048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->state++;
6051 return( 0 );
6052 }
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
6056 * If using SSLv3 and got no cert, send an Alert message
6057 * (otherwise an empty Certificate message will be sent).
6058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6060 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6064 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6065 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 goto write_msg;
6069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_CLI_C */
6073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6079 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 }
6081 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 /*
6087 * 0 . 0 handshake type
6088 * 1 . 3 handshake length
6089 * 4 . 6 length of all certs
6090 * 7 . 9 length of cert. 1
6091 * 10 . n-1 peer certificate
6092 * n . n+2 length of cert. 2
6093 * n+3 . ... upper level cert, etc.
6094 */
6095 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakker29087132010-03-21 21:03:34 +00006098 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006101 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006104 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
6108 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6109 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6110 ssl->out_msg[i + 2] = (unsigned char)( n );
6111
6112 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6113 i += n; crt = crt->next;
6114 }
6115
6116 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6117 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6118 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6119
6120 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6122 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006125write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
6128 ssl->state++;
6129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006130 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Paul Bakkered27a042013-04-18 22:46:23 +02006138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139}
6140
Hanno Becker285ff0c2019-01-31 07:44:03 +00006141#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006142static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6143 unsigned char *crt_buf,
6144 size_t crt_buf_len )
6145{
6146 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6147
6148 if( peer_crt == NULL )
6149 return( -1 );
6150
6151 if( peer_crt->raw.len != crt_buf_len )
6152 return( -1 );
6153
Hanno Becker68b856d2019-02-08 14:00:04 +00006154 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006155}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006156#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006157
Hanno Becker22141592019-02-05 12:38:15 +00006158static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6159{
6160 if( session->peer_cert != NULL )
6161 {
6162 mbedtls_x509_crt_free( session->peer_cert );
6163 mbedtls_free( session->peer_cert );
6164 session->peer_cert = NULL;
6165 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +00006166
6167#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6168 if( session->peer_cert_digest != NULL )
6169 {
6170 /* Zeroization is not necessary. */
6171 mbedtls_free( session->peer_cert_digest );
6172 session->peer_cert_digest = NULL;
6173 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6174 session->peer_cert_digest_len = 0;
6175 }
6176#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker22141592019-02-05 12:38:15 +00006177}
6178
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006179/*
6180 * Once the certificate message is read, parse it into a cert chain and
6181 * perform basic checks, but leave actual verification to the caller
6182 */
Hanno Becker35e41772019-02-05 15:37:23 +00006183static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6184 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006185{
Hanno Becker35e41772019-02-05 15:37:23 +00006186 int ret;
6187#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6188 int crt_cnt=0;
6189#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006190 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006191 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006196 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6197 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 }
6200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6202 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006205 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6206 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 }
6209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006211
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006215 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006216
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006217 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006221 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6222 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 }
6225
Hanno Becker33c3dc82019-01-30 14:46:46 +00006226 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6227 i += 3;
6228
Hanno Becker33c3dc82019-01-30 14:46:46 +00006229 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 while( i < ssl->in_hslen )
6231 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006232 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006233 if ( i + 3 > ssl->in_hslen ) {
6234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006235 mbedtls_ssl_send_alert_message( ssl,
6236 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6237 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6239 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006240 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6241 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 if( ssl->in_msg[i] != 0 )
6243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006245 mbedtls_ssl_send_alert_message( ssl,
6246 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6247 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249 }
6250
Hanno Becker33c3dc82019-01-30 14:46:46 +00006251 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6253 | (unsigned int) ssl->in_msg[i + 2];
6254 i += 3;
6255
6256 if( n < 128 || i + n > ssl->in_hslen )
6257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006259 mbedtls_ssl_send_alert_message( ssl,
6260 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6261 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 }
6264
Hanno Becker33c3dc82019-01-30 14:46:46 +00006265 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006266#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6267 if( crt_cnt++ == 0 &&
6268 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6269 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006271 /* During client-side renegotiation, check that the server's
6272 * end-CRTs hasn't changed compared to the initial handshake,
6273 * mitigating the triple handshake attack. On success, reuse
6274 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6276 if( ssl_check_peer_crt_unchanged( ssl,
6277 &ssl->in_msg[i],
6278 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006279 {
Hanno Becker35e41772019-02-05 15:37:23 +00006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6281 mbedtls_ssl_send_alert_message( ssl,
6282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6284 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006285 }
Hanno Becker35e41772019-02-05 15:37:23 +00006286
6287 /* Now we can safely free the original chain. */
6288 ssl_clear_peer_cert( ssl->session );
6289 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006290#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6291
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006293 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006294 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006296 case 0: /*ok*/
6297 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6298 /* Ignore certificate with an unknown algorithm: maybe a
6299 prior certificate was already trusted. */
6300 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006301
Hanno Becker33c3dc82019-01-30 14:46:46 +00006302 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6303 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6304 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006305
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6307 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6308 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006309
Hanno Becker33c3dc82019-01-30 14:46:46 +00006310 default:
6311 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6312 crt_parse_der_failed:
6313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6314 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6315 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006316 }
6317
6318 i += n;
6319 }
6320
Hanno Becker35e41772019-02-05 15:37:23 +00006321 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006322 return( 0 );
6323}
6324
Hanno Beckerb8a08572019-02-05 12:49:06 +00006325#if defined(MBEDTLS_SSL_SRV_C)
6326static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6327{
6328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6329 return( -1 );
6330
6331#if defined(MBEDTLS_SSL_PROTO_SSL3)
6332 /*
6333 * Check if the client sent an empty certificate
6334 */
6335 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6336 {
6337 if( ssl->in_msglen == 2 &&
6338 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6339 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6340 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6343 return( 0 );
6344 }
6345
6346 return( -1 );
6347 }
6348#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6349
6350#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6351 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6352 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6353 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6354 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6355 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6356 {
6357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6358 return( 0 );
6359 }
6360
6361 return( -1 );
6362#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6363 MBEDTLS_SSL_PROTO_TLS1_2 */
6364}
6365#endif /* MBEDTLS_SSL_SRV_C */
6366
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006367/* Check if a certificate message is expected.
6368 * Return either
6369 * - SSL_CERTIFICATE_EXPECTED, or
6370 * - SSL_CERTIFICATE_SKIP
6371 * indicating whether a Certificate message is expected or not.
6372 */
6373#define SSL_CERTIFICATE_EXPECTED 0
6374#define SSL_CERTIFICATE_SKIP 1
6375static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6376 int authmode )
6377{
6378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6379 ssl->handshake->ciphersuite_info;
6380
6381 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6382 return( SSL_CERTIFICATE_SKIP );
6383
6384#if defined(MBEDTLS_SSL_SRV_C)
6385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6386 {
6387 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6388 return( SSL_CERTIFICATE_SKIP );
6389
6390 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6391 {
6392 /* NOTE: Is it intentional that we set verify_result
6393 * to SKIP_VERIFY on server-side only? */
6394 ssl->session_negotiate->verify_result =
6395 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6396 return( SSL_CERTIFICATE_SKIP );
6397 }
6398 }
6399#endif /* MBEDTLS_SSL_SRV_C */
6400
6401 return( SSL_CERTIFICATE_EXPECTED );
6402}
6403
Hanno Becker3cf50612019-02-05 14:36:34 +00006404static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6405 int authmode,
6406 mbedtls_x509_crt *chain,
6407 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006408{
Hanno Becker613d4902019-02-05 13:11:17 +00006409 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6411 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006412 mbedtls_x509_crt *ca_chain;
6413 mbedtls_x509_crl *ca_crl;
6414
6415 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6416 return( 0 );
6417
6418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6419 if( ssl->handshake->sni_ca_chain != NULL )
6420 {
6421 ca_chain = ssl->handshake->sni_ca_chain;
6422 ca_crl = ssl->handshake->sni_ca_crl;
6423 }
6424 else
6425#endif
6426 {
6427 ca_chain = ssl->conf->ca_chain;
6428 ca_crl = ssl->conf->ca_crl;
6429 }
6430
6431 /*
6432 * Main check: verify certificate
6433 */
6434 ret = mbedtls_x509_crt_verify_restartable(
6435 chain,
6436 ca_chain, ca_crl,
6437 ssl->conf->cert_profile,
6438 ssl->hostname,
6439 &ssl->session_negotiate->verify_result,
6440 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6441
6442 if( ret != 0 )
6443 {
6444 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6445 }
6446
6447#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6448 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6449 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6450#endif
6451
6452 /*
6453 * Secondary checks: always done, but change 'ret' only if it was 0
6454 */
6455
6456#if defined(MBEDTLS_ECP_C)
6457 {
6458 const mbedtls_pk_context *pk = &chain->pk;
6459
6460 /* If certificate uses an EC key, make sure the curve is OK */
6461 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6462 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6463 {
6464 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6465
6466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6467 if( ret == 0 )
6468 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6469 }
6470 }
6471#endif /* MBEDTLS_ECP_C */
6472
6473 if( mbedtls_ssl_check_cert_usage( chain,
6474 ciphersuite_info,
6475 ! ssl->conf->endpoint,
6476 &ssl->session_negotiate->verify_result ) != 0 )
6477 {
6478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6479 if( ret == 0 )
6480 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6481 }
6482
6483 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6484 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6485 * with details encoded in the verification flags. All other kinds
6486 * of error codes, including those from the user provided f_vrfy
6487 * functions, are treated as fatal and lead to a failure of
6488 * ssl_parse_certificate even if verification was optional. */
6489 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6490 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6491 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6492 {
6493 ret = 0;
6494 }
6495
6496 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6499 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6500 }
6501
6502 if( ret != 0 )
6503 {
6504 uint8_t alert;
6505
6506 /* The certificate may have been rejected for several reasons.
6507 Pick one and send the corresponding alert. Which alert to send
6508 may be a subject of debate in some cases. */
6509 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6510 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6511 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6512 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6513 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6514 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6522 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6524 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6526 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6528 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6529 else
6530 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6532 alert );
6533 }
6534
6535#if defined(MBEDTLS_DEBUG_C)
6536 if( ssl->session_negotiate->verify_result != 0 )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6539 ssl->session_negotiate->verify_result ) );
6540 }
6541 else
6542 {
6543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6544 }
6545#endif /* MBEDTLS_DEBUG_C */
6546
6547 return( ret );
6548}
6549
6550int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6551{
6552 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006553 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006554#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6555 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6556 ? ssl->handshake->sni_authmode
6557 : ssl->conf->authmode;
6558#else
6559 const int authmode = ssl->conf->authmode;
6560#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006561 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006562
6563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6564
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006565 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6566 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006567 {
6568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006569 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006570 }
6571
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006572#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6573 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006574 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006575 {
6576 goto crt_verify;
6577 }
6578#endif
6579
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006580 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006581 {
6582 /* mbedtls_ssl_read_record may have sent an alert already. We
6583 let it decide whether to alert. */
6584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6585 return( ret );
6586 }
6587
Hanno Beckerb8a08572019-02-05 12:49:06 +00006588#if defined(MBEDTLS_SSL_SRV_C)
6589 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6590 {
6591 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006592
6593 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006594 ret = 0;
6595 else
6596 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006597
Hanno Becker613d4902019-02-05 13:11:17 +00006598 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006599 }
6600#endif /* MBEDTLS_SSL_SRV_C */
6601
Hanno Becker35e41772019-02-05 15:37:23 +00006602 /* Clear existing peer CRT structure in case we tried to
6603 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006604 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker35e41772019-02-05 15:37:23 +00006605 ssl->session_negotiate->peer_cert =
6606 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6607 if( ssl->session_negotiate->peer_cert == NULL )
6608 {
6609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6610 sizeof( mbedtls_x509_crt ) ) );
6611 mbedtls_ssl_send_alert_message( ssl,
6612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6614 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6615 }
6616 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Beckera46c2872019-02-05 13:08:01 +00006617
Hanno Becker35e41772019-02-05 15:37:23 +00006618 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6619 if( ret != 0 )
Hanno Beckera7c1df62019-02-05 14:35:46 +00006620 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006621
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006622#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6623 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006624 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006625
6626crt_verify:
6627 if( ssl->handshake->ecrs_enabled)
6628 rs_ctx = &ssl->handshake->ecrs_ctx;
6629#endif
6630
Hanno Becker3cf50612019-02-05 14:36:34 +00006631 ret = ssl_parse_certificate_verify( ssl, authmode,
6632 ssl->session_negotiate->peer_cert,
6633 rs_ctx );
6634 if( ret != 0 )
6635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Hanno Becker3008d282019-02-05 17:02:28 +00006637#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6638 /* Remember digest of the peer's end-CRT. */
6639 ssl->session_negotiate->peer_cert_digest =
6640 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6641 if( ssl->session_negotiate->peer_cert_digest == NULL )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6644 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6645 mbedtls_ssl_send_alert_message( ssl,
6646 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6647 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6648 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6649 }
6650 ret = mbedtls_md( mbedtls_md_info_from_type(
6651 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6652 ssl->session_negotiate->peer_cert->raw.p,
6653 ssl->session_negotiate->peer_cert->raw.len,
6654 ssl->session_negotiate->peer_cert_digest );
6655 if( ret != 0 )
6656 return( ret );
6657
6658 ssl->session_negotiate->peer_cert_digest_type =
6659 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6660 ssl->session_negotiate->peer_cert_digest_len =
6661 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6662#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006665
Hanno Becker613d4902019-02-05 13:11:17 +00006666exit:
6667
6668 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 return( ret );
6670}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006671#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006674{
6675 int ret;
6676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006679 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006680 ssl->out_msglen = 1;
6681 ssl->out_msg[0] = 1;
6682
Paul Bakker5121ce52009-01-03 21:22:43 +00006683 ssl->state++;
6684
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006685 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006686 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006688 return( ret );
6689 }
6690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006692
6693 return( 0 );
6694}
6695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006697{
6698 int ret;
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006701
Hanno Becker327c93b2018-08-15 13:56:18 +01006702 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006705 return( ret );
6706 }
6707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006708 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006711 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6712 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006714 }
6715
Hanno Beckere678eaa2018-08-21 14:57:46 +01006716 /* CCS records are only accepted if they have length 1 and content '1',
6717 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006718
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006719 /*
6720 * Switch to our negotiated transform and session parameters for inbound
6721 * data.
6722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006724 ssl->transform_in = ssl->transform_negotiate;
6725 ssl->session_in = ssl->session_negotiate;
6726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006731 ssl_dtls_replay_reset( ssl );
6732#endif
6733
6734 /* Increment epoch */
6735 if( ++ssl->in_epoch == 0 )
6736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006738 /* This is highly unlikely to happen for legitimate reasons, so
6739 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006740 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006741 }
6742 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006743 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006744#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006745#if defined(MBEDTLS_SSL_PROTO_TLS)
6746 {
6747 memset( ssl->in_ctr, 0, 8 );
6748 }
6749#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006750
Hanno Beckerf5970a02019-05-08 09:38:41 +01006751 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006753#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6754 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006759 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6760 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006761 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006762 }
6763 }
6764#endif
6765
Paul Bakker5121ce52009-01-03 21:22:43 +00006766 ssl->state++;
6767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006769
6770 return( 0 );
6771}
6772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6774 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006775{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006776 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006778#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6779 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6780 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006781 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006782 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006783#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006784#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6785#if defined(MBEDTLS_SHA512_C)
6786 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006787 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6788 else
6789#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790#if defined(MBEDTLS_SHA256_C)
6791 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006792 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006793 else
6794#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006798 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006799 }
Paul Bakker380da532012-04-18 16:10:25 +00006800}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006803{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6805 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006806 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6807 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6810#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006811 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006814 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006815#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006817}
6818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006820 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6823 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006824 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6825 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006826#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6828#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006829 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006830#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006832 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006835}
6836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6838 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6839static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006840 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006841{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006842 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6843 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006844}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006845#endif
Paul Bakker380da532012-04-18 16:10:25 +00006846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6848#if defined(MBEDTLS_SHA256_C)
6849static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006850 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006851{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006852 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006853}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006854#endif
Paul Bakker380da532012-04-18 16:10:25 +00006855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006856#if defined(MBEDTLS_SHA512_C)
6857static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006858 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006859{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006860 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006861}
Paul Bakker769075d2012-11-24 11:26:46 +01006862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006866static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006868{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006869 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006870 mbedtls_md5_context md5;
6871 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006872
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 unsigned char padbuf[48];
6874 unsigned char md5sum[16];
6875 unsigned char sha1sum[20];
6876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006878 if( !session )
6879 session = ssl->session;
6880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006882
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006883 mbedtls_md5_init( &md5 );
6884 mbedtls_sha1_init( &sha1 );
6885
6886 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6887 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
6889 /*
6890 * SSLv3:
6891 * hash =
6892 * MD5( master + pad2 +
6893 * MD5( handshake + sender + master + pad1 ) )
6894 * + SHA1( master + pad2 +
6895 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006896 */
6897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006899 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6900 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006901#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006904 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6905 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006906#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006909 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006910
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006912
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006913 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6914 mbedtls_md5_update_ret( &md5, session->master, 48 );
6915 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6916 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006918 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6919 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6920 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6921 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006922
Paul Bakker1ef83d62012-04-11 12:09:53 +00006923 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006924
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006925 mbedtls_md5_starts_ret( &md5 );
6926 mbedtls_md5_update_ret( &md5, session->master, 48 );
6927 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6928 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6929 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006930
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006931 mbedtls_sha1_starts_ret( &sha1 );
6932 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6933 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6934 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6935 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006938
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006939 mbedtls_md5_free( &md5 );
6940 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006941
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006942 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6943 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6944 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006950#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006951static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006953{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006955 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006956 mbedtls_md5_context md5;
6957 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006958 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006961 if( !session )
6962 session = ssl->session;
6963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006965
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006966 mbedtls_md5_init( &md5 );
6967 mbedtls_sha1_init( &sha1 );
6968
6969 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6970 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006971
Paul Bakker1ef83d62012-04-11 12:09:53 +00006972 /*
6973 * TLSv1:
6974 * hash = PRF( master, finished_label,
6975 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6976 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006979 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6980 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006981#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006984 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6985 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006986#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006989 ? "client finished"
6990 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006991
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006992 mbedtls_md5_finish_ret( &md5, padbuf );
6993 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006994
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006995 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006996 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006999
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007000 mbedtls_md5_free( &md5 );
7001 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007002
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007003 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7010#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007011static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007012 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007013{
7014 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007015 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007016 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007017 unsigned char padbuf[32];
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007020 if( !session )
7021 session = ssl->session;
7022
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007023 mbedtls_sha256_init( &sha256 );
7024
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007026
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007027 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007028
7029 /*
7030 * TLSv1.2:
7031 * hash = PRF( master, finished_label,
7032 * Hash( handshake ) )[0.11]
7033 */
7034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if !defined(MBEDTLS_SHA256_ALT)
7036 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007037 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007038#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007041 ? "client finished"
7042 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007043
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007044 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007045
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007046 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007047 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007049 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007050
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007051 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007052
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007053 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007060static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007061 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007062{
7063 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007064 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007065 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007066 unsigned char padbuf[48];
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007069 if( !session )
7070 session = ssl->session;
7071
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007072 mbedtls_sha512_init( &sha512 );
7073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007075
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007076 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007077
7078 /*
7079 * TLSv1.2:
7080 * hash = PRF( master, finished_label,
7081 * Hash( handshake ) )[0.11]
7082 */
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007085 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7086 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007087#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007090 ? "client finished"
7091 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007092
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007093 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007094
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007095 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007096 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007099
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007100 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007101
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007102 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106#endif /* MBEDTLS_SHA512_C */
7107#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007112
7113 /*
7114 * Free our handshake params
7115 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007116 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007118 ssl->handshake = NULL;
7119
7120 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007121 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007122 */
7123 if( ssl->transform )
7124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 mbedtls_ssl_transform_free( ssl->transform );
7126 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007127 }
7128 ssl->transform = ssl->transform_negotiate;
7129 ssl->transform_negotiate = NULL;
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007132}
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007135{
7136 int resume = ssl->handshake->resume;
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140#if defined(MBEDTLS_SSL_RENEGOTIATION)
7141 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007144 ssl->renego_records_seen = 0;
7145 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007146#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007147
7148 /*
7149 * Free the previous session and switch in the current one
7150 */
Paul Bakker0a597072012-09-25 21:55:46 +00007151 if( ssl->session )
7152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007154 /* RFC 7366 3.1: keep the EtM state */
7155 ssl->session_negotiate->encrypt_then_mac =
7156 ssl->session->encrypt_then_mac;
7157#endif
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 mbedtls_ssl_session_free( ssl->session );
7160 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007161 }
7162 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007163 ssl->session_negotiate = NULL;
7164
Paul Bakker0a597072012-09-25 21:55:46 +00007165 /*
7166 * Add cache entry
7167 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007168 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007169 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007170 resume == 0 )
7171 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007172 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007174 }
Paul Bakker0a597072012-09-25 21:55:46 +00007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007177 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007178 ssl->handshake->flight != NULL )
7179 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007180 /* Cancel handshake timer */
7181 ssl_set_timer( ssl, 0 );
7182
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007183 /* Keep last flight around in case we need to resend it:
7184 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007186 }
7187 else
7188#endif
7189 ssl_handshake_wrapup_free_hs_transform( ssl );
7190
Paul Bakker48916f92012-09-16 19:57:18 +00007191 ssl->state++;
7192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007194}
7195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007197{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007198 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007201
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007202 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007203
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007204 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007205
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007206 /*
7207 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7208 * may define some other value. Currently (early 2016), no defined
7209 * ciphersuite does this (and this is unlikely to change as activity has
7210 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007215 ssl->verify_data_len = hash_len;
7216 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007217#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007218
Paul Bakker5121ce52009-01-03 21:22:43 +00007219 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7221 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007222
7223 /*
7224 * In case of session resuming, invert the client and server
7225 * ChangeCipherSpec messages order.
7226 */
Paul Bakker0a597072012-09-25 21:55:46 +00007227 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007230 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007236#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 }
7238 else
7239 ssl->state++;
7240
Paul Bakker48916f92012-09-16 19:57:18 +00007241 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007242 * Switch to our negotiated transform and session parameters for outbound
7243 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007248 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007249 {
7250 unsigned char i;
7251
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007252 /* Remember current epoch settings for resending */
7253 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007254 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007255
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007256 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007257 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007258
7259 /* Increment epoch */
7260 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007261 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007262 break;
7263
7264 /* The loop goes to its end iff the counter is wrapping */
7265 if( i == 0 )
7266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7268 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007269 }
7270 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007271 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007273#if defined(MBEDTLS_SSL_PROTO_TLS)
7274 {
7275 memset( ssl->cur_out_ctr, 0, 8 );
7276 }
7277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007278
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007279 ssl->transform_out = ssl->transform_negotiate;
7280 ssl->session_out = ssl->session_negotiate;
7281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7283 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7288 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007289 }
7290 }
7291#endif
7292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007294 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007296#endif
7297
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007298 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007299 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007301 return( ret );
7302 }
7303
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007305 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007306 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7307 {
7308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7309 return( ret );
7310 }
7311#endif
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007314
7315 return( 0 );
7316}
7317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007319#define SSL_MAX_HASH_LEN 36
7320#else
7321#define SSL_MAX_HASH_LEN 12
7322#endif
7323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007325{
Paul Bakker23986e52011-04-24 08:57:21 +00007326 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007327 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007328 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007331
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007332 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007333
Hanno Becker327c93b2018-08-15 13:56:18 +01007334 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007337 return( ret );
7338 }
7339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007343 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7344 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346 }
7347
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007348 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349#if defined(MBEDTLS_SSL_PROTO_SSL3)
7350 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007351 hash_len = 36;
7352 else
7353#endif
7354 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7357 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007360 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7361 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363 }
7364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007366 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007369 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7370 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372 }
7373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007375 ssl->verify_data_len = hash_len;
7376 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007377#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007378
Paul Bakker0a597072012-09-25 21:55:46 +00007379 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007382 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007386 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007388#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007389 }
7390 else
7391 ssl->state++;
7392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007394 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007396#endif
7397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
7400 return( 0 );
7401}
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7408 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7409 mbedtls_md5_init( &handshake->fin_md5 );
7410 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007411 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7412 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007413#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7415#if defined(MBEDTLS_SHA256_C)
7416 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007417 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#if defined(MBEDTLS_SHA512_C)
7420 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007421 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007422#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007424
7425 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007426
7427#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7428 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7429 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7430#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432#if defined(MBEDTLS_DHM_C)
7433 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007434#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435#if defined(MBEDTLS_ECDH_C)
7436 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007438#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007439 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007440#if defined(MBEDTLS_SSL_CLI_C)
7441 handshake->ecjpake_cache = NULL;
7442 handshake->ecjpake_cache_len = 0;
7443#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007444#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007445
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007446#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007447 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007448#endif
7449
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007450#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7451 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7452#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007453}
7454
Hanno Becker611a83b2018-01-03 14:27:32 +00007455void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007456{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7460 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007461
Hanno Becker92231322018-01-03 15:32:51 +00007462#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 mbedtls_md_init( &transform->md_ctx_enc );
7464 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007465#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007466}
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007469{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007471}
7472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007474{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007475 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007476 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007478 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007480 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007481 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007482
7483 /*
7484 * Either the pointers are now NULL or cleared properly and can be freed.
7485 * Now allocate missing structures.
7486 */
7487 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007488 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007489 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007490 }
Paul Bakker48916f92012-09-16 19:57:18 +00007491
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007492 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007493 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007494 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007495 }
Paul Bakker48916f92012-09-16 19:57:18 +00007496
Paul Bakker82788fb2014-10-20 13:59:19 +02007497 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007498 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007499 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007500 }
Paul Bakker48916f92012-09-16 19:57:18 +00007501
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007502 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007503 if( ssl->handshake == NULL ||
7504 ssl->transform_negotiate == NULL ||
7505 ssl->session_negotiate == NULL )
7506 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 mbedtls_free( ssl->handshake );
7510 mbedtls_free( ssl->transform_negotiate );
7511 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007512
7513 ssl->handshake = NULL;
7514 ssl->transform_negotiate = NULL;
7515 ssl->session_negotiate = NULL;
7516
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007517 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007518 }
7519
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007520 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007522 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007523 ssl_handshake_params_init( ssl->handshake );
7524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007526 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007527 {
7528 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007529
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007530 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7531 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7532 else
7533 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007534
7535 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007536 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007537#endif
7538
Paul Bakker48916f92012-09-16 19:57:18 +00007539 return( 0 );
7540}
7541
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007542#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007543/* Dummy cookie callbacks for defaults */
7544static int ssl_cookie_write_dummy( void *ctx,
7545 unsigned char **p, unsigned char *end,
7546 const unsigned char *cli_id, size_t cli_id_len )
7547{
7548 ((void) ctx);
7549 ((void) p);
7550 ((void) end);
7551 ((void) cli_id);
7552 ((void) cli_id_len);
7553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007555}
7556
7557static int ssl_cookie_check_dummy( void *ctx,
7558 const unsigned char *cookie, size_t cookie_len,
7559 const unsigned char *cli_id, size_t cli_id_len )
7560{
7561 ((void) ctx);
7562 ((void) cookie);
7563 ((void) cookie_len);
7564 ((void) cli_id);
7565 ((void) cli_id_len);
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007568}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007569#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007570
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007571/* Once ssl->out_hdr as the address of the beginning of the
7572 * next outgoing record is set, deduce the other pointers.
7573 *
7574 * Note: For TLS, we save the implicit record sequence number
7575 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7576 * and the caller has to make sure there's space for this.
7577 */
7578
7579static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7580 mbedtls_ssl_transform *transform )
7581{
7582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007583 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007584 {
7585 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007586#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007587 ssl->out_cid = ssl->out_ctr + 8;
7588 ssl->out_len = ssl->out_cid;
7589 if( transform != NULL )
7590 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007591#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007592 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007593#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007594 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007595 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007596 MBEDTLS_SSL_TRANSPORT_ELSE
7597#endif /* MBEDTLS_SSL_PROTO_DTLS */
7598#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007599 {
7600 ssl->out_ctr = ssl->out_hdr - 8;
7601 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007602#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007603 ssl->out_cid = ssl->out_len;
7604#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007605 ssl->out_iv = ssl->out_hdr + 5;
7606 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007607#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007608
7609 /* Adjust out_msg to make space for explicit IV, if used. */
7610 if( transform != NULL &&
7611 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7612 {
7613 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7614 }
7615 else
7616 ssl->out_msg = ssl->out_iv;
7617}
7618
7619/* Once ssl->in_hdr as the address of the beginning of the
7620 * next incoming record is set, deduce the other pointers.
7621 *
7622 * Note: For TLS, we save the implicit record sequence number
7623 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7624 * and the caller has to make sure there's space for this.
7625 */
7626
Hanno Beckerf5970a02019-05-08 09:38:41 +01007627static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007628{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007629 /* This function sets the pointers to match the case
7630 * of unprotected TLS/DTLS records, with both ssl->in_iv
7631 * and ssl->in_msg pointing to the beginning of the record
7632 * content.
7633 *
7634 * When decrypting a protected record, ssl->in_msg
7635 * will be shifted to point to the beginning of the
7636 * record plaintext.
7637 */
7638
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007640 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007641 {
Hanno Becker70e79282019-05-03 14:34:53 +01007642 /* This sets the header pointers to match records
7643 * without CID. When we receive a record containing
7644 * a CID, the fields are shifted accordingly in
7645 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007646 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007648 ssl->in_cid = ssl->in_ctr + 8;
7649 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007650#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007651 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007652#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007653 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007654 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007655 MBEDTLS_SSL_TRANSPORT_ELSE
7656#endif /* MBEDTLS_SSL_PROTO_DTLS */
7657#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007658 {
7659 ssl->in_ctr = ssl->in_hdr - 8;
7660 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007662 ssl->in_cid = ssl->in_len;
7663#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007664 ssl->in_iv = ssl->in_hdr + 5;
7665 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007666#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007667
Hanno Beckerf5970a02019-05-08 09:38:41 +01007668 /* This will be adjusted at record decryption time. */
7669 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007670}
7671
Paul Bakker5121ce52009-01-03 21:22:43 +00007672/*
7673 * Initialize an SSL context
7674 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007675void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7676{
7677 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7678}
7679
7680/*
7681 * Setup an SSL context
7682 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007683
7684static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7685{
7686 /* Set the incoming and outgoing record pointers. */
7687#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007688 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007689 {
7690 ssl->out_hdr = ssl->out_buf;
7691 ssl->in_hdr = ssl->in_buf;
7692 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007693 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007694#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007695#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007696 {
7697 ssl->out_hdr = ssl->out_buf + 8;
7698 ssl->in_hdr = ssl->in_buf + 8;
7699 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007700#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007701
7702 /* Derive other internal pointers. */
7703 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007704 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007705}
7706
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007707int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007708 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007709{
Paul Bakker48916f92012-09-16 19:57:18 +00007710 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007711
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007712 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007713
7714 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007715 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007716 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007717
7718 /* Set to NULL in case of an error condition */
7719 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007720
Angus Grattond8213d02016-05-25 20:56:48 +10007721 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7722 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 {
Angus Grattond8213d02016-05-25 20:56:48 +10007724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007725 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007726 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007727 }
7728
7729 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7730 if( ssl->out_buf == NULL )
7731 {
7732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007733 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007734 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 }
7736
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007737 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007738
Paul Bakker48916f92012-09-16 19:57:18 +00007739 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007740 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007741
7742 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007743
7744error:
7745 mbedtls_free( ssl->in_buf );
7746 mbedtls_free( ssl->out_buf );
7747
7748 ssl->conf = NULL;
7749
7750 ssl->in_buf = NULL;
7751 ssl->out_buf = NULL;
7752
7753 ssl->in_hdr = NULL;
7754 ssl->in_ctr = NULL;
7755 ssl->in_len = NULL;
7756 ssl->in_iv = NULL;
7757 ssl->in_msg = NULL;
7758
7759 ssl->out_hdr = NULL;
7760 ssl->out_ctr = NULL;
7761 ssl->out_len = NULL;
7762 ssl->out_iv = NULL;
7763 ssl->out_msg = NULL;
7764
k-stachowiak9f7798e2018-07-31 16:52:32 +02007765 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007766}
7767
7768/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007769 * Reset an initialized and used SSL context for re-use while retaining
7770 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007771 *
7772 * If partial is non-zero, keep data in the input buffer and client ID.
7773 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007774 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007775static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007776{
Paul Bakker48916f92012-09-16 19:57:18 +00007777 int ret;
7778
Hanno Becker7e772132018-08-10 12:38:21 +01007779#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7780 !defined(MBEDTLS_SSL_SRV_C)
7781 ((void) partial);
7782#endif
7783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007785
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007786 /* Cancel any possibly running timer */
7787 ssl_set_timer( ssl, 0 );
7788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789#if defined(MBEDTLS_SSL_RENEGOTIATION)
7790 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007791 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007792
7793 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7795 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007798
Paul Bakker7eb013f2011-10-06 12:37:39 +00007799 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007800 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007801
7802 ssl->in_msgtype = 0;
7803 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007805 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007806 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007807#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007808#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007809 ssl_dtls_replay_reset( ssl );
7810#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007811
7812 ssl->in_hslen = 0;
7813 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007814
7815 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007816
7817 ssl->out_msgtype = 0;
7818 ssl->out_msglen = 0;
7819 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7821 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007822 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007823#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007824
Hanno Becker19859472018-08-06 09:40:20 +01007825 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7826
Paul Bakker48916f92012-09-16 19:57:18 +00007827 ssl->transform_in = NULL;
7828 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007829
Hanno Becker78640902018-08-13 16:35:15 +01007830 ssl->session_in = NULL;
7831 ssl->session_out = NULL;
7832
Angus Grattond8213d02016-05-25 20:56:48 +10007833 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007834
7835#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007836 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007837#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7838 {
7839 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007840 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007841 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7844 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7847 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7850 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007851 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007852 }
7853#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007854
Paul Bakker48916f92012-09-16 19:57:18 +00007855 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 mbedtls_ssl_transform_free( ssl->transform );
7858 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007859 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007860 }
Paul Bakker48916f92012-09-16 19:57:18 +00007861
Paul Bakkerc0463502013-02-14 11:19:38 +01007862 if( ssl->session )
7863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007864 mbedtls_ssl_session_free( ssl->session );
7865 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007866 ssl->session = NULL;
7867 }
7868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007870 ssl->alpn_chosen = NULL;
7871#endif
7872
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007873#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007874#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007875 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007876#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007877 {
7878 mbedtls_free( ssl->cli_id );
7879 ssl->cli_id = NULL;
7880 ssl->cli_id_len = 0;
7881 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007882#endif
7883
Paul Bakker48916f92012-09-16 19:57:18 +00007884 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7885 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007886
7887 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007888}
7889
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007890/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007891 * Reset an initialized and used SSL context for re-use while retaining
7892 * all application-set variables, function pointers and data.
7893 */
7894int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7895{
7896 return( ssl_session_reset_int( ssl, 0 ) );
7897}
7898
7899/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007900 * SSL set accessors
7901 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007902void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007903{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007904 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007905}
7906
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007907void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007908{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007909 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007910}
7911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007913void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007915 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007916}
7917#endif
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007920void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007921{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007922 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007923}
7924#endif
7925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007927
Hanno Becker1841b0a2018-08-24 11:13:57 +01007928void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7929 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007930{
7931 ssl->disable_datagram_packing = !allow_packing;
7932}
7933
7934void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7935 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007937 conf->hs_timeout_min = min;
7938 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007939}
7940#endif
7941
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007942void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007944 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007945}
7946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007948void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007949 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007950 void *p_vrfy )
7951{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007952 conf->f_vrfy = f_vrfy;
7953 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007956
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007957void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007958 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007959 void *p_rng )
7960{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007961 conf->f_rng = f_rng;
7962 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007963}
7964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007965void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007966 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007967 void *p_dbg )
7968{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007969 conf->f_dbg = f_dbg;
7970 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007971}
7972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007974 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007975 mbedtls_ssl_send_t *f_send,
7976 mbedtls_ssl_recv_t *f_recv,
7977 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007978{
7979 ssl->p_bio = p_bio;
7980 ssl->f_send = f_send;
7981 ssl->f_recv = f_recv;
7982 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007983}
7984
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007985#if defined(MBEDTLS_SSL_PROTO_DTLS)
7986void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7987{
7988 ssl->mtu = mtu;
7989}
7990#endif
7991
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007992void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007993{
7994 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007995}
7996
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007997void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7998 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007999 mbedtls_ssl_set_timer_t *f_set_timer,
8000 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008001{
8002 ssl->p_timer = p_timer;
8003 ssl->f_set_timer = f_set_timer;
8004 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008005
8006 /* Make sure we start with no timer running */
8007 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008008}
8009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008011void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008012 void *p_cache,
8013 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8014 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008015{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008016 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008017 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008018 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_CLI_C)
8023int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008024{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008025 int ret;
8026
8027 if( ssl == NULL ||
8028 session == NULL ||
8029 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008030 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008033 }
8034
Hanno Becker58fccf22019-02-06 14:30:46 +00008035 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8036 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008037 return( ret );
8038
Paul Bakker0a597072012-09-25 21:55:46 +00008039 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008040
8041 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008043#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008044
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008045void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008046 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008048 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8049 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8050 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8051 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008052}
8053
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008054void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008055 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008056 int major, int minor )
8057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008059 return;
8060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008061 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008062 return;
8063
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008064 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008065}
8066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008068void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008069 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008070{
8071 conf->cert_profile = profile;
8072}
8073
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008074/* Append a new keycert entry to a (possibly empty) list */
8075static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8076 mbedtls_x509_crt *cert,
8077 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008078{
niisato8ee24222018-06-25 19:05:48 +09008079 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008080
niisato8ee24222018-06-25 19:05:48 +09008081 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8082 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008084
niisato8ee24222018-06-25 19:05:48 +09008085 new_cert->cert = cert;
8086 new_cert->key = key;
8087 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008088
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008089 /* Update head is the list was null, else add to the end */
8090 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008091 {
niisato8ee24222018-06-25 19:05:48 +09008092 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008093 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008094 else
8095 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008096 mbedtls_ssl_key_cert *cur = *head;
8097 while( cur->next != NULL )
8098 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008099 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008100 }
8101
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008102 return( 0 );
8103}
8104
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008105int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008106 mbedtls_x509_crt *own_cert,
8107 mbedtls_pk_context *pk_key )
8108{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008109 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008110}
8111
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008112void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008113 mbedtls_x509_crt *ca_chain,
8114 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008116 conf->ca_chain = ca_chain;
8117 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008118}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008119#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008120
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008121#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8122int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8123 mbedtls_x509_crt *own_cert,
8124 mbedtls_pk_context *pk_key )
8125{
8126 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8127 own_cert, pk_key ) );
8128}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008129
8130void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8131 mbedtls_x509_crt *ca_chain,
8132 mbedtls_x509_crl *ca_crl )
8133{
8134 ssl->handshake->sni_ca_chain = ca_chain;
8135 ssl->handshake->sni_ca_crl = ca_crl;
8136}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008137
8138void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8139 int authmode )
8140{
8141 ssl->handshake->sni_authmode = authmode;
8142}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008143#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8144
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008145#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008146/*
8147 * Set EC J-PAKE password for current handshake
8148 */
8149int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8150 const unsigned char *pw,
8151 size_t pw_len )
8152{
8153 mbedtls_ecjpake_role role;
8154
Janos Follath8eb64132016-06-03 15:40:57 +01008155 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8157
8158 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8159 role = MBEDTLS_ECJPAKE_SERVER;
8160 else
8161 role = MBEDTLS_ECJPAKE_CLIENT;
8162
8163 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8164 role,
8165 MBEDTLS_MD_SHA256,
8166 MBEDTLS_ECP_DP_SECP256R1,
8167 pw, pw_len ) );
8168}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008169#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008172int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008173 const unsigned char *psk, size_t psk_len,
8174 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008175{
Paul Bakker6db455e2013-09-18 17:29:31 +02008176 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008179 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008181
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008182 /* Identity len will be encoded on two bytes */
8183 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008184 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008185 {
8186 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8187 }
8188
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008189 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008190 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008191 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008192
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008193 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008194 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008195 conf->psk_len = 0;
8196 }
8197 if( conf->psk_identity != NULL )
8198 {
8199 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008200 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008201 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008202 }
8203
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008204 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8205 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008206 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008207 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008208 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008209 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008210 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008211 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008212 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008213
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008214 conf->psk_len = psk_len;
8215 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008216
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008217 memcpy( conf->psk, psk, conf->psk_len );
8218 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008219
8220 return( 0 );
8221}
8222
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008223int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8224 const unsigned char *psk, size_t psk_len )
8225{
8226 if( psk == NULL || ssl->handshake == NULL )
8227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8228
8229 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8231
8232 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008233 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008234 mbedtls_platform_zeroize( ssl->handshake->psk,
8235 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008236 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008237 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008238 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008239
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008240 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008241 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008242
8243 ssl->handshake->psk_len = psk_len;
8244 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8245
8246 return( 0 );
8247}
8248
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008249void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008250 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008251 size_t),
8252 void *p_psk )
8253{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008254 conf->f_psk = f_psk;
8255 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008258
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008259#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008260
8261#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008262int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008263{
8264 int ret;
8265
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008266 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8267 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8268 {
8269 mbedtls_mpi_free( &conf->dhm_P );
8270 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008271 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008273
8274 return( 0 );
8275}
Hanno Becker470a8c42017-10-04 15:28:46 +01008276#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008277
Hanno Beckera90658f2017-10-04 15:29:08 +01008278int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8279 const unsigned char *dhm_P, size_t P_len,
8280 const unsigned char *dhm_G, size_t G_len )
8281{
8282 int ret;
8283
8284 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8285 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8286 {
8287 mbedtls_mpi_free( &conf->dhm_P );
8288 mbedtls_mpi_free( &conf->dhm_G );
8289 return( ret );
8290 }
8291
8292 return( 0 );
8293}
Paul Bakker5121ce52009-01-03 21:22:43 +00008294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008296{
8297 int ret;
8298
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008299 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8300 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8301 {
8302 mbedtls_mpi_free( &conf->dhm_P );
8303 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008304 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008305 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008306
8307 return( 0 );
8308}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008309#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008310
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008311#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8312/*
8313 * Set the minimum length for Diffie-Hellman parameters
8314 */
8315void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8316 unsigned int bitlen )
8317{
8318 conf->dhm_min_bitlen = bitlen;
8319}
8320#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8321
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008322#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008323/*
8324 * Set allowed/preferred hashes for handshake signatures
8325 */
8326void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8327 const int *hashes )
8328{
8329 conf->sig_hashes = hashes;
8330}
Hanno Becker947194e2017-04-07 13:25:49 +01008331#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008332
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008333#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008334/*
8335 * Set the allowed elliptic curves
8336 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008337void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008338 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008339{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008340 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008341}
Hanno Becker947194e2017-04-07 13:25:49 +01008342#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008343
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008344#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008346{
Hanno Becker947194e2017-04-07 13:25:49 +01008347 /* Initialize to suppress unnecessary compiler warning */
8348 size_t hostname_len = 0;
8349
8350 /* Check if new hostname is valid before
8351 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008352 if( hostname != NULL )
8353 {
8354 hostname_len = strlen( hostname );
8355
8356 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8358 }
8359
8360 /* Now it's clear that we will overwrite the old hostname,
8361 * so we can free it safely */
8362
8363 if( ssl->hostname != NULL )
8364 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008365 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008366 mbedtls_free( ssl->hostname );
8367 }
8368
8369 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008370
Paul Bakker5121ce52009-01-03 21:22:43 +00008371 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008372 {
8373 ssl->hostname = NULL;
8374 }
8375 else
8376 {
8377 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008378 if( ssl->hostname == NULL )
8379 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008380
Hanno Becker947194e2017-04-07 13:25:49 +01008381 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008382
Hanno Becker947194e2017-04-07 13:25:49 +01008383 ssl->hostname[hostname_len] = '\0';
8384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008385
8386 return( 0 );
8387}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008388#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008389
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008390#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008391void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008393 const unsigned char *, size_t),
8394 void *p_sni )
8395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008396 conf->f_sni = f_sni;
8397 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008398}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008399#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008402int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008403{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008404 size_t cur_len, tot_len;
8405 const char **p;
8406
8407 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008408 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8409 * MUST NOT be truncated."
8410 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008411 */
8412 tot_len = 0;
8413 for( p = protos; *p != NULL; p++ )
8414 {
8415 cur_len = strlen( *p );
8416 tot_len += cur_len;
8417
8418 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008419 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008420 }
8421
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008422 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008423
8424 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008425}
8426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008428{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008429 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008431#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008432
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008433void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008435 conf->max_major_ver = major;
8436 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008437}
8438
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008439void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008440{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008441 conf->min_major_ver = major;
8442 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008443}
8444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008446void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008447{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008448 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008449}
8450#endif
8451
Janos Follath088ce432017-04-10 12:42:31 +01008452#if defined(MBEDTLS_SSL_SRV_C)
8453void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8454 char cert_req_ca_list )
8455{
8456 conf->cert_req_ca_list = cert_req_ca_list;
8457}
8458#endif
8459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008463 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008464}
8465#endif
8466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008468void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008470 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008471}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008472
8473void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008474 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008475{
8476 conf->enforce_extended_master_secret = ems_enf;
8477}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008478#endif
8479
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008480#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008481void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008482{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008483 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008484}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008485#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008488int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008489{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008491 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008493 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008494 }
8495
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008496 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008497
8498 return( 0 );
8499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008500#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008503void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008505 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008509#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008510void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008511{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008512 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008513}
8514#endif
8515
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008517{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008518 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008519}
8520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008521#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008522void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008523{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008524 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008525}
8526
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008529 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008530}
8531
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008532void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008533 const unsigned char period[8] )
8534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008535 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008540#if defined(MBEDTLS_SSL_CLI_C)
8541void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008542{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008543 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008544}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008545#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008546
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008547#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008548void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8549 mbedtls_ssl_ticket_write_t *f_ticket_write,
8550 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8551 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008552{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008553 conf->f_ticket_write = f_ticket_write;
8554 conf->f_ticket_parse = f_ticket_parse;
8555 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008556}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008557#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008559
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008560#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8561void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8562 mbedtls_ssl_export_keys_t *f_export_keys,
8563 void *p_export_keys )
8564{
8565 conf->f_export_keys = f_export_keys;
8566 conf->p_export_keys = p_export_keys;
8567}
8568#endif
8569
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008570#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008571void mbedtls_ssl_conf_async_private_cb(
8572 mbedtls_ssl_config *conf,
8573 mbedtls_ssl_async_sign_t *f_async_sign,
8574 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8575 mbedtls_ssl_async_resume_t *f_async_resume,
8576 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008577 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008578{
8579 conf->f_async_sign_start = f_async_sign;
8580 conf->f_async_decrypt_start = f_async_decrypt;
8581 conf->f_async_resume = f_async_resume;
8582 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008583 conf->p_async_config_data = async_config_data;
8584}
8585
Gilles Peskine8f97af72018-04-26 11:46:10 +02008586void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8587{
8588 return( conf->p_async_config_data );
8589}
8590
Gilles Peskine1febfef2018-04-30 11:54:39 +02008591void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008592{
8593 if( ssl->handshake == NULL )
8594 return( NULL );
8595 else
8596 return( ssl->handshake->user_async_ctx );
8597}
8598
Gilles Peskine1febfef2018-04-30 11:54:39 +02008599void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008600 void *ctx )
8601{
8602 if( ssl->handshake != NULL )
8603 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008604}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008605#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008606
Paul Bakker5121ce52009-01-03 21:22:43 +00008607/*
8608 * SSL get accessors
8609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008610size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008611{
8612 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8613}
8614
Hanno Becker8b170a02017-10-10 11:51:19 +01008615int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8616{
8617 /*
8618 * Case A: We're currently holding back
8619 * a message for further processing.
8620 */
8621
8622 if( ssl->keep_current_message == 1 )
8623 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008625 return( 1 );
8626 }
8627
8628 /*
8629 * Case B: Further records are pending in the current datagram.
8630 */
8631
8632#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008633 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008634 ssl->in_left > ssl->next_record_offset )
8635 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008637 return( 1 );
8638 }
8639#endif /* MBEDTLS_SSL_PROTO_DTLS */
8640
8641 /*
8642 * Case C: A handshake message is being processed.
8643 */
8644
Hanno Becker8b170a02017-10-10 11:51:19 +01008645 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8646 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008648 return( 1 );
8649 }
8650
8651 /*
8652 * Case D: An application data message is being processed
8653 */
8654 if( ssl->in_offt != NULL )
8655 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008657 return( 1 );
8658 }
8659
8660 /*
8661 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008662 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008663 * we implement support for multiple alerts in single records.
8664 */
8665
8666 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8667 return( 0 );
8668}
8669
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008670uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008671{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008672 if( ssl->session != NULL )
8673 return( ssl->session->verify_result );
8674
8675 if( ssl->session_negotiate != NULL )
8676 return( ssl->session_negotiate->verify_result );
8677
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008678 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008679}
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008682{
Paul Bakker926c8e42013-03-06 10:23:34 +01008683 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008684 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008686 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008687}
8688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008692 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008693 {
8694 switch( ssl->minor_ver )
8695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008696 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008697 return( "DTLSv1.0" );
8698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008700 return( "DTLSv1.2" );
8701
8702 default:
8703 return( "unknown (DTLS)" );
8704 }
8705 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008706 MBEDTLS_SSL_TRANSPORT_ELSE
8707#endif /* MBEDTLS_SSL_PROTO_DTLS */
8708#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008709 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008710 switch( ssl->minor_ver )
8711 {
8712 case MBEDTLS_SSL_MINOR_VERSION_0:
8713 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008714
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008715 case MBEDTLS_SSL_MINOR_VERSION_1:
8716 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008717
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008718 case MBEDTLS_SSL_MINOR_VERSION_2:
8719 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008720
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008721 case MBEDTLS_SSL_MINOR_VERSION_3:
8722 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008723
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008724 default:
8725 return( "unknown" );
8726 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008727 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008728#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008729}
8730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008732{
Hanno Becker3136ede2018-08-17 15:28:19 +01008733 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008734 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008735 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008736
Hanno Becker43395762019-05-03 14:46:38 +01008737 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8738
Hanno Becker78640902018-08-13 16:35:15 +01008739 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008740 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008742#if defined(MBEDTLS_ZLIB_SUPPORT)
8743 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8744 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008745#endif
8746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008747 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008749 case MBEDTLS_MODE_GCM:
8750 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008751 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008752 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008753 transform_expansion = transform->minlen;
8754 break;
8755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008756 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008757
8758 block_size = mbedtls_cipher_get_block_size(
8759 &transform->cipher_ctx_enc );
8760
Hanno Becker3136ede2018-08-17 15:28:19 +01008761 /* Expansion due to the addition of the MAC. */
8762 transform_expansion += transform->maclen;
8763
8764 /* Expansion due to the addition of CBC padding;
8765 * Theoretically up to 256 bytes, but we never use
8766 * more than the block size of the underlying cipher. */
8767 transform_expansion += block_size;
8768
8769 /* For TLS 1.1 or higher, an explicit IV is added
8770 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008771#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8772 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008773 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008774#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008775
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008776 break;
8777
8778 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008781 }
8782
Hanno Beckera5a2b082019-05-15 14:03:01 +01008783#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008784 if( transform->out_cid_len != 0 )
8785 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008786#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008787
Hanno Becker43395762019-05-03 14:46:38 +01008788 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008789}
8790
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008791#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8792size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8793{
8794 size_t max_len;
8795
8796 /*
8797 * Assume mfl_code is correct since it was checked when set
8798 */
Angus Grattond8213d02016-05-25 20:56:48 +10008799 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008800
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008801 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008802 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008803 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008804 {
Angus Grattond8213d02016-05-25 20:56:48 +10008805 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008806 }
8807
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008808 /* During a handshake, use the value being negotiated */
8809 if( ssl->session_negotiate != NULL &&
8810 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8811 {
8812 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8813 }
8814
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008815 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008816}
8817#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8818
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008819#if defined(MBEDTLS_SSL_PROTO_DTLS)
8820static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8821{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008822 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8823 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8824 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8825 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8826 return ( 0 );
8827
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008828 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8829 return( ssl->mtu );
8830
8831 if( ssl->mtu == 0 )
8832 return( ssl->handshake->mtu );
8833
8834 return( ssl->mtu < ssl->handshake->mtu ?
8835 ssl->mtu : ssl->handshake->mtu );
8836}
8837#endif /* MBEDTLS_SSL_PROTO_DTLS */
8838
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008839int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8840{
8841 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8842
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008843#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8844 !defined(MBEDTLS_SSL_PROTO_DTLS)
8845 (void) ssl;
8846#endif
8847
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008848#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8849 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8850
8851 if( max_len > mfl )
8852 max_len = mfl;
8853#endif
8854
8855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008856 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008857 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008858 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008859 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8860 const size_t overhead = (size_t) ret;
8861
8862 if( ret < 0 )
8863 return( ret );
8864
8865 if( mtu <= overhead )
8866 {
8867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8868 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8869 }
8870
8871 if( max_len > mtu - overhead )
8872 max_len = mtu - overhead;
8873 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008874#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008875
Hanno Becker0defedb2018-08-10 12:35:02 +01008876#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8877 !defined(MBEDTLS_SSL_PROTO_DTLS)
8878 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008879#endif
8880
8881 return( (int) max_len );
8882}
8883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008884#if defined(MBEDTLS_X509_CRT_PARSE_C)
8885const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008886{
8887 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008888 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008889
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008890 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008891}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008892#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008895int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8896 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008897{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008898 if( ssl == NULL ||
8899 dst == NULL ||
8900 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008901 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008904 }
8905
Hanno Becker58fccf22019-02-06 14:30:46 +00008906 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008908#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008909
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008910const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8911{
8912 if( ssl == NULL )
8913 return( NULL );
8914
8915 return( ssl->session );
8916}
8917
Paul Bakker5121ce52009-01-03 21:22:43 +00008918/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008919 * Define ticket header determining Mbed TLS version
8920 * and structure of the ticket.
8921 */
8922
Hanno Becker41527622019-05-16 12:50:45 +01008923/*
Hanno Becker26829e92019-05-28 14:30:45 +01008924 * Define bitflag determining compile-time settings influencing
8925 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008926 */
8927
Hanno Becker26829e92019-05-28 14:30:45 +01008928#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008929#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008930#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008931#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008932#endif /* MBEDTLS_HAVE_TIME */
8933
8934#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008935#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008936#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008937#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008938#endif /* MBEDTLS_X509_CRT_PARSE_C */
8939
8940#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008941#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008942#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008943#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008944#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8945
8946#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008947#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008948#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008949#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008950#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8951
8952#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008953#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008954#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008955#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008956#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8957
8958#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008959#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008960#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008961#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008962#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8963
Hanno Becker41527622019-05-16 12:50:45 +01008964#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8965#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8966#else
8967#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8968#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8969
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008970#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8971#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8972#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8973#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8974#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8975#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8976#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008977
Hanno Becker26829e92019-05-28 14:30:45 +01008978#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008979 ( (uint16_t) ( \
8980 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8981 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8982 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8983 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8984 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8985 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008986 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008987
Hanno Becker557fe9f2019-05-16 12:41:07 +01008988static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008989 MBEDTLS_VERSION_MAJOR,
8990 MBEDTLS_VERSION_MINOR,
8991 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008992 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8993 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008994};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008995
8996/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008997 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008998 * (in the presentation language of TLS, RFC 8446 section 3)
8999 *
Hanno Becker26829e92019-05-28 14:30:45 +01009000 * opaque mbedtls_version[3]; // major, minor, patch
9001 * opaque session_format[2]; // version-specific 16-bit field determining
9002 * // the format of the remaining
9003 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009004 *
9005 * Note: When updating the format, remember to keep
9006 * these version+format bytes.
9007 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009008 * // In this version, `session_format` determines
9009 * // the setting of those compile-time
9010 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009011 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009012 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009013 * uint8 ciphersuite[2]; // defined by the standard
9014 * uint8 compression; // 0 or 1
9015 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009016 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009017 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009018 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01009019 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
9020 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009021 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009022 * uint8 mfl_code; // up to 255 according to standard
9023 * uint8 trunc_hmac; // 0 or 1
9024 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009025 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009026 * The order is the same as in the definition of the structure, except
9027 * verify_result is put before peer_cert so that all mandatory fields come
9028 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009029 */
9030int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9031 unsigned char *buf,
9032 size_t buf_len,
9033 size_t *olen )
9034{
9035 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009036 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009037#if defined(MBEDTLS_HAVE_TIME)
9038 uint64_t start;
9039#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009040#if defined(MBEDTLS_X509_CRT_PARSE_C)
9041 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009042#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009043
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009044 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009045 * Add version identifier
9046 */
9047
9048 used += sizeof( ssl_serialized_session_header );
9049
9050 if( used <= buf_len )
9051 {
9052 memcpy( p, ssl_serialized_session_header,
9053 sizeof( ssl_serialized_session_header ) );
9054 p += sizeof( ssl_serialized_session_header );
9055 }
9056
9057 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009058 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009059 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009060#if defined(MBEDTLS_HAVE_TIME)
9061 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009062
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009063 if( used <= buf_len )
9064 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009065 start = (uint64_t) session->start;
9066
9067 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9068 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9069 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9070 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9071 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9072 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9073 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9074 *p++ = (unsigned char)( ( start ) & 0xFF );
9075 }
9076#endif /* MBEDTLS_HAVE_TIME */
9077
9078 /*
9079 * Basic mandatory fields
9080 */
9081 used += 2 /* ciphersuite */
9082 + 1 /* compression */
9083 + 1 /* id_len */
9084 + sizeof( session->id )
9085 + sizeof( session->master )
9086 + 4; /* verify_result */
9087
9088 if( used <= buf_len )
9089 {
9090 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9091 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9092
9093 *p++ = (unsigned char)( session->compression & 0xFF );
9094
9095 *p++ = (unsigned char)( session->id_len & 0xFF );
9096 memcpy( p, session->id, 32 );
9097 p += 32;
9098
9099 memcpy( p, session->master, 48 );
9100 p += 48;
9101
9102 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9103 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9104 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9105 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009106 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009107
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009108 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009109 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009110 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009111#if defined(MBEDTLS_X509_CRT_PARSE_C)
9112 if( session->peer_cert == NULL )
9113 cert_len = 0;
9114 else
9115 cert_len = session->peer_cert->raw.len;
9116
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009117 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009118
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009119 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009120 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009121 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9122 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9123 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9124
9125 if( session->peer_cert != NULL )
9126 {
9127 memcpy( p, session->peer_cert->raw.p, cert_len );
9128 p += cert_len;
9129 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009130 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009131#endif /* MBEDTLS_X509_CRT_PARSE_C */
9132
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009133 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009134 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009135 */
9136#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009137 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009138
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009139 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009140 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009141 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9142 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9143 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9144
9145 if( session->ticket != NULL )
9146 {
9147 memcpy( p, session->ticket, session->ticket_len );
9148 p += session->ticket_len;
9149 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009150
9151 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9152 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9153 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9154 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009155 }
9156#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9157
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009158 /*
9159 * Misc extension-related info
9160 */
9161#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9162 used += 1;
9163
9164 if( used <= buf_len )
9165 *p++ = session->mfl_code;
9166#endif
9167
9168#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9169 used += 1;
9170
9171 if( used <= buf_len )
9172 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9173#endif
9174
9175#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9176 used += 1;
9177
9178 if( used <= buf_len )
9179 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9180#endif
9181
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009182 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009183 *olen = used;
9184
9185 if( used > buf_len )
9186 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009187
9188 return( 0 );
9189}
9190
9191/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009192 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009193 *
9194 * This internal version is wrapped by a public function that cleans up in
9195 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009196 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009197static int ssl_session_load( mbedtls_ssl_session *session,
9198 const unsigned char *buf,
9199 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009200{
9201 const unsigned char *p = buf;
9202 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009203#if defined(MBEDTLS_HAVE_TIME)
9204 uint64_t start;
9205#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009206#if defined(MBEDTLS_X509_CRT_PARSE_C)
9207 size_t cert_len;
9208#endif /* MBEDTLS_X509_CRT_PARSE_C */
9209
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009210 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009211 * Check version identifier
9212 */
9213
9214 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009216
9217 if( memcmp( p, ssl_serialized_session_header,
9218 sizeof( ssl_serialized_session_header ) ) != 0 )
9219 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009220 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009221 }
9222 p += sizeof( ssl_serialized_session_header );
9223
9224 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009225 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009226 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009227#if defined(MBEDTLS_HAVE_TIME)
9228 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009229 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9230
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009231 start = ( (uint64_t) p[0] << 56 ) |
9232 ( (uint64_t) p[1] << 48 ) |
9233 ( (uint64_t) p[2] << 40 ) |
9234 ( (uint64_t) p[3] << 32 ) |
9235 ( (uint64_t) p[4] << 24 ) |
9236 ( (uint64_t) p[5] << 16 ) |
9237 ( (uint64_t) p[6] << 8 ) |
9238 ( (uint64_t) p[7] );
9239 p += 8;
9240
9241 session->start = (time_t) start;
9242#endif /* MBEDTLS_HAVE_TIME */
9243
9244 /*
9245 * Basic mandatory fields
9246 */
9247 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9248 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9249
9250 session->ciphersuite = ( p[0] << 8 ) | p[1];
9251 p += 2;
9252
9253 session->compression = *p++;
9254
9255 session->id_len = *p++;
9256 memcpy( session->id, p, 32 );
9257 p += 32;
9258
9259 memcpy( session->master, p, 48 );
9260 p += 48;
9261
9262 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9263 ( (uint32_t) p[1] << 16 ) |
9264 ( (uint32_t) p[2] << 8 ) |
9265 ( (uint32_t) p[3] );
9266 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009267
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009268 /* Immediately clear invalid pointer values that have been read, in case
9269 * we exit early before we replaced them with valid ones. */
9270#if defined(MBEDTLS_X509_CRT_PARSE_C)
9271 session->peer_cert = NULL;
9272#endif
9273#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9274 session->ticket = NULL;
9275#endif
9276
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009277 /*
9278 * Peer certificate
9279 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009280#if defined(MBEDTLS_X509_CRT_PARSE_C)
9281 if( 3 > (size_t)( end - p ) )
9282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9283
9284 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9285 p += 3;
9286
9287 if( cert_len == 0 )
9288 {
9289 session->peer_cert = NULL;
9290 }
9291 else
9292 {
9293 int ret;
9294
9295 if( cert_len > (size_t)( end - p ) )
9296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9297
9298 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9299
9300 if( session->peer_cert == NULL )
9301 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9302
9303 mbedtls_x509_crt_init( session->peer_cert );
9304
9305 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9306 p, cert_len ) ) != 0 )
9307 {
9308 mbedtls_x509_crt_free( session->peer_cert );
9309 mbedtls_free( session->peer_cert );
9310 session->peer_cert = NULL;
9311 return( ret );
9312 }
9313
9314 p += cert_len;
9315 }
9316#endif /* MBEDTLS_X509_CRT_PARSE_C */
9317
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009318 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009319 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009320 */
9321#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9322 if( 3 > (size_t)( end - p ) )
9323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9324
9325 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9326 p += 3;
9327
9328 if( session->ticket_len != 0 )
9329 {
9330 if( session->ticket_len > (size_t)( end - p ) )
9331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9332
9333 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9334 if( session->ticket == NULL )
9335 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9336
9337 memcpy( session->ticket, p, session->ticket_len );
9338 p += session->ticket_len;
9339 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009340
9341 if( 4 > (size_t)( end - p ) )
9342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9343
9344 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9345 ( (uint32_t) p[1] << 16 ) |
9346 ( (uint32_t) p[2] << 8 ) |
9347 ( (uint32_t) p[3] );
9348 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009349#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9350
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009351 /*
9352 * Misc extension-related info
9353 */
9354#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9355 if( 1 > (size_t)( end - p ) )
9356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9357
9358 session->mfl_code = *p++;
9359#endif
9360
9361#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9362 if( 1 > (size_t)( end - p ) )
9363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9364
9365 session->trunc_hmac = *p++;
9366#endif
9367
9368#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9369 if( 1 > (size_t)( end - p ) )
9370 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9371
9372 session->encrypt_then_mac = *p++;
9373#endif
9374
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009375 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009376 if( p != end )
9377 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9378
9379 return( 0 );
9380}
9381
9382/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009383 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009384 */
9385int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9386 const unsigned char *buf,
9387 size_t len )
9388{
9389 int ret = ssl_session_load( session, buf, len );
9390
9391 if( ret != 0 )
9392 mbedtls_ssl_session_free( session );
9393
9394 return( ret );
9395}
9396
9397/*
Paul Bakker1961b702013-01-25 14:49:24 +01009398 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009400int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009401{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009403
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009404 if( ssl == NULL || ssl->conf == NULL )
9405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009408 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009412 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009414#endif
9415
Paul Bakker1961b702013-01-25 14:49:24 +01009416 return( ret );
9417}
9418
9419/*
9420 * Perform the SSL handshake
9421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009423{
9424 int ret = 0;
9425
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009426 if( ssl == NULL || ssl->conf == NULL )
9427 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009434
9435 if( ret != 0 )
9436 break;
9437 }
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009440
9441 return( ret );
9442}
9443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444#if defined(MBEDTLS_SSL_RENEGOTIATION)
9445#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009446/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009447 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009450{
9451 int ret;
9452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009454
9455 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9457 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009458
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009459 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009460 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009462 return( ret );
9463 }
9464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009466
9467 return( 0 );
9468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009469#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009470
9471/*
9472 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 * - any side: calling mbedtls_ssl_renegotiate(),
9474 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9475 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009476 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009477 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009481{
9482 int ret;
9483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009485
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009486 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9487 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009488
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009489 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9490 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009492 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009494 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009495 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009496 ssl->handshake->out_msg_seq = 1;
9497 else
9498 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009499 }
9500#endif
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9503 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009508 return( ret );
9509 }
9510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009512
9513 return( 0 );
9514}
9515
9516/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009517 * Renegotiate current connection on client,
9518 * or request renegotiation on server
9519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009523
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009524 if( ssl == NULL || ssl->conf == NULL )
9525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009528 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009529 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009535
9536 /* Did we already try/start sending HelloRequest? */
9537 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009539
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009540 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009541 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009545 /*
9546 * On client, either start the renegotiation process or,
9547 * if already in progress, continue the handshake
9548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009553
9554 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009557 return( ret );
9558 }
9559 }
9560 else
9561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009565 return( ret );
9566 }
9567 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009569
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009570 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571}
9572
9573/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009574 * Check record counters and renegotiate if they're above the limit.
9575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009577{
Andres AG2196c7f2016-12-15 17:01:16 +00009578 size_t ep_len = ssl_ep_len( ssl );
9579 int in_ctr_cmp;
9580 int out_ctr_cmp;
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9583 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009584 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009585 {
9586 return( 0 );
9587 }
9588
Andres AG2196c7f2016-12-15 17:01:16 +00009589 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9590 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009591 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009592 ssl->conf->renego_period + ep_len, 8 - ep_len );
9593
9594 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009595 {
9596 return( 0 );
9597 }
9598
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009603
9604/*
9605 * Receive application data decrypted from the SSL layer
9606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009608{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009609 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009610 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009611
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009612 if( ssl == NULL || ssl->conf == NULL )
9613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009618 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009621 return( ret );
9622
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009623 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009625 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009626 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009627 return( ret );
9628 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009629 }
9630#endif
9631
Hanno Becker4a810fb2017-05-24 16:27:30 +01009632 /*
9633 * Check if renegotiation is necessary and/or handshake is
9634 * in process. If yes, perform/continue, and fall through
9635 * if an unexpected packet is received while the client
9636 * is waiting for the ServerHello.
9637 *
9638 * (There is no equivalent to the last condition on
9639 * the server-side as it is not treated as within
9640 * a handshake while waiting for the ClientHello
9641 * after a renegotiation request.)
9642 */
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009645 ret = ssl_check_ctr_renegotiate( ssl );
9646 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9647 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009650 return( ret );
9651 }
9652#endif
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009657 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9658 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009661 return( ret );
9662 }
9663 }
9664
Hanno Beckere41158b2017-10-23 13:30:32 +01009665 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009666 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009667 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009668 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009669 if( ssl->f_get_timer != NULL &&
9670 ssl->f_get_timer( ssl->p_timer ) == -1 )
9671 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009672 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009673 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009674
Hanno Becker327c93b2018-08-15 13:56:18 +01009675 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009676 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009677 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9678 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009679
Hanno Becker4a810fb2017-05-24 16:27:30 +01009680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9681 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009682 }
9683
9684 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009686 {
9687 /*
9688 * OpenSSL sends empty messages to randomize the IV
9689 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009690 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009693 return( 0 );
9694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009696 return( ret );
9697 }
9698 }
9699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009703
Hanno Becker4a810fb2017-05-24 16:27:30 +01009704 /*
9705 * - For client-side, expect SERVER_HELLO_REQUEST.
9706 * - For server-side, expect CLIENT_HELLO.
9707 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9708 */
9709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009713 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009716
9717 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009719 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009720 {
9721 continue;
9722 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009723 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009724#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009725#if defined(MBEDTLS_SSL_PROTO_TLS)
9726 {
9727 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9728 }
9729#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009730 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009731#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009732
Hanno Becker4a810fb2017-05-24 16:27:30 +01009733#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009738
9739 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009742 {
9743 continue;
9744 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009745 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009746#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009747#if defined(MBEDTLS_SSL_PROTO_TLS)
9748 {
9749 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9750 }
9751#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009752 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009753#endif /* MBEDTLS_SSL_SRV_C */
9754
Hanno Becker21df7f92017-10-17 11:03:26 +01009755#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009756 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009757 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9758 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9759 ssl->conf->allow_legacy_renegotiation ==
9760 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9761 {
9762 /*
9763 * Accept renegotiation request
9764 */
Paul Bakker48916f92012-09-16 19:57:18 +00009765
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009766 /* DTLS clients need to know renego is server-initiated */
9767#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009768 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009769 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9770 {
9771 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9772 }
9773#endif
9774 ret = ssl_start_renegotiation( ssl );
9775 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9776 ret != 0 )
9777 {
9778 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9779 return( ret );
9780 }
9781 }
9782 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009783#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009784 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009785 /*
9786 * Refuse renegotiation
9787 */
9788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791#if defined(MBEDTLS_SSL_PROTO_SSL3)
9792 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009793 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009794 /* SSLv3 does not have a "no_renegotiation" warning, so
9795 we send a fatal alert and abort the connection. */
9796 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9797 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9798 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009799 }
9800 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9802#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9803 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9804 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9807 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9808 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009809 {
9810 return( ret );
9811 }
Paul Bakker48916f92012-09-16 19:57:18 +00009812 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9815 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009819 }
Paul Bakker48916f92012-09-16 19:57:18 +00009820 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009821
Hanno Becker90333da2017-10-10 11:27:13 +01009822 /* At this point, we don't know whether the renegotiation has been
9823 * completed or not. The cases to consider are the following:
9824 * 1) The renegotiation is complete. In this case, no new record
9825 * has been read yet.
9826 * 2) The renegotiation is incomplete because the client received
9827 * an application data record while awaiting the ServerHello.
9828 * 3) The renegotiation is incomplete because the client received
9829 * a non-handshake, non-application data message while awaiting
9830 * the ServerHello.
9831 * In each of these case, looping will be the proper action:
9832 * - For 1), the next iteration will read a new record and check
9833 * if it's application data.
9834 * - For 2), the loop condition isn't satisfied as application data
9835 * is present, hence continue is the same as break
9836 * - For 3), the loop condition is satisfied and read_record
9837 * will re-deliver the message that was held back by the client
9838 * when expecting the ServerHello.
9839 */
9840 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009841 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009842#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009844 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009845 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009846 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009847 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009850 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009852 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009853 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9858 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009861 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009862 }
9863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009868 }
9869
9870 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009871
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009872 /* We're going to return something now, cancel timer,
9873 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009875 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009876
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009878 /* If we requested renego but received AppData, resend HelloRequest.
9879 * Do it now, after setting in_offt, to avoid taking this branch
9880 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009881#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009882 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009884 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009885 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009888 return( ret );
9889 }
9890 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009892#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009893 }
9894
9895 n = ( len < ssl->in_msglen )
9896 ? len : ssl->in_msglen;
9897
9898 memcpy( buf, ssl->in_offt, n );
9899 ssl->in_msglen -= n;
9900
9901 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009902 {
9903 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009904 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009905 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009906 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009907 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009908 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009909 /* more data available */
9910 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009914
Paul Bakker23986e52011-04-24 08:57:21 +00009915 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009916}
9917
9918/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009919 * Send application data to be encrypted by the SSL layer, taking care of max
9920 * fragment length and buffer size.
9921 *
9922 * According to RFC 5246 Section 6.2.1:
9923 *
9924 * Zero-length fragments of Application data MAY be sent as they are
9925 * potentially useful as a traffic analysis countermeasure.
9926 *
9927 * Therefore, it is possible that the input message length is 0 and the
9928 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009929 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009930static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009931 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009932{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009933 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9934 const size_t max_len = (size_t) ret;
9935
9936 if( ret < 0 )
9937 {
9938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9939 return( ret );
9940 }
9941
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009942 if( len > max_len )
9943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009945 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009948 "maximum fragment length: %d > %d",
9949 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009951 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009952 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009953#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009954#if defined(MBEDTLS_SSL_PROTO_TLS)
9955 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009956 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009957 }
9958#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009959 }
Paul Bakker887bd502011-06-08 13:10:54 +00009960
Paul Bakker5121ce52009-01-03 21:22:43 +00009961 if( ssl->out_left != 0 )
9962 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009963 /*
9964 * The user has previously tried to send the data and
9965 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9966 * written. In this case, we expect the high-level write function
9967 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 return( ret );
9973 }
9974 }
Paul Bakker887bd502011-06-08 13:10:54 +00009975 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009976 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009977 /*
9978 * The user is trying to send a message the first time, so we need to
9979 * copy the data into the internal buffers and setup the data structure
9980 * to keep track of partial writes
9981 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009982 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009984 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009985
Hanno Becker67bc7c32018-08-06 11:33:50 +01009986 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009989 return( ret );
9990 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009991 }
9992
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009993 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009994}
9995
9996/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009997 * Write application data, doing 1/n-1 splitting if necessary.
9998 *
9999 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010000 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010001 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010004static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010005 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010006{
10007 int ret;
10008
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010009 if( ssl->conf->cbc_record_splitting ==
10010 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010011 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10013 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10014 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010015 {
10016 return( ssl_write_real( ssl, buf, len ) );
10017 }
10018
10019 if( ssl->split_done == 0 )
10020 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010021 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010022 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010023 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010024 }
10025
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010026 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10027 return( ret );
10028 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010029
10030 return( ret + 1 );
10031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010032#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010033
10034/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010035 * Write application data (public-facing wrapper)
10036 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010037int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010038{
10039 int ret;
10040
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010042
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010043 if( ssl == NULL || ssl->conf == NULL )
10044 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10045
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010047 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10048 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010049 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010050 return( ret );
10051 }
10052#endif
10053
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010054 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010055 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010056 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010057 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010059 return( ret );
10060 }
10061 }
10062
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010063#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010064 ret = ssl_write_split( ssl, buf, len );
10065#else
10066 ret = ssl_write_real( ssl, buf, len );
10067#endif
10068
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010070
10071 return( ret );
10072}
10073
10074/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010075 * Notify the peer that the connection is being closed
10076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010078{
10079 int ret;
10080
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010081 if( ssl == NULL || ssl->conf == NULL )
10082 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010085
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010086 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010091 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10092 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10093 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010096 return( ret );
10097 }
10098 }
10099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010101
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010102 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010103}
10104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010105void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010106{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010107 if( transform == NULL )
10108 return;
10109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010111 deflateEnd( &transform->ctx_deflate );
10112 inflateEnd( &transform->ctx_inflate );
10113#endif
10114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10116 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010117
Hanno Becker92231322018-01-03 15:32:51 +000010118#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010119 mbedtls_md_free( &transform->md_ctx_enc );
10120 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010121#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010122
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010123 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010124}
10125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#if defined(MBEDTLS_X509_CRT_PARSE_C)
10127static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010130
10131 while( cur != NULL )
10132 {
10133 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010134 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010135 cur = next;
10136 }
10137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010139
Hanno Becker0271f962018-08-16 13:23:47 +010010140#if defined(MBEDTLS_SSL_PROTO_DTLS)
10141
10142static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10143{
10144 unsigned offset;
10145 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10146
10147 if( hs == NULL )
10148 return;
10149
Hanno Becker283f5ef2018-08-24 09:34:47 +010010150 ssl_free_buffered_record( ssl );
10151
Hanno Becker0271f962018-08-16 13:23:47 +010010152 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010153 ssl_buffering_free_slot( ssl, offset );
10154}
10155
10156static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10157 uint8_t slot )
10158{
10159 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10160 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010161
10162 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10163 return;
10164
Hanno Beckere605b192018-08-21 15:59:07 +010010165 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010166 {
Hanno Beckere605b192018-08-21 15:59:07 +010010167 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010168 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010169 mbedtls_free( hs_buf->data );
10170 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010171 }
10172}
10173
10174#endif /* MBEDTLS_SSL_PROTO_DTLS */
10175
Gilles Peskine9b562d52018-04-25 20:32:43 +020010176void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010177{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010178 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10179
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010180 if( handshake == NULL )
10181 return;
10182
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010183#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10184 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10185 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010186 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010187 handshake->async_in_progress = 0;
10188 }
10189#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10190
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010191#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10192 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10193 mbedtls_md5_free( &handshake->fin_md5 );
10194 mbedtls_sha1_free( &handshake->fin_sha1 );
10195#endif
10196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10197#if defined(MBEDTLS_SHA256_C)
10198 mbedtls_sha256_free( &handshake->fin_sha256 );
10199#endif
10200#if defined(MBEDTLS_SHA512_C)
10201 mbedtls_sha512_free( &handshake->fin_sha512 );
10202#endif
10203#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205#if defined(MBEDTLS_DHM_C)
10206 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_ECDH_C)
10209 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010210#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010211#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010212 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010213#if defined(MBEDTLS_SSL_CLI_C)
10214 mbedtls_free( handshake->ecjpake_cache );
10215 handshake->ecjpake_cache = NULL;
10216 handshake->ecjpake_cache_len = 0;
10217#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010218#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010219
Janos Follath4ae5c292016-02-10 11:27:43 +000010220#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10221 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010222 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010224#endif
10225
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010226#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10227 if( handshake->psk != NULL )
10228 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010229 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010230 mbedtls_free( handshake->psk );
10231 }
10232#endif
10233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10235 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010236 /*
10237 * Free only the linked list wrapper, not the keys themselves
10238 * since the belong to the SNI callback
10239 */
10240 if( handshake->sni_key_cert != NULL )
10241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010243
10244 while( cur != NULL )
10245 {
10246 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010248 cur = next;
10249 }
10250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010252
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010253#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010254 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010255#endif
10256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257#if defined(MBEDTLS_SSL_PROTO_DTLS)
10258 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010259 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010260 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010261#endif
10262
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010263 mbedtls_platform_zeroize( handshake,
10264 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010265}
10266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010268{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010269 if( session == NULL )
10270 return;
10271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010273 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010274#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010275
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010276#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010278#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010279
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010280 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010281}
10282
Paul Bakker5121ce52009-01-03 21:22:43 +000010283/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010284 * Serialize a full SSL context
10285 */
10286int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10287 unsigned char *buf,
10288 size_t buf_len,
10289 size_t *olen )
10290{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010291 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010292 (void) ssl;
10293
10294 if( buf != NULL )
10295 memset( buf, 0, buf_len );
10296
10297 *olen = 0;
10298
10299 return( 0 );
10300}
10301
10302/*
10303 * Deserialize a full SSL context
10304 */
10305int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10306 const unsigned char *buf,
10307 size_t len )
10308{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010309 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010310 (void) ssl;
10311 (void) buf;
10312 (void) len;
10313
10314 return( 0 );
10315}
10316
10317/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010318 * Free an SSL context
10319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010321{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010322 if( ssl == NULL )
10323 return;
10324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010326
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010327 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010328 {
Angus Grattond8213d02016-05-25 20:56:48 +100010329 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010331 }
10332
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010333 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010334 {
Angus Grattond8213d02016-05-25 20:56:48 +100010335 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010337 }
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010340 if( ssl->compress_buf != NULL )
10341 {
Angus Grattond8213d02016-05-25 20:56:48 +100010342 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010344 }
10345#endif
10346
Paul Bakker48916f92012-09-16 19:57:18 +000010347 if( ssl->transform )
10348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 mbedtls_ssl_transform_free( ssl->transform );
10350 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010351 }
10352
10353 if( ssl->handshake )
10354 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010355 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10357 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359 mbedtls_free( ssl->handshake );
10360 mbedtls_free( ssl->transform_negotiate );
10361 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010362 }
10363
Paul Bakkerc0463502013-02-14 11:19:38 +010010364 if( ssl->session )
10365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 mbedtls_ssl_session_free( ssl->session );
10367 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010368 }
10369
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010370#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010371 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010372 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010373 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010374 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010375 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010376#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10379 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10382 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010383 }
10384#endif
10385
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010386#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010388#endif
10389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010391
Paul Bakker86f04f42013-02-14 11:20:09 +010010392 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010393 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010394}
10395
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010396/*
10397 * Initialze mbedtls_ssl_config
10398 */
10399void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10400{
10401 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010402
10403#if !defined(MBEDTLS_SSL_PROTO_TLS)
10404 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10405#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010406}
10407
Simon Butcherc97b6972015-12-27 23:48:17 +000010408#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010409static int ssl_preset_default_hashes[] = {
10410#if defined(MBEDTLS_SHA512_C)
10411 MBEDTLS_MD_SHA512,
10412 MBEDTLS_MD_SHA384,
10413#endif
10414#if defined(MBEDTLS_SHA256_C)
10415 MBEDTLS_MD_SHA256,
10416 MBEDTLS_MD_SHA224,
10417#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010418#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010419 MBEDTLS_MD_SHA1,
10420#endif
10421 MBEDTLS_MD_NONE
10422};
Simon Butcherc97b6972015-12-27 23:48:17 +000010423#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010424
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010425static int ssl_preset_suiteb_ciphersuites[] = {
10426 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10427 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10428 0
10429};
10430
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010431#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010432static int ssl_preset_suiteb_hashes[] = {
10433 MBEDTLS_MD_SHA256,
10434 MBEDTLS_MD_SHA384,
10435 MBEDTLS_MD_NONE
10436};
10437#endif
10438
10439#if defined(MBEDTLS_ECP_C)
10440static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10441 MBEDTLS_ECP_DP_SECP256R1,
10442 MBEDTLS_ECP_DP_SECP384R1,
10443 MBEDTLS_ECP_DP_NONE
10444};
10445#endif
10446
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010447/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010448 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010449 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010450int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010451 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010452{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010453#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010454 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010455#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010456
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010457 /* Use the functions here so that they are covered in tests,
10458 * but otherwise access member directly for efficiency */
10459 mbedtls_ssl_conf_endpoint( conf, endpoint );
10460 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010461
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010462 /*
10463 * Things that are common to all presets
10464 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010465#if defined(MBEDTLS_SSL_CLI_C)
10466 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10467 {
10468 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10469#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10470 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10471#endif
10472 }
10473#endif
10474
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010475#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010476 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010477#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010478
10479#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10480 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10481#endif
10482
10483#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10484 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010485 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010486 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010487#endif
10488
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010489#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10490 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10491#endif
10492
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010493#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010494 conf->f_cookie_write = ssl_cookie_write_dummy;
10495 conf->f_cookie_check = ssl_cookie_check_dummy;
10496#endif
10497
10498#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10499 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10500#endif
10501
Janos Follath088ce432017-04-10 12:42:31 +010010502#if defined(MBEDTLS_SSL_SRV_C)
10503 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10504#endif
10505
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010506#if defined(MBEDTLS_SSL_PROTO_DTLS)
10507 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10508 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10509#endif
10510
10511#if defined(MBEDTLS_SSL_RENEGOTIATION)
10512 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010513 memset( conf->renego_period, 0x00, 2 );
10514 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010515#endif
10516
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010517#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10518 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10519 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010520 const unsigned char dhm_p[] =
10521 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10522 const unsigned char dhm_g[] =
10523 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10524
Hanno Beckera90658f2017-10-04 15:29:08 +010010525 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10526 dhm_p, sizeof( dhm_p ),
10527 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010528 {
10529 return( ret );
10530 }
10531 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010532#endif
10533
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010534 /*
10535 * Preset-specific defaults
10536 */
10537 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010538 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010539 /*
10540 * NSA Suite B
10541 */
10542 case MBEDTLS_SSL_PRESET_SUITEB:
10543 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10544 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10545 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10546 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10547
10548 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10549 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10550 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10551 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10552 ssl_preset_suiteb_ciphersuites;
10553
10554#if defined(MBEDTLS_X509_CRT_PARSE_C)
10555 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556#endif
10557
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010558#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010559 conf->sig_hashes = ssl_preset_suiteb_hashes;
10560#endif
10561
10562#if defined(MBEDTLS_ECP_C)
10563 conf->curve_list = ssl_preset_suiteb_curves;
10564#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010565 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010566
10567 /*
10568 * Default
10569 */
10570 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010571 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10572 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10573 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10574 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10575 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10576 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10577 MBEDTLS_SSL_MIN_MINOR_VERSION :
10578 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010579 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10580 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10581
10582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010583 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010584 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10585#endif
10586
10587 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10588 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10589 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10590 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10591 mbedtls_ssl_list_ciphersuites();
10592
10593#if defined(MBEDTLS_X509_CRT_PARSE_C)
10594 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10595#endif
10596
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010597#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010598 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010599#endif
10600
10601#if defined(MBEDTLS_ECP_C)
10602 conf->curve_list = mbedtls_ecp_grp_id_list();
10603#endif
10604
10605#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10606 conf->dhm_min_bitlen = 1024;
10607#endif
10608 }
10609
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010610 return( 0 );
10611}
10612
10613/*
10614 * Free mbedtls_ssl_config
10615 */
10616void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10617{
10618#if defined(MBEDTLS_DHM_C)
10619 mbedtls_mpi_free( &conf->dhm_P );
10620 mbedtls_mpi_free( &conf->dhm_G );
10621#endif
10622
10623#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10624 if( conf->psk != NULL )
10625 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010626 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010627 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010628 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010629 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010630 }
10631
10632 if( conf->psk_identity != NULL )
10633 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010634 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010635 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010636 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010637 conf->psk_identity_len = 0;
10638 }
10639#endif
10640
10641#if defined(MBEDTLS_X509_CRT_PARSE_C)
10642 ssl_key_cert_free( conf->key_cert );
10643#endif
10644
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010645 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010646}
10647
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010648#if defined(MBEDTLS_PK_C) && \
10649 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010650/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655#if defined(MBEDTLS_RSA_C)
10656 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10657 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010658#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659#if defined(MBEDTLS_ECDSA_C)
10660 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10661 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010664}
10665
Hanno Becker7e5437a2017-04-28 17:15:26 +010010666unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10667{
10668 switch( type ) {
10669 case MBEDTLS_PK_RSA:
10670 return( MBEDTLS_SSL_SIG_RSA );
10671 case MBEDTLS_PK_ECDSA:
10672 case MBEDTLS_PK_ECKEY:
10673 return( MBEDTLS_SSL_SIG_ECDSA );
10674 default:
10675 return( MBEDTLS_SSL_SIG_ANON );
10676 }
10677}
10678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010680{
10681 switch( sig )
10682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_RSA_C)
10684 case MBEDTLS_SSL_SIG_RSA:
10685 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_ECDSA_C)
10688 case MBEDTLS_SSL_SIG_ECDSA:
10689 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010690#endif
10691 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010692 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010693 }
10694}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010695#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696
Hanno Becker7e5437a2017-04-28 17:15:26 +010010697#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10698 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10699
10700/* Find an entry in a signature-hash set matching a given hash algorithm. */
10701mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10702 mbedtls_pk_type_t sig_alg )
10703{
10704 switch( sig_alg )
10705 {
10706 case MBEDTLS_PK_RSA:
10707 return( set->rsa );
10708 case MBEDTLS_PK_ECDSA:
10709 return( set->ecdsa );
10710 default:
10711 return( MBEDTLS_MD_NONE );
10712 }
10713}
10714
10715/* Add a signature-hash-pair to a signature-hash set */
10716void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10717 mbedtls_pk_type_t sig_alg,
10718 mbedtls_md_type_t md_alg )
10719{
10720 switch( sig_alg )
10721 {
10722 case MBEDTLS_PK_RSA:
10723 if( set->rsa == MBEDTLS_MD_NONE )
10724 set->rsa = md_alg;
10725 break;
10726
10727 case MBEDTLS_PK_ECDSA:
10728 if( set->ecdsa == MBEDTLS_MD_NONE )
10729 set->ecdsa = md_alg;
10730 break;
10731
10732 default:
10733 break;
10734 }
10735}
10736
10737/* Allow exactly one hash algorithm for each signature. */
10738void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10739 mbedtls_md_type_t md_alg )
10740{
10741 set->rsa = md_alg;
10742 set->ecdsa = md_alg;
10743}
10744
10745#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10746 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10747
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010748/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010749 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010752{
10753 switch( hash )
10754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#if defined(MBEDTLS_MD5_C)
10756 case MBEDTLS_SSL_HASH_MD5:
10757 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759#if defined(MBEDTLS_SHA1_C)
10760 case MBEDTLS_SSL_HASH_SHA1:
10761 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763#if defined(MBEDTLS_SHA256_C)
10764 case MBEDTLS_SSL_HASH_SHA224:
10765 return( MBEDTLS_MD_SHA224 );
10766 case MBEDTLS_SSL_HASH_SHA256:
10767 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010768#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010769#if defined(MBEDTLS_SHA512_C)
10770 case MBEDTLS_SSL_HASH_SHA384:
10771 return( MBEDTLS_MD_SHA384 );
10772 case MBEDTLS_SSL_HASH_SHA512:
10773 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010774#endif
10775 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010777 }
10778}
10779
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010780/*
10781 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10782 */
10783unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10784{
10785 switch( md )
10786 {
10787#if defined(MBEDTLS_MD5_C)
10788 case MBEDTLS_MD_MD5:
10789 return( MBEDTLS_SSL_HASH_MD5 );
10790#endif
10791#if defined(MBEDTLS_SHA1_C)
10792 case MBEDTLS_MD_SHA1:
10793 return( MBEDTLS_SSL_HASH_SHA1 );
10794#endif
10795#if defined(MBEDTLS_SHA256_C)
10796 case MBEDTLS_MD_SHA224:
10797 return( MBEDTLS_SSL_HASH_SHA224 );
10798 case MBEDTLS_MD_SHA256:
10799 return( MBEDTLS_SSL_HASH_SHA256 );
10800#endif
10801#if defined(MBEDTLS_SHA512_C)
10802 case MBEDTLS_MD_SHA384:
10803 return( MBEDTLS_SSL_HASH_SHA384 );
10804 case MBEDTLS_MD_SHA512:
10805 return( MBEDTLS_SSL_HASH_SHA512 );
10806#endif
10807 default:
10808 return( MBEDTLS_SSL_HASH_NONE );
10809 }
10810}
10811
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010812#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010813/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010814 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010815 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010816 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010817int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010820
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010821 if( ssl->conf->curve_list == NULL )
10822 return( -1 );
10823
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010824 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010825 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010826 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010827
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010828 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010829}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010830#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010831
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010832#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010833/*
10834 * Check if a hash proposed by the peer is in our list.
10835 * Return 0 if we're willing to use it, -1 otherwise.
10836 */
10837int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10838 mbedtls_md_type_t md )
10839{
10840 const int *cur;
10841
10842 if( ssl->conf->sig_hashes == NULL )
10843 return( -1 );
10844
10845 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10846 if( *cur == (int) md )
10847 return( 0 );
10848
10849 return( -1 );
10850}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010851#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_X509_CRT_PARSE_C)
10854int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10855 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010856 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010857 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010858{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010859 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010861 int usage = 0;
10862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010864 const char *ext_oid;
10865 size_t ext_len;
10866#endif
10867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10869 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010870 ((void) cert);
10871 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010872 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010873#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10876 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010877 {
10878 /* Server part of the key exchange */
10879 switch( ciphersuite->key_exchange )
10880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 case MBEDTLS_KEY_EXCHANGE_RSA:
10882 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010883 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010884 break;
10885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10887 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10888 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10889 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010890 break;
10891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10893 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010894 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010895 break;
10896
10897 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898 case MBEDTLS_KEY_EXCHANGE_NONE:
10899 case MBEDTLS_KEY_EXCHANGE_PSK:
10900 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10901 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010902 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010903 usage = 0;
10904 }
10905 }
10906 else
10907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10909 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010910 }
10911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010913 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010914 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010915 ret = -1;
10916 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010917#else
10918 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010921#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10922 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10925 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010926 }
10927 else
10928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10930 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010931 }
10932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010934 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010935 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010936 ret = -1;
10937 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010939
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010940 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010941}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010942#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010943
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010944/*
10945 * Convert version numbers to/from wire format
10946 * and, for DTLS, to/from TLS equivalent.
10947 *
10948 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010949 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010950 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10951 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010954 unsigned char ver[2] )
10955{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010956#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10957 ((void) transport);
10958#endif
10959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010961 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010964 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10965
10966 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10967 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10968 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010969 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010970#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010971#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010972 {
10973 ver[0] = (unsigned char) major;
10974 ver[1] = (unsigned char) minor;
10975 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010976#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010977}
10978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010980 const unsigned char ver[2] )
10981{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010982#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10983 ((void) transport);
10984#endif
10985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010987 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010988 {
10989 *major = 255 - ver[0] + 2;
10990 *minor = 255 - ver[1] + 1;
10991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010993 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10994 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010995 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010996#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010997#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010998 {
10999 *major = ver[0];
11000 *minor = ver[1];
11001 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011002#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011003}
11004
Simon Butcher99000142016-10-13 17:21:01 +010011005int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11006{
11007#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11008 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11009 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11010
11011 switch( md )
11012 {
11013#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11014#if defined(MBEDTLS_MD5_C)
11015 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011016 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011017#endif
11018#if defined(MBEDTLS_SHA1_C)
11019 case MBEDTLS_SSL_HASH_SHA1:
11020 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11021 break;
11022#endif
11023#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11024#if defined(MBEDTLS_SHA512_C)
11025 case MBEDTLS_SSL_HASH_SHA384:
11026 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11027 break;
11028#endif
11029#if defined(MBEDTLS_SHA256_C)
11030 case MBEDTLS_SSL_HASH_SHA256:
11031 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11032 break;
11033#endif
11034 default:
11035 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11036 }
11037
11038 return 0;
11039#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11040 (void) ssl;
11041 (void) md;
11042
11043 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11044#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11045}
11046
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011047#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11048 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11049int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11050 unsigned char *output,
11051 unsigned char *data, size_t data_len )
11052{
11053 int ret = 0;
11054 mbedtls_md5_context mbedtls_md5;
11055 mbedtls_sha1_context mbedtls_sha1;
11056
11057 mbedtls_md5_init( &mbedtls_md5 );
11058 mbedtls_sha1_init( &mbedtls_sha1 );
11059
11060 /*
11061 * digitally-signed struct {
11062 * opaque md5_hash[16];
11063 * opaque sha_hash[20];
11064 * };
11065 *
11066 * md5_hash
11067 * MD5(ClientHello.random + ServerHello.random
11068 * + ServerParams);
11069 * sha_hash
11070 * SHA(ClientHello.random + ServerHello.random
11071 * + ServerParams);
11072 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011073 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011074 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011076 goto exit;
11077 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011078 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011079 ssl->handshake->randbytes, 64 ) ) != 0 )
11080 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011082 goto exit;
11083 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011084 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011085 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011087 goto exit;
11088 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011089 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011090 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011092 goto exit;
11093 }
11094
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011095 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011096 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011098 goto exit;
11099 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011100 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011101 ssl->handshake->randbytes, 64 ) ) != 0 )
11102 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011104 goto exit;
11105 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011106 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011107 data_len ) ) != 0 )
11108 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011110 goto exit;
11111 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011112 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011113 output + 16 ) ) != 0 )
11114 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011116 goto exit;
11117 }
11118
11119exit:
11120 mbedtls_md5_free( &mbedtls_md5 );
11121 mbedtls_sha1_free( &mbedtls_sha1 );
11122
11123 if( ret != 0 )
11124 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11125 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11126
11127 return( ret );
11128
11129}
11130#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11131 MBEDTLS_SSL_PROTO_TLS1_1 */
11132
11133#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11134 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11135int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011136 unsigned char *hash, size_t *hashlen,
11137 unsigned char *data, size_t data_len,
11138 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011139{
11140 int ret = 0;
11141 mbedtls_md_context_t ctx;
11142 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011143 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011144
11145 mbedtls_md_init( &ctx );
11146
11147 /*
11148 * digitally-signed struct {
11149 * opaque client_random[32];
11150 * opaque server_random[32];
11151 * ServerDHParams params;
11152 * };
11153 */
11154 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11155 {
11156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11157 goto exit;
11158 }
11159 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11160 {
11161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11162 goto exit;
11163 }
11164 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11165 {
11166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11167 goto exit;
11168 }
11169 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11170 {
11171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11172 goto exit;
11173 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011174 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 {
11176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11177 goto exit;
11178 }
11179
11180exit:
11181 mbedtls_md_free( &ctx );
11182
11183 if( ret != 0 )
11184 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11185 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11186
11187 return( ret );
11188}
11189#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11190 MBEDTLS_SSL_PROTO_TLS1_2 */
11191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192#endif /* MBEDTLS_SSL_TLS_C */