blob: d25942bfcd36f8fae91a63b2cfa905171a6a2919 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001266#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001267 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001268 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1270 return( 0 );
1271 }
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03001272#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273
1274 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1275 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
1277#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001278 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1279 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001280 {
1281 unsigned char session_hash[48];
1282 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001284 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001285
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001286 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1287 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001289 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001290 "extended master secret",
1291 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001292 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001293 }
1294 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001296 {
1297 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1298 "master secret",
1299 handshake->randbytes, 64,
1300 master, 48 );
1301 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001302 if( ret != 0 )
1303 {
1304 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1305 return( ret );
1306 }
1307
1308 mbedtls_platform_zeroize( handshake->premaster,
1309 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001310
1311 return( 0 );
1312}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001314int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1315{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001316 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001317 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1318 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1321
1322 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 ret = ssl_set_handshake_prfs( ssl->handshake,
1324 ssl->minor_ver,
1325 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001326 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001327 {
1328 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001329 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001330 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001331
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001332 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001333 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001334 ssl->session_negotiate->master,
1335 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001336 if( ret != 0 )
1337 {
1338 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1339 return( ret );
1340 }
1341
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001342 /* Swap the client and server random values:
1343 * - MS derivation wanted client+server (RFC 5246 8.1)
1344 * - key derivation wants server+client (RFC 5246 6.3) */
1345 {
1346 unsigned char tmp[64];
1347 memcpy( tmp, ssl->handshake->randbytes, 64 );
1348 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1349 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1350 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1351 }
1352
1353 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001354 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001355 ssl->session_negotiate->ciphersuite,
1356 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001357#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001358#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1359 ssl->session_negotiate->encrypt_then_mac,
1360#endif
1361#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1362 ssl->session_negotiate->trunc_hmac,
1363#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001364#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001365#if defined(MBEDTLS_ZLIB_SUPPORT)
1366 ssl->session_negotiate->compression,
1367#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001368 ssl->handshake->tls_prf,
1369 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001370 ssl->minor_ver,
1371 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001372 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001373 if( ret != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1376 return( ret );
1377 }
1378
1379 /* We no longer need Server/ClientHello.random values */
1380 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1381 sizeof( ssl->handshake->randbytes ) );
1382
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001383 /* Allocate compression buffer */
1384#if defined(MBEDTLS_ZLIB_SUPPORT)
1385 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1386 ssl->compress_buf == NULL )
1387 {
1388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1389 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1390 if( ssl->compress_buf == NULL )
1391 {
1392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001393 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001394 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1395 }
1396 }
1397#endif
1398
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1400
1401 return( 0 );
1402}
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001405void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1406 unsigned char hash[36],
1407 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001409 mbedtls_md5_context md5;
1410 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001411 unsigned char pad_1[48];
1412 unsigned char pad_2[48];
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001416 mbedtls_md5_init( &md5 );
1417 mbedtls_sha1_init( &sha1 );
1418
1419 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1420 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Paul Bakker380da532012-04-18 16:10:25 +00001422 memset( pad_1, 0x36, 48 );
1423 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001425 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1426 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1427 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001429 mbedtls_md5_starts_ret( &md5 );
1430 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1431 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1432 mbedtls_md5_update_ret( &md5, hash, 16 );
1433 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001435 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1436 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1437 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001438
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001439 mbedtls_sha1_starts_ret( &sha1 );
1440 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1441 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1442 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1443 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001444
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001445 *hlen = 36;
1446
1447 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001449
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001450 mbedtls_md5_free( &md5 );
1451 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001452
Paul Bakker380da532012-04-18 16:10:25 +00001453 return;
1454}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001458void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1459 unsigned char hash[36],
1460 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001461{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001462 mbedtls_md5_context md5;
1463 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001467 mbedtls_md5_init( &md5 );
1468 mbedtls_sha1_init( &sha1 );
1469
1470 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1471 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001473 mbedtls_md5_finish_ret( &md5, hash );
1474 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001476 *hlen = 36;
1477
1478 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001480
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001481 mbedtls_md5_free( &md5 );
1482 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001483
Paul Bakker380da532012-04-18 16:10:25 +00001484 return;
1485}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1489#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001490void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1491 unsigned char hash[32],
1492 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001493{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001494 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001496 mbedtls_sha256_init( &sha256 );
1497
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001500 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001501 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001502
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001503 *hlen = 32;
1504
1505 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001507
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001508 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001509
Paul Bakker380da532012-04-18 16:10:25 +00001510 return;
1511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001515void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1516 unsigned char hash[48],
1517 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001518{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001519 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001520
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001521 mbedtls_sha512_init( &sha512 );
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001526 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001527
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001528 *hlen = 48;
1529
1530 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001533 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001534
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 return;
1536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_SHA512_C */
1538#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1541int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001542{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543 unsigned char *p = ssl->handshake->premaster;
1544 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001545 const unsigned char *psk = ssl->conf->psk;
1546 size_t psk_len = ssl->conf->psk_len;
1547
1548 /* If the psk callback was called, use its result */
1549 if( ssl->handshake->psk != NULL )
1550 {
1551 psk = ssl->handshake->psk;
1552 psk_len = ssl->handshake->psk_len;
1553 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001554
1555 /*
1556 * PMS = struct {
1557 * opaque other_secret<0..2^16-1>;
1558 * opaque psk<0..2^16-1>;
1559 * };
1560 * with "other_secret" depending on the particular key exchange
1561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1563 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001564 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001565 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001568 *(p++) = (unsigned char)( psk_len >> 8 );
1569 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001570
1571 if( end < p || (size_t)( end - p ) < psk_len )
1572 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1573
1574 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001575 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576 }
1577 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1579#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1580 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001581 {
1582 /*
1583 * other_secret already set by the ClientKeyExchange message,
1584 * and is 48 bytes long
1585 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001586 if( end - p < 2 )
1587 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1588
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001589 *p++ = 0;
1590 *p++ = 48;
1591 p += 48;
1592 }
1593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1595#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1596 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001598 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001599 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001600
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001601 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001603 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001604 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001607 return( ret );
1608 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001609 *(p++) = (unsigned char)( len >> 8 );
1610 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 p += len;
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001614 }
1615 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1617#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1618 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001620 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001621 size_t zlen;
1622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001624 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001625 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 return( ret );
1629 }
1630
1631 *(p++) = (unsigned char)( zlen >> 8 );
1632 *(p++) = (unsigned char)( zlen );
1633 p += zlen;
1634
Janos Follath3fbdada2018-08-15 10:26:53 +01001635 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1636 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 }
1638 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001643 }
1644
1645 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001646 if( end - p < 2 )
1647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001648
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001649 *(p++) = (unsigned char)( psk_len >> 8 );
1650 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001651
1652 if( end < p || (size_t)( end - p ) < psk_len )
1653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1654
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001655 memcpy( p, psk, psk_len );
1656 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001657
1658 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1659
1660 return( 0 );
1661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665/*
1666 * SSLv3.0 MAC functions
1667 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001668#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001669static void ssl_mac( mbedtls_md_context_t *md_ctx,
1670 const unsigned char *secret,
1671 const unsigned char *buf, size_t len,
1672 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001673 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001674{
1675 unsigned char header[11];
1676 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001677 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1679 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001680
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001681 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001683 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001684 else
Paul Bakker68884e32013-01-07 18:20:04 +01001685 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001686
1687 memcpy( header, ctr, 8 );
1688 header[ 8] = (unsigned char) type;
1689 header[ 9] = (unsigned char)( len >> 8 );
1690 header[10] = (unsigned char)( len );
1691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_md_starts( md_ctx );
1694 mbedtls_md_update( md_ctx, secret, md_size );
1695 mbedtls_md_update( md_ctx, padding, padlen );
1696 mbedtls_md_update( md_ctx, header, 11 );
1697 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001698 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Paul Bakker68884e32013-01-07 18:20:04 +01001700 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_md_starts( md_ctx );
1702 mbedtls_md_update( md_ctx, secret, md_size );
1703 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001704 mbedtls_md_update( md_ctx, out, md_size );
1705 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001706}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001708
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001709/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001710 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001711#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001712 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1713 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1714 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1715/* This function makes sure every byte in the memory region is accessed
1716 * (in ascending addresses order) */
1717static void ssl_read_memory( unsigned char *p, size_t len )
1718{
1719 unsigned char acc = 0;
1720 volatile unsigned char force;
1721
1722 for( ; len != 0; p++, len-- )
1723 acc ^= *p;
1724
1725 force = acc;
1726 (void) force;
1727}
1728#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1729
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001730/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001732 */
Hanno Becker3307b532017-12-27 21:37:21 +00001733
Hanno Beckera5a2b082019-05-15 14:03:01 +01001734#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001735/* This functions transforms a DTLS plaintext fragment and a record content
1736 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001737 *
1738 * struct {
1739 * opaque content[DTLSPlaintext.length];
1740 * ContentType real_type;
1741 * uint8 zeros[length_of_padding];
1742 * } DTLSInnerPlaintext;
1743 *
1744 * Input:
1745 * - `content`: The beginning of the buffer holding the
1746 * plaintext to be wrapped.
1747 * - `*content_size`: The length of the plaintext in Bytes.
1748 * - `max_len`: The number of Bytes available starting from
1749 * `content`. This must be `>= *content_size`.
1750 * - `rec_type`: The desired record content type.
1751 *
1752 * Output:
1753 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1754 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1755 *
1756 * Returns:
1757 * - `0` on success.
1758 * - A negative error code if `max_len` didn't offer enough space
1759 * for the expansion.
1760 */
1761static int ssl_cid_build_inner_plaintext( unsigned char *content,
1762 size_t *content_size,
1763 size_t remaining,
1764 uint8_t rec_type )
1765{
1766 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001767 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1768 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1769 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001770
1771 /* Write real content type */
1772 if( remaining == 0 )
1773 return( -1 );
1774 content[ len ] = rec_type;
1775 len++;
1776 remaining--;
1777
1778 if( remaining < pad )
1779 return( -1 );
1780 memset( content + len, 0, pad );
1781 len += pad;
1782 remaining -= pad;
1783
1784 *content_size = len;
1785 return( 0 );
1786}
1787
Hanno Becker7dc25772019-05-20 15:08:01 +01001788/* This function parses a DTLSInnerPlaintext structure.
1789 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001790static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1791 size_t *content_size,
1792 uint8_t *rec_type )
1793{
1794 size_t remaining = *content_size;
1795
1796 /* Determine length of padding by skipping zeroes from the back. */
1797 do
1798 {
1799 if( remaining == 0 )
1800 return( -1 );
1801 remaining--;
1802 } while( content[ remaining ] == 0 );
1803
1804 *content_size = remaining;
1805 *rec_type = content[ remaining ];
1806
1807 return( 0 );
1808}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001809#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001810
Hanno Becker99abf512019-05-20 14:50:53 +01001811/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001812 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001813static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001814 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001815 mbedtls_record *rec )
1816{
Hanno Becker99abf512019-05-20 14:50:53 +01001817 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001818 *
1819 * additional_data = seq_num + TLSCompressed.type +
1820 * TLSCompressed.version + TLSCompressed.length;
1821 *
Hanno Becker99abf512019-05-20 14:50:53 +01001822 * For the CID extension, this is extended as follows
1823 * (quoting draft-ietf-tls-dtls-connection-id-05,
1824 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001825 *
1826 * additional_data = seq_num + DTLSPlaintext.type +
1827 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001828 * cid +
1829 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001830 * length_of_DTLSInnerPlaintext;
1831 */
1832
Hanno Becker3307b532017-12-27 21:37:21 +00001833 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1834 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001835 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001836
Hanno Beckera5a2b082019-05-15 14:03:01 +01001837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001838 if( rec->cid_len != 0 )
1839 {
1840 memcpy( add_data + 11, rec->cid, rec->cid_len );
1841 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1842 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1843 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1844 *add_data_len = 13 + 1 + rec->cid_len;
1845 }
1846 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001847#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001848 {
1849 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1850 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1851 *add_data_len = 13;
1852 }
Hanno Becker3307b532017-12-27 21:37:21 +00001853}
1854
Hanno Becker611a83b2018-01-03 14:27:32 +00001855int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1856 mbedtls_ssl_transform *transform,
1857 mbedtls_record *rec,
1858 int (*f_rng)(void *, unsigned char *, size_t),
1859 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001862 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001863 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001864 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001865 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001866 size_t post_avail;
1867
1868 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001869#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001870 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001871 ((void) ssl);
1872#endif
1873
1874 /* The PRNG is used for dynamic IV generation that's used
1875 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1876#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1877 ( defined(MBEDTLS_AES_C) || \
1878 defined(MBEDTLS_ARIA_C) || \
1879 defined(MBEDTLS_CAMELLIA_C) ) && \
1880 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1881 ((void) f_rng);
1882 ((void) p_rng);
1883#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
Hanno Becker3307b532017-12-27 21:37:21 +00001887 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001888 {
Hanno Becker3307b532017-12-27 21:37:21 +00001889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1891 }
Hanno Becker505089d2019-05-01 09:45:57 +01001892 if( rec == NULL
1893 || rec->buf == NULL
1894 || rec->buf_len < rec->data_offset
1895 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001897 || rec->cid_len != 0
1898#endif
1899 )
Hanno Becker3307b532017-12-27 21:37:21 +00001900 {
1901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001903 }
1904
Hanno Becker3307b532017-12-27 21:37:21 +00001905 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001906 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001908 data, rec->data_len );
1909
1910 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1911
1912 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1913 {
1914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1915 (unsigned) rec->data_len,
1916 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1917 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1918 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001919
Hanno Beckera5a2b082019-05-15 14:03:01 +01001920#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001921 /*
1922 * Add CID information
1923 */
1924 rec->cid_len = transform->out_cid_len;
1925 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1926 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001927
1928 if( rec->cid_len != 0 )
1929 {
1930 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001931 * Wrap plaintext into DTLSInnerPlaintext structure.
1932 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001933 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001934 * Note that this changes `rec->data_len`, and hence
1935 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001936 */
1937 if( ssl_cid_build_inner_plaintext( data,
1938 &rec->data_len,
1939 post_avail,
1940 rec->type ) != 0 )
1941 {
1942 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1943 }
1944
1945 rec->type = MBEDTLS_SSL_MSG_CID;
1946 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001947#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001948
Hanno Becker92c930f2019-04-29 17:31:37 +01001949 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1950
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001952 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001954#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( mode == MBEDTLS_MODE_STREAM ||
1956 ( mode == MBEDTLS_MODE_CBC
1957#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001958 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001959#endif
1960 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 {
Hanno Becker3307b532017-12-27 21:37:21 +00001962 if( post_avail < transform->maclen )
1963 {
1964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1965 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1966 }
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001969 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001971 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001972 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1973 data, rec->data_len, rec->ctr, rec->type, mac );
1974 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975 }
1976 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1979 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001980 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001981 {
Hanno Becker992b6872017-11-09 18:57:39 +00001982 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1983
Hanno Beckere83efe62019-04-29 13:52:53 +01001984 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001985
Hanno Becker3307b532017-12-27 21:37:21 +00001986 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001987 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001988 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1989 data, rec->data_len );
1990 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1991 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1992
1993 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001994 }
1995 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001996#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002000 }
2001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2003 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002004
Hanno Becker3307b532017-12-27 21:37:21 +00002005 rec->data_len += transform->maclen;
2006 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002007 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002008 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002009#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002010
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002011 /*
2012 * Encrypt
2013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2015 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002017 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002018 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002020 "including %d bytes of padding",
2021 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2024 transform->iv_enc, transform->ivlen,
2025 data, rec->data_len,
2026 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002029 return( ret );
2030 }
2031
Hanno Becker3307b532017-12-27 21:37:21 +00002032 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 }
Paul Bakker68884e32013-01-07 18:20:04 +01002038 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002040
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041#if defined(MBEDTLS_GCM_C) || \
2042 defined(MBEDTLS_CCM_C) || \
2043 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002045 mode == MBEDTLS_MODE_CCM ||
2046 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002047 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002048 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002049 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002050 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Hanno Becker3307b532017-12-27 21:37:21 +00002052 /* Check that there's space for both the authentication tag
2053 * and the explicit IV before and after the record content. */
2054 if( post_avail < transform->taglen ||
2055 rec->data_offset < explicit_iv_len )
2056 {
2057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2058 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2059 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002060
Paul Bakker68884e32013-01-07 18:20:04 +01002061 /*
2062 * Generate IV
2063 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002068 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2069 explicit_iv_len );
2070 /* Prefix record content with explicit IV. */
2071 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002072 }
2073 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2074 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002075 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002076 unsigned char i;
2077
2078 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2079
2080 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002081 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 }
2083 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002084 {
2085 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002088 }
2089
Hanno Beckere83efe62019-04-29 13:52:53 +01002090 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002091
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2093 iv, transform->ivlen );
2094 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002095 data - explicit_iv_len, explicit_iv_len );
2096 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002097 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002099 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002100 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002101
Paul Bakker68884e32013-01-07 18:20:04 +01002102 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002103 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002104 */
Hanno Becker3307b532017-12-27 21:37:21 +00002105
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002106 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002107 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002108 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002109 data, rec->data_len, /* source */
2110 data, &rec->data_len, /* destination */
2111 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002114 return( ret );
2115 }
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2118 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002119
Hanno Becker3307b532017-12-27 21:37:21 +00002120 rec->data_len += transform->taglen + explicit_iv_len;
2121 rec->data_offset -= explicit_iv_len;
2122 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002123 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2127#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002128 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002131 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002132 size_t padlen, i;
2133 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134
Hanno Becker3307b532017-12-27 21:37:21 +00002135 /* Currently we're always using minimal padding
2136 * (up to 255 bytes would be allowed). */
2137 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2138 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 padlen = 0;
2140
Hanno Becker3307b532017-12-27 21:37:21 +00002141 /* Check there's enough space in the buffer for the padding. */
2142 if( post_avail < padlen + 1 )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2145 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2146 }
2147
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002149 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 rec->data_len += padlen + 1;
2152 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002156 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2157 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 */
Hanno Becker3307b532017-12-27 21:37:21 +00002159 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002160 {
Hanno Becker3307b532017-12-27 21:37:21 +00002161 if( f_rng == NULL )
2162 {
2163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2165 }
2166
2167 if( rec->data_offset < transform->ivlen )
2168 {
2169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2170 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2171 }
2172
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173 /*
2174 * Generate IV
2175 */
Hanno Becker3307b532017-12-27 21:37:21 +00002176 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002177 if( ret != 0 )
2178 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179
Hanno Becker3307b532017-12-27 21:37:21 +00002180 memcpy( data - transform->ivlen, transform->iv_enc,
2181 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002182
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002187 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002188 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002189 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190
Hanno Becker3307b532017-12-27 21:37:21 +00002191 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2192 transform->iv_enc,
2193 transform->ivlen,
2194 data, rec->data_len,
2195 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002198 return( ret );
2199 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002200
Hanno Becker3307b532017-12-27 21:37:21 +00002201 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002205 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002208 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002209 {
2210 /*
2211 * Save IV in SSL3 and TLS1
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2214 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
Hanno Becker3307b532017-12-27 21:37:21 +00002216 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002217#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002218 {
2219 data -= transform->ivlen;
2220 rec->data_offset -= transform->ivlen;
2221 rec->data_len += transform->ivlen;
2222 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002227 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2228
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002229 /*
2230 * MAC(MAC_write_key, seq_num +
2231 * TLSCipherText.type +
2232 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002233 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234 * IV + // except for TLS 1.0
2235 * ENC(content + padding + padding_length));
2236 */
Hanno Becker3307b532017-12-27 21:37:21 +00002237
2238 if( post_avail < transform->maclen)
2239 {
2240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2241 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2242 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Hanno Beckere83efe62019-04-29 13:52:53 +01002244 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002247 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002248 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002251 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002252 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2253 data, rec->data_len );
2254 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2255 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002256
Hanno Becker3307b532017-12-27 21:37:21 +00002257 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002258
Hanno Becker3307b532017-12-27 21:37:21 +00002259 rec->data_len += transform->maclen;
2260 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002261 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002265 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002267 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002273 /* Make extra sure authentication was performed, exactly once */
2274 if( auth_done != 1 )
2275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2277 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278 }
2279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
2282 return( 0 );
2283}
2284
Hanno Becker611a83b2018-01-03 14:27:32 +00002285int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2286 mbedtls_ssl_transform *transform,
2287 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002291 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002292#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002293 size_t padlen = 0, correct = 1;
2294#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002296 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002297 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298
Hanno Becker611a83b2018-01-03 14:27:32 +00002299#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002300 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002301 ((void) ssl);
2302#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002305 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002306 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2309 }
2310 if( rec == NULL ||
2311 rec->buf == NULL ||
2312 rec->buf_len < rec->data_offset ||
2313 rec->buf_len - rec->data_offset < rec->data_len )
2314 {
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002317 }
2318
Hanno Becker4c6876b2017-12-27 21:28:58 +00002319 data = rec->buf + rec->data_offset;
2320 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002323 /*
2324 * Match record's CID with incoming CID.
2325 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002326 if( rec->cid_len != transform->in_cid_len ||
2327 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2328 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002329 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002330 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002331#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2334 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002335 {
2336 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002337 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2338 transform->iv_dec,
2339 transform->ivlen,
2340 data, rec->data_len,
2341 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002344 return( ret );
2345 }
2346
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002352 }
Paul Bakker68884e32013-01-07 18:20:04 +01002353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355#if defined(MBEDTLS_GCM_C) || \
2356 defined(MBEDTLS_CCM_C) || \
2357 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002359 mode == MBEDTLS_MODE_CCM ||
2360 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002361 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002362 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002364
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 /*
2366 * Compute and update sizes
2367 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002368 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002371 "+ taglen (%d)", rec->data_len,
2372 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002374 }
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 /*
2377 * Prepare IV
2378 */
2379 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2380 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002381 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002383 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 }
2386 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2387 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002388 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002389 unsigned char i;
2390
2391 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2392
2393 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002395 }
2396 else
2397 {
2398 /* Reminder if we ever add an AEAD mode with a different size */
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2400 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2401 }
2402
Hanno Becker4c6876b2017-12-27 21:28:58 +00002403 data += explicit_iv_len;
2404 rec->data_offset += explicit_iv_len;
2405 rec->data_len -= explicit_iv_len + transform->taglen;
2406
Hanno Beckere83efe62019-04-29 13:52:53 +01002407 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002408 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002409 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410
2411 memcpy( transform->iv_dec + transform->fixed_ivlen,
2412 data - explicit_iv_len, explicit_iv_len );
2413
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002414 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002416 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002417
Paul Bakker68884e32013-01-07 18:20:04 +01002418
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002420 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002421 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002422 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2423 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002424 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002425 data, rec->data_len,
2426 data, &olen,
2427 data + rec->data_len,
2428 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2433 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 return( ret );
2436 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002437 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002438
Hanno Becker4c6876b2017-12-27 21:28:58 +00002439 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2442 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002443 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002444 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2447#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002448 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002451 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002452
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 /*
Paul Bakker45829992013-01-03 14:52:21 +01002454 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002457 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2458 {
2459 /* The ciphertext is prefixed with the CBC IV. */
2460 minlen += transform->ivlen;
2461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002462#endif
Paul Bakker45829992013-01-03 14:52:21 +01002463
Hanno Becker4c6876b2017-12-27 21:28:58 +00002464 /* Size considerations:
2465 *
2466 * - The CBC cipher text must not be empty and hence
2467 * at least of size transform->ivlen.
2468 *
2469 * Together with the potential IV-prefix, this explains
2470 * the first of the two checks below.
2471 *
2472 * - The record must contain a MAC, either in plain or
2473 * encrypted, depending on whether Encrypt-then-MAC
2474 * is used or not.
2475 * - If it is, the message contains the IV-prefix,
2476 * the CBC ciphertext, and the MAC.
2477 * - If it is not, the padded plaintext, and hence
2478 * the CBC ciphertext, has at least length maclen + 1
2479 * because there is at least the padding length byte.
2480 *
2481 * As the CBC ciphertext is not empty, both cases give the
2482 * lower bound minlen + maclen + 1 on the record size, which
2483 * we test for in the second check below.
2484 */
2485 if( rec->data_len < minlen + transform->ivlen ||
2486 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002489 "+ 1 ) ( + expl IV )", rec->data_len,
2490 transform->ivlen,
2491 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002493 }
2494
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495 /*
2496 * Authenticate before decrypt if enabled
2497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002499 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 {
Hanno Becker992b6872017-11-09 18:57:39 +00002501 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002504
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2506 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
Hanno Beckere83efe62019-04-29 13:52:53 +01002508 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Beckere83efe62019-04-29 13:52:53 +01002510 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2511 add_data_len );
2512 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2513 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002514 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2515 data, rec->data_len );
2516 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2517 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002518
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2520 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002521 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002522 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2525 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002530 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533
2534 /*
2535 * Check length sanity
2536 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002542 }
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002546 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002548 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002550 /* This is safe because data_len >= minlen + maclen + 1 initially,
2551 * and at this point we have at most subtracted maclen (note that
2552 * minlen == transform->ivlen here). */
2553 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002554
Hanno Becker4c6876b2017-12-27 21:28:58 +00002555 data += transform->ivlen;
2556 rec->data_offset += transform->ivlen;
2557 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2562 transform->iv_dec, transform->ivlen,
2563 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002566 return( ret );
2567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002568
Hanno Becker4c6876b2017-12-27 21:28:58 +00002569 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002576 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002577 {
2578 /*
2579 * Save IV in SSL3 and TLS1
2580 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002581 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2582 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Hanno Becker4c6876b2017-12-27 21:28:58 +00002586 /* Safe since data_len >= minlen + maclen + 1, so after having
2587 * subtracted at most minlen and maclen up to this point,
2588 * data_len > 0. */
2589 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002590
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( auth_done == 1 )
2592 {
2593 correct *= ( rec->data_len >= padlen + 1 );
2594 padlen *= ( rec->data_len >= padlen + 1 );
2595 }
2596 else
Paul Bakker45829992013-01-03 14:52:21 +01002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599 if( rec->data_len < transform->maclen + padlen + 1 )
2600 {
2601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2602 rec->data_len,
2603 transform->maclen,
2604 padlen + 1 ) );
2605 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002606#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607
2608 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2609 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002610 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002611
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 padlen++;
2613
2614 /* Regardless of the validity of the padding,
2615 * we have data_len >= padlen here. */
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002618 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002620 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SSL_DEBUG_ALL)
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002624 "should be no more than %d",
2625 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002626#endif
Paul Bakker45829992013-01-03 14:52:21 +01002627 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 }
2629 }
2630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2632#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2633 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002636 /* The padding check involves a series of up to 256
2637 * consecutive memory reads at the end of the record
2638 * plaintext buffer. In order to hide the length and
2639 * validity of the padding, always perform exactly
2640 * `min(256,plaintext_len)` reads (but take into account
2641 * only the last `padlen` bytes for the padding check). */
2642 size_t pad_count = 0;
2643 size_t real_count = 0;
2644 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002645
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 /* Index of first padding byte; it has been ensured above
2647 * that the subtraction is safe. */
2648 size_t const padding_idx = rec->data_len - padlen;
2649 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2650 size_t const start_idx = rec->data_len - num_checks;
2651 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002652
Hanno Becker4c6876b2017-12-27 21:28:58 +00002653 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002654 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002655 real_count |= ( idx >= padding_idx );
2656 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002657 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002658 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002661 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002663#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002664 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002666 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2668 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002672 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002673
Hanno Becker4c6876b2017-12-27 21:28:58 +00002674 /* If the padding was found to be invalid, padlen == 0
2675 * and the subtraction is safe. If the padding was found valid,
2676 * padlen hasn't been changed and the previous assertion
2677 * data_len >= padlen still holds. */
2678 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002680 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002682 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002686 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002688#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002690 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002691#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002694 * Authenticate if not done yet.
2695 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002697#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002698 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002699 {
Hanno Becker992b6872017-11-09 18:57:39 +00002700 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002701
Hanno Becker4c6876b2017-12-27 21:28:58 +00002702 /* If the initial value of padlen was such that
2703 * data_len < maclen + padlen + 1, then padlen
2704 * got reset to 1, and the initial check
2705 * data_len >= minlen + maclen + 1
2706 * guarantees that at this point we still
2707 * have at least data_len >= maclen.
2708 *
2709 * If the initial value of padlen was such that
2710 * data_len >= maclen + padlen + 1, then we have
2711 * subtracted either padlen + 1 (if the padding was correct)
2712 * or 0 (if the padding was incorrect) since then,
2713 * hence data_len >= maclen in any case.
2714 */
2715 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Hanno Beckere83efe62019-04-29 13:52:53 +01002717 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002720 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002721 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002722 ssl_mac( &transform->md_ctx_dec,
2723 transform->mac_dec,
2724 data, rec->data_len,
2725 rec->ctr, rec->type,
2726 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002727 }
2728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002729#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2730#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2731 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002732 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002733 {
2734 /*
2735 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002736 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002737 *
2738 * Known timing attacks:
2739 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2740 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002741 * To compensate for different timings for the MAC calculation
2742 * depending on how much padding was removed (which is determined
2743 * by padlen), process extra_run more blocks through the hash
2744 * function.
2745 *
2746 * The formula in the paper is
2747 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2748 * where L1 is the size of the header plus the decrypted message
2749 * plus CBC padding and L2 is the size of the header plus the
2750 * decrypted message. This is for an underlying hash function
2751 * with 64-byte blocks.
2752 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2753 * correctly. We round down instead of up, so -56 is the correct
2754 * value for our calculations instead of -55.
2755 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002756 * Repeat the formula rather than defining a block_size variable.
2757 * This avoids requiring division by a variable at runtime
2758 * (which would be marginally less efficient and would require
2759 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002760 */
2761 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002762 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002763
2764 /*
2765 * The next two sizes are the minimum and maximum values of
2766 * in_msglen over all padlen values.
2767 *
2768 * They're independent of padlen, since we previously did
2769 * in_msglen -= padlen.
2770 *
2771 * Note that max_len + maclen is never more than the buffer
2772 * length, as we previously did in_msglen -= maclen too.
2773 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002774 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002775 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2776
Hanno Becker4c6876b2017-12-27 21:28:58 +00002777 memset( tmp, 0, sizeof( tmp ) );
2778
2779 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002780 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002781#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2782 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 case MBEDTLS_MD_MD5:
2784 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002786 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 extra_run =
2788 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2789 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 break;
2791#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002792#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002795 extra_run =
2796 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2797 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002798 break;
2799#endif
2800 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002802 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2803 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002804
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002806
Hanno Beckere83efe62019-04-29 13:52:53 +01002807 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2808 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002809 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2810 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002811 /* Make sure we access everything even when padlen > 0. This
2812 * makes the synchronisation requirements for just-in-time
2813 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 ssl_read_memory( data + rec->data_len, padlen );
2815 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002816
2817 /* Call mbedtls_md_process at least once due to cache attacks
2818 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002819 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002821
Hanno Becker4c6876b2017-12-27 21:28:58 +00002822 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002823
2824 /* Make sure we access all the memory that could contain the MAC,
2825 * before we check it in the next code block. This makes the
2826 * synchronisation requirements for just-in-time Prime+Probe
2827 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002828 ssl_read_memory( data + min_len,
2829 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002830 }
2831 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2833 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2836 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002838
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002839#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2841 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2845 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847#if defined(MBEDTLS_SSL_DEBUG_ALL)
2848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 correct = 0;
2851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002853 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002854
2855 /*
2856 * Finally check the correct flag
2857 */
2858 if( correct == 0 )
2859 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002860#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002861
2862 /* Make extra sure authentication was performed, exactly once */
2863 if( auth_done != 1 )
2864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002870 if( rec->cid_len != 0 )
2871 {
2872 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2873 &rec->type );
2874 if( ret != 0 )
2875 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2876 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002877#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880
2881 return( 0 );
2882}
2883
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002884#undef MAC_NONE
2885#undef MAC_PLAINTEXT
2886#undef MAC_CIPHERTEXT
2887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889/*
2890 * Compression/decompression functions
2891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893{
2894 int ret;
2895 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002896 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002898 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002901
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002902 if( len_pre == 0 )
2903 return( 0 );
2904
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 memcpy( msg_pre, ssl->out_msg, len_pre );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msglen ) );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911 ssl->out_msg, ssl->out_msglen );
2912
Paul Bakker48916f92012-09-16 19:57:18 +00002913 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2914 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2915 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Paul Bakker48916f92012-09-16 19:57:18 +00002918 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919 if( ret != Z_OK )
2920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2922 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 }
2924
Angus Grattond8213d02016-05-25 20:56:48 +10002925 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002926 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002929 ssl->out_msglen ) );
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932 ssl->out_msg, ssl->out_msglen );
2933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935
2936 return( 0 );
2937}
2938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940{
2941 int ret;
2942 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002943 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002945 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002949 if( len_pre == 0 )
2950 return( 0 );
2951
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 memcpy( msg_pre, ssl->in_msg, len_pre );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 ssl->in_msglen ) );
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 ssl->in_msg, ssl->in_msglen );
2959
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2961 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2962 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002963 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002964 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965
Paul Bakker48916f92012-09-16 19:57:18 +00002966 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967 if( ret != Z_OK )
2968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2970 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 }
2972
Angus Grattond8213d02016-05-25 20:56:48 +10002973 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002974 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002977 ssl->in_msglen ) );
2978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002980 ssl->in_msg, ssl->in_msglen );
2981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002983
2984 return( 0 );
2985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002988#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2989static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#if defined(MBEDTLS_SSL_PROTO_DTLS)
2992static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002993{
2994 /* If renegotiation is not enforced, retransmit until we would reach max
2995 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002996 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002997 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002998 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002999 unsigned char doublings = 1;
3000
3001 while( ratio != 0 )
3002 {
3003 ++doublings;
3004 ratio >>= 1;
3005 }
3006
3007 if( ++ssl->renego_records_seen > doublings )
3008 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003010 return( 0 );
3011 }
3012 }
3013
3014 return( ssl_write_hello_request( ssl ) );
3015}
3016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003018
Paul Bakker5121ce52009-01-03 21:22:43 +00003019/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003020 * Fill the input message buffer by appending data to it.
3021 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003022 *
3023 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3024 * available (from this read and/or a previous one). Otherwise, an error code
3025 * is returned (possibly EOF or WANT_READ).
3026 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003027 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3028 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3029 * since we always read a whole datagram at once.
3030 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003031 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003032 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003035{
Paul Bakker23986e52011-04-24 08:57:21 +00003036 int ret;
3037 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003041 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003044 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003046 }
3047
Angus Grattond8213d02016-05-25 20:56:48 +10003048 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003052 }
3053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003055 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003057 uint32_t timeout;
3058
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003059 /* Just to be sure */
3060 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3061 {
3062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3063 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3065 }
3066
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003067 /*
3068 * The point is, we need to always read a full datagram at once, so we
3069 * sometimes read more then requested, and handle the additional data.
3070 * It could be the rest of the current record (while fetching the
3071 * header) and/or some other records in the same datagram.
3072 */
3073
3074 /*
3075 * Move to the next record in the already read datagram if applicable
3076 */
3077 if( ssl->next_record_offset != 0 )
3078 {
3079 if( ssl->in_left < ssl->next_record_offset )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3082 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003083 }
3084
3085 ssl->in_left -= ssl->next_record_offset;
3086
3087 if( ssl->in_left != 0 )
3088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003090 ssl->next_record_offset ) );
3091 memmove( ssl->in_hdr,
3092 ssl->in_hdr + ssl->next_record_offset,
3093 ssl->in_left );
3094 }
3095
3096 ssl->next_record_offset = 0;
3097 }
3098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003100 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003103 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003104 */
3105 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003108 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003109 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003110
3111 /*
3112 * A record can't be split accross datagrams. If we need to read but
3113 * are not at the beginning of a new record, the caller did something
3114 * wrong.
3115 */
3116 if( ssl->in_left != 0 )
3117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3119 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003120 }
3121
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003122 /*
3123 * Don't even try to read if time's out already.
3124 * This avoids by-passing the timer when repeatedly receiving messages
3125 * that will end up being dropped.
3126 */
3127 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003128 {
3129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003130 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003131 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003132 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003133 {
Angus Grattond8213d02016-05-25 20:56:48 +10003134 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003137 timeout = ssl->handshake->retransmit_timeout;
3138 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003139 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003143 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3145 timeout );
3146 else
3147 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150
3151 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003153 }
3154
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003155 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003158 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003161 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003166 }
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003171 return( ret );
3172 }
3173
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003174 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003177 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003180 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003183 return( ret );
3184 }
3185
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003186 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003189 }
3190
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 if( ret < 0 )
3192 return( ret );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 ssl->in_left = ret;
3195 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003196 MBEDTLS_SSL_TRANSPORT_ELSE
3197#endif /* MBEDTLS_SSL_PROTO_DTLS */
3198#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003201 ssl->in_left, nb_want ) );
3202
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203 while( ssl->in_left < nb_want )
3204 {
3205 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003206
3207 if( ssl_check_timer( ssl ) != 0 )
3208 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3209 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003210 {
3211 if( ssl->f_recv_timeout != NULL )
3212 {
3213 ret = ssl->f_recv_timeout( ssl->p_bio,
3214 ssl->in_hdr + ssl->in_left, len,
3215 ssl->conf->read_timeout );
3216 }
3217 else
3218 {
3219 ret = ssl->f_recv( ssl->p_bio,
3220 ssl->in_hdr + ssl->in_left, len );
3221 }
3222 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003225 ssl->in_left, nb_want ) );
3226 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003227
3228 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003229 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003230
3231 if( ret < 0 )
3232 return( ret );
3233
mohammad160352aecb92018-03-28 23:41:40 -07003234 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003235 {
Darryl Green11999bb2018-03-13 15:22:58 +00003236 MBEDTLS_SSL_DEBUG_MSG( 1,
3237 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003238 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3240 }
3241
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003242 ssl->in_left += ret;
3243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003245#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248
3249 return( 0 );
3250}
3251
3252/*
3253 * Flush any data not yet written
3254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003256{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003257 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003258 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003262 if( ssl->f_send == NULL )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003265 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003267 }
3268
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003269 /* Avoid incrementing counter if data is flushed */
3270 if( ssl->out_left == 0 )
3271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003273 return( 0 );
3274 }
3275
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 while( ssl->out_left > 0 )
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003279 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Hanno Becker2b1e3542018-08-06 11:19:13 +01003281 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003282 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003284 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285
3286 if( ret <= 0 )
3287 return( ret );
3288
mohammad160352aecb92018-03-28 23:41:40 -07003289 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003290 {
Darryl Green11999bb2018-03-13 15:22:58 +00003291 MBEDTLS_SSL_DEBUG_MSG( 1,
3292 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003293 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3295 }
3296
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 ssl->out_left -= ret;
3298 }
3299
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003301 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003302 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003305 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003306#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003307#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003308 {
3309 ssl->out_hdr = ssl->out_buf + 8;
3310 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003311#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003312 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
3316 return( 0 );
3317}
3318
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003319/*
3320 * Functions to handle the DTLS retransmission state machine
3321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323/*
3324 * Append current handshake message to current outgoing flight
3325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3330 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3331 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332
3333 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003334 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003338 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339 }
3340
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003341 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003345 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003346 }
3347
3348 /* Copy current handshake message with headers */
3349 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3350 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003351 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 msg->next = NULL;
3353
3354 /* Append to the current flight */
3355 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003356 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 else
3358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003360 while( cur->next != NULL )
3361 cur = cur->next;
3362 cur->next = msg;
3363 }
3364
Hanno Becker3b235902018-08-06 09:54:53 +01003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003366 return( 0 );
3367}
3368
3369/*
3370 * Free the current flight of handshake messages
3371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 mbedtls_ssl_flight_item *cur = flight;
3375 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003376
3377 while( cur != NULL )
3378 {
3379 next = cur->next;
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_free( cur->p );
3382 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383
3384 cur = next;
3385 }
3386}
3387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3389static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003390#endif
3391
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003393 * Swap transform_out and out_ctr with the alternative ones
3394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398 unsigned char tmp_out_ctr[8];
3399
3400 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003403 return;
3404 }
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003409 tmp_transform = ssl->transform_out;
3410 ssl->transform_out = ssl->handshake->alt_transform_out;
3411 ssl->handshake->alt_transform_out = tmp_transform;
3412
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003413 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003414 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3415 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003416 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417
3418 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003419 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3422 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3427 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003428 }
3429 }
3430#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431}
3432
3433/*
3434 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 */
3436int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3437{
3438 int ret = 0;
3439
3440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3441
3442 ret = mbedtls_ssl_flight_transmit( ssl );
3443
3444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3445
3446 return( ret );
3447}
3448
3449/*
3450 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451 *
3452 * Need to remember the current message in case flush_output returns
3453 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003456int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003457{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003458 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003464
3465 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003467 ssl_swap_epochs( ssl );
3468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471
3472 while( ssl->handshake->cur_msg != NULL )
3473 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003475 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003476
Hanno Beckere1dcb032018-08-17 16:47:58 +01003477 int const is_finished =
3478 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3479 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3480
Hanno Becker04da1892018-08-14 13:22:10 +01003481 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3482 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3483
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003484 /* Swap epochs before sending Finished: we can't do it after
3485 * sending ChangeCipherSpec, in case write returns WANT_READ.
3486 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003487 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003488 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003490 ssl_swap_epochs( ssl );
3491 }
3492
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 ret = ssl_get_remaining_payload_in_datagram( ssl );
3494 if( ret < 0 )
3495 return( ret );
3496 max_frag_len = (size_t) ret;
3497
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003498 /* CCS is copied as is, while HS messages may need fragmentation */
3499 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3500 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 if( max_frag_len == 0 )
3502 {
3503 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3504 return( ret );
3505
3506 continue;
3507 }
3508
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003509 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003510 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003512
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003513 /* Update position inside current message */
3514 ssl->handshake->cur_msg_p += cur->len;
3515 }
3516 else
3517 {
3518 const unsigned char * const p = ssl->handshake->cur_msg_p;
3519 const size_t hs_len = cur->len - 12;
3520 const size_t frag_off = p - ( cur->p + 12 );
3521 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003522 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003523
Hanno Beckere1dcb032018-08-17 16:47:58 +01003524 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003525 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003526 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003528
Hanno Becker67bc7c32018-08-06 11:33:50 +01003529 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3530 return( ret );
3531
3532 continue;
3533 }
3534 max_hs_frag_len = max_frag_len - 12;
3535
3536 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3537 max_hs_frag_len : rem_len;
3538
3539 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003540 {
3541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003542 (unsigned) cur_hs_frag_len,
3543 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003544 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003545
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003546 /* Messages are stored with handshake headers as if not fragmented,
3547 * copy beginning of headers then fill fragmentation fields.
3548 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3549 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003551 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3552 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3553 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3554
Hanno Becker67bc7c32018-08-06 11:33:50 +01003555 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3556 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3557 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003558
3559 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3560
Hanno Becker3f7b9732018-08-28 09:53:25 +01003561 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003562 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3563 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003564 ssl->out_msgtype = cur->type;
3565
3566 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003567 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003568 }
3569
3570 /* If done with the current message move to the next one if any */
3571 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3572 {
3573 if( cur->next != NULL )
3574 {
3575 ssl->handshake->cur_msg = cur->next;
3576 ssl->handshake->cur_msg_p = cur->next->p + 12;
3577 }
3578 else
3579 {
3580 ssl->handshake->cur_msg = NULL;
3581 ssl->handshake->cur_msg_p = NULL;
3582 }
3583 }
3584
3585 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003586 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 return( ret );
3590 }
3591 }
3592
Hanno Becker67bc7c32018-08-06 11:33:50 +01003593 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3594 return( ret );
3595
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003596 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3598 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003599 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003601 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003602 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3603 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003604
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606
3607 return( 0 );
3608}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609
3610/*
3611 * To be called when the last message of an incoming flight is received.
3612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614{
3615 /* We won't need to resend that one any more */
3616 ssl_flight_free( ssl->handshake->flight );
3617 ssl->handshake->flight = NULL;
3618 ssl->handshake->cur_msg = NULL;
3619
3620 /* The next incoming flight will start with this msg_seq */
3621 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3622
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003623 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003624 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003625
Hanno Becker0271f962018-08-16 13:23:47 +01003626 /* Clear future message buffering structure. */
3627 ssl_buffering_free( ssl );
3628
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003629 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003630 ssl_set_timer( ssl, 0 );
3631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3633 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636 }
3637 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003639}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
3641/*
3642 * To be called when the last message of an outgoing flight is send.
3643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003646 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003647 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3650 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003653 }
3654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658
Paul Bakker5121ce52009-01-03 21:22:43 +00003659/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003660 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003662
3663/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003664 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 *
3666 * - fill in handshake headers
3667 * - update handshake checksum
3668 * - DTLS: save message for resending
3669 * - then pass to the record layer
3670 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003671 * DTLS: except for HelloRequest, messages are only queued, and will only be
3672 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003673 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003674 * Inputs:
3675 * - ssl->out_msglen: 4 + actual handshake message len
3676 * (4 is the size of handshake headers for TLS)
3677 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3678 * - ssl->out_msg + 4: the handshake message body
3679 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003680 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003681 * - ssl->out_msglen: the length of the record contents
3682 * (including handshake headers but excluding record headers)
3683 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003684 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003686{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003687 int ret;
3688 const size_t hs_len = ssl->out_msglen - 4;
3689 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3692
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003693 /*
3694 * Sanity checks
3695 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003696 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003697 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3698 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003699 /* In SSLv3, the client might send a NoCertificate alert. */
3700#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3701 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3702 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3703 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3704#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3705 {
3706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3708 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003711 /* Whenever we send anything different from a
3712 * HelloRequest we should be in a handshake - double check. */
3713 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3714 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 ssl->handshake == NULL )
3716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3719 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003722 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003725 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3727 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003728 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003729#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003730
Hanno Beckerb50a2532018-08-06 11:52:54 +01003731 /* Double-check that we did not exceed the bounds
3732 * of the outgoing record buffer.
3733 * This should never fail as the various message
3734 * writing functions must obey the bounds of the
3735 * outgoing record buffer, but better be safe.
3736 *
3737 * Note: We deliberately do not check for the MTU or MFL here.
3738 */
3739 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3740 {
3741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3742 "size %u, maximum %u",
3743 (unsigned) ssl->out_msglen,
3744 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3746 }
3747
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 /*
3749 * Fill handshake headers
3750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003751 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3754 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3755 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 /*
3758 * DTLS has additional fields in the Handshake layer,
3759 * between the length field and the actual payload:
3760 * uint16 message_seq;
3761 * uint24 fragment_offset;
3762 * uint24 fragment_length;
3763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003765 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003766 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003767 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003768 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003769 {
3770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3771 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003773 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003774 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3775 }
3776
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003777 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003778 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003779
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003780 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003781 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003782 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003783 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3784 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3785 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003786 }
3787 else
3788 {
3789 ssl->out_msg[4] = 0;
3790 ssl->out_msg[5] = 0;
3791 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003792
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 /* Handshake hashes are computed without fragmentation,
3794 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003795 memset( ssl->out_msg + 6, 0x00, 3 );
3796 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003797 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003799
Hanno Becker0207e532018-08-28 10:28:28 +01003800 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003801 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3802 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 }
3804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003807 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003808 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3809 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810 {
3811 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003817 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003819 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 {
3822 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3823 return( ret );
3824 }
3825 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003826
3827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3828
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003829 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003830}
3831
3832/*
3833 * Record layer functions
3834 */
3835
3836/*
3837 * Write current record.
3838 *
3839 * Uses:
3840 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3841 * - ssl->out_msglen: length of the record content (excl headers)
3842 * - ssl->out_msg: record content
3843 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003844int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003845{
3846 int ret, done = 0;
3847 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003848 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003849
3850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003853 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003855 {
3856 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003859 return( ret );
3860 }
3861
3862 len = ssl->out_msglen;
3863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3867 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 ret = mbedtls_ssl_hw_record_write( ssl );
3872 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3875 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003876 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003877
3878 if( ret == 0 )
3879 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003880 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003882 if( !done )
3883 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003884 unsigned i;
3885 size_t protected_record_size;
3886
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003887 /* Skip writing the record content type to after the encryption,
3888 * as it may change when using the CID extension. */
3889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003891 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003892
Hanno Becker19859472018-08-06 09:40:20 +01003893 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003894 ssl->out_len[0] = (unsigned char)( len >> 8 );
3895 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003896
Paul Bakker48916f92012-09-16 19:57:18 +00003897 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003898 {
Hanno Becker3307b532017-12-27 21:37:21 +00003899 mbedtls_record rec;
3900
3901 rec.buf = ssl->out_iv;
3902 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3903 ( ssl->out_iv - ssl->out_buf );
3904 rec.data_len = ssl->out_msglen;
3905 rec.data_offset = ssl->out_msg - rec.buf;
3906
3907 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3908 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3909 ssl->conf->transport, rec.ver );
3910 rec.type = ssl->out_msgtype;
3911
Hanno Beckera5a2b082019-05-15 14:03:01 +01003912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003913 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003914 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003915#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003916
Hanno Becker611a83b2018-01-03 14:27:32 +00003917 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003918 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003921 return( ret );
3922 }
3923
Hanno Becker3307b532017-12-27 21:37:21 +00003924 if( rec.data_offset != 0 )
3925 {
3926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3928 }
3929
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003930 /* Update the record content type and CID. */
3931 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003932#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003933 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003934#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003935 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003936 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3937 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003938 }
3939
Hanno Becker43395762019-05-03 14:46:38 +01003940 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003941
3942#if defined(MBEDTLS_SSL_PROTO_DTLS)
3943 /* In case of DTLS, double-check that we don't exceed
3944 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003945 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003946 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003947 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003948 if( ret < 0 )
3949 return( ret );
3950
3951 if( protected_record_size > (size_t) ret )
3952 {
3953 /* Should never happen */
3954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3955 }
3956 }
3957#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003958
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003959 /* Now write the potentially updated record content type. */
3960 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003963 "version = [%d:%d], msglen = %d",
3964 ssl->out_hdr[0], ssl->out_hdr[1],
3965 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003968 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003969
3970 ssl->out_left += protected_record_size;
3971 ssl->out_hdr += protected_record_size;
3972 ssl_update_out_pointers( ssl, ssl->transform_out );
3973
Hanno Becker04484622018-08-06 09:49:38 +01003974 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3975 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3976 break;
3977
3978 /* The loop goes to its end iff the counter is wrapping */
3979 if( i == ssl_ep_len( ssl ) )
3980 {
3981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3982 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003984 }
3985
Hanno Becker67bc7c32018-08-06 11:33:50 +01003986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003987 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003988 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003989 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003990 size_t remaining;
3991 ret = ssl_get_remaining_payload_in_datagram( ssl );
3992 if( ret < 0 )
3993 {
3994 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3995 ret );
3996 return( ret );
3997 }
3998
3999 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004000 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004001 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004003 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004004 else
4005 {
Hanno Becker513815a2018-08-20 11:56:09 +01004006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004007 }
4008 }
4009#endif /* MBEDTLS_SSL_PROTO_DTLS */
4010
4011 if( ( flush == SSL_FORCE_FLUSH ) &&
4012 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 return( ret );
4016 }
4017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
4020 return( 0 );
4021}
4022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004024
4025static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4026{
4027 if( ssl->in_msglen < ssl->in_hslen ||
4028 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4029 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4030 {
4031 return( 1 );
4032 }
4033 return( 0 );
4034}
Hanno Becker44650b72018-08-16 12:51:11 +01004035
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004036static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004037{
4038 return( ( ssl->in_msg[9] << 16 ) |
4039 ( ssl->in_msg[10] << 8 ) |
4040 ssl->in_msg[11] );
4041}
4042
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004043static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004044{
4045 return( ( ssl->in_msg[6] << 16 ) |
4046 ( ssl->in_msg[7] << 8 ) |
4047 ssl->in_msg[8] );
4048}
4049
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004050static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004051{
4052 uint32_t msg_len, frag_off, frag_len;
4053
4054 msg_len = ssl_get_hs_total_len( ssl );
4055 frag_off = ssl_get_hs_frag_off( ssl );
4056 frag_len = ssl_get_hs_frag_len( ssl );
4057
4058 if( frag_off > msg_len )
4059 return( -1 );
4060
4061 if( frag_len > msg_len - frag_off )
4062 return( -1 );
4063
4064 if( frag_len + 12 > ssl->in_msglen )
4065 return( -1 );
4066
4067 return( 0 );
4068}
4069
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004070/*
4071 * Mark bits in bitmask (used for DTLS HS reassembly)
4072 */
4073static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4074{
4075 unsigned int start_bits, end_bits;
4076
4077 start_bits = 8 - ( offset % 8 );
4078 if( start_bits != 8 )
4079 {
4080 size_t first_byte_idx = offset / 8;
4081
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004082 /* Special case */
4083 if( len <= start_bits )
4084 {
4085 for( ; len != 0; len-- )
4086 mask[first_byte_idx] |= 1 << ( start_bits - len );
4087
4088 /* Avoid potential issues with offset or len becoming invalid */
4089 return;
4090 }
4091
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004092 offset += start_bits; /* Now offset % 8 == 0 */
4093 len -= start_bits;
4094
4095 for( ; start_bits != 0; start_bits-- )
4096 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4097 }
4098
4099 end_bits = len % 8;
4100 if( end_bits != 0 )
4101 {
4102 size_t last_byte_idx = ( offset + len ) / 8;
4103
4104 len -= end_bits; /* Now len % 8 == 0 */
4105
4106 for( ; end_bits != 0; end_bits-- )
4107 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4108 }
4109
4110 memset( mask + offset / 8, 0xFF, len / 8 );
4111}
4112
4113/*
4114 * Check that bitmask is full
4115 */
4116static int ssl_bitmask_check( unsigned char *mask, size_t len )
4117{
4118 size_t i;
4119
4120 for( i = 0; i < len / 8; i++ )
4121 if( mask[i] != 0xFF )
4122 return( -1 );
4123
4124 for( i = 0; i < len % 8; i++ )
4125 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4126 return( -1 );
4127
4128 return( 0 );
4129}
4130
Hanno Becker56e205e2018-08-16 09:06:12 +01004131/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004132static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004133 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004134{
Hanno Becker56e205e2018-08-16 09:06:12 +01004135 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004136
Hanno Becker56e205e2018-08-16 09:06:12 +01004137 alloc_len = 12; /* Handshake header */
4138 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139
Hanno Beckerd07df862018-08-16 09:14:58 +01004140 if( add_bitmap )
4141 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004142
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004143 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004144}
Hanno Becker56e205e2018-08-16 09:06:12 +01004145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004147
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004148static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004149{
4150 return( ( ssl->in_msg[1] << 16 ) |
4151 ( ssl->in_msg[2] << 8 ) |
4152 ssl->in_msg[3] );
4153}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004154
Simon Butcher99000142016-10-13 17:21:01 +01004155int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004160 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004162 }
4163
Hanno Becker12555c62018-08-16 12:47:53 +01004164 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004167 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004168 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004171 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004172 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004174 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004175
Hanno Becker44650b72018-08-16 12:51:11 +01004176 if( ssl_check_hs_header( ssl ) != 0 )
4177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4179 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4180 }
4181
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004182 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004183 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4184 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4185 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4186 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004187 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004188 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4189 {
4190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4191 recv_msg_seq,
4192 ssl->handshake->in_msg_seq ) );
4193 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4194 }
4195
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004196 /* Retransmit only on last message from previous flight, to avoid
4197 * too many retransmissions.
4198 * Besides, No sane server ever retransmits HelloVerifyRequest */
4199 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004203 "message_seq = %d, start_of_flight = %d",
4204 recv_msg_seq,
4205 ssl->handshake->in_flight_start_seq ) );
4206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004210 return( ret );
4211 }
4212 }
4213 else
4214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004216 "message_seq = %d, expected = %d",
4217 recv_msg_seq,
4218 ssl->handshake->in_msg_seq ) );
4219 }
4220
Hanno Becker90333da2017-10-10 11:27:13 +01004221 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004222 }
4223 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224
Hanno Becker6d97ef52018-08-16 13:09:04 +01004225 /* Message reassembly is handled alongside buffering of future
4226 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004227 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004228 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004229 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004232 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004233 }
4234 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004235 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004237#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004238 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004239 /* With TLS we don't handle fragmentation (for now) */
4240 if( ssl->in_msglen < ssl->in_hslen )
4241 {
4242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4243 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4244 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004245 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004246#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004247
Simon Butcher99000142016-10-13 17:21:01 +01004248 return( 0 );
4249}
4250
4251void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4252{
Hanno Becker0271f962018-08-16 13:23:47 +01004253 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004254
Hanno Becker0271f962018-08-16 13:23:47 +01004255 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004256 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004258 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004259
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004260 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004262 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004263 ssl->handshake != NULL )
4264 {
Hanno Becker0271f962018-08-16 13:23:47 +01004265 unsigned offset;
4266 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004267
Hanno Becker0271f962018-08-16 13:23:47 +01004268 /* Increment handshake sequence number */
4269 hs->in_msg_seq++;
4270
4271 /*
4272 * Clear up handshake buffering and reassembly structure.
4273 */
4274
4275 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004276 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004277
4278 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004279 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4280 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004281 offset++, hs_buf++ )
4282 {
4283 *hs_buf = *(hs_buf + 1);
4284 }
4285
4286 /* Create a fresh last entry */
4287 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004288 }
4289#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004290}
4291
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004292/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 * DTLS anti-replay: RFC 6347 4.1.2.6
4294 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004295 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4296 * Bit n is set iff record number in_window_top - n has been seen.
4297 *
4298 * Usually, in_window_top is the last record number seen and the lsb of
4299 * in_window is set. The only exception is the initial state (record number 0
4300 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004302#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4303static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004304{
4305 ssl->in_window_top = 0;
4306 ssl->in_window = 0;
4307}
4308
4309static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4310{
4311 return( ( (uint64_t) buf[0] << 40 ) |
4312 ( (uint64_t) buf[1] << 32 ) |
4313 ( (uint64_t) buf[2] << 24 ) |
4314 ( (uint64_t) buf[3] << 16 ) |
4315 ( (uint64_t) buf[4] << 8 ) |
4316 ( (uint64_t) buf[5] ) );
4317}
4318
4319/*
4320 * Return 0 if sequence number is acceptable, -1 otherwise
4321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004323{
4324 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4325 uint64_t bit;
4326
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return( 0 );
4329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004330 if( rec_seqnum > ssl->in_window_top )
4331 return( 0 );
4332
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004333 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004334
4335 if( bit >= 64 )
4336 return( -1 );
4337
4338 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4339 return( -1 );
4340
4341 return( 0 );
4342}
4343
4344/*
4345 * Update replay window on new validated record
4346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004347void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004348{
4349 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4350
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004351 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004352 return;
4353
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 if( rec_seqnum > ssl->in_window_top )
4355 {
4356 /* Update window_top and the contents of the window */
4357 uint64_t shift = rec_seqnum - ssl->in_window_top;
4358
4359 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004360 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004362 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004363 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 ssl->in_window |= 1;
4365 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004366
4367 ssl->in_window_top = rec_seqnum;
4368 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004369 else
4370 {
4371 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004372 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004373
4374 if( bit < 64 ) /* Always true, but be extra sure */
4375 ssl->in_window |= (uint64_t) 1 << bit;
4376 }
4377}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004378#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004379
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004380#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004381/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004382static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4383
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004384/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004385 * Without any SSL context, check if a datagram looks like a ClientHello with
4386 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004387 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004388 *
4389 * - if cookie is valid, return 0
4390 * - if ClientHello looks superficially valid but cookie is not,
4391 * fill obuf and set olen, then
4392 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4393 * - otherwise return a specific error code
4394 */
4395static int ssl_check_dtls_clihlo_cookie(
4396 mbedtls_ssl_cookie_write_t *f_cookie_write,
4397 mbedtls_ssl_cookie_check_t *f_cookie_check,
4398 void *p_cookie,
4399 const unsigned char *cli_id, size_t cli_id_len,
4400 const unsigned char *in, size_t in_len,
4401 unsigned char *obuf, size_t buf_len, size_t *olen )
4402{
4403 size_t sid_len, cookie_len;
4404 unsigned char *p;
4405
4406 if( f_cookie_write == NULL || f_cookie_check == NULL )
4407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4408
4409 /*
4410 * Structure of ClientHello with record and handshake headers,
4411 * and expected values. We don't need to check a lot, more checks will be
4412 * done when actually parsing the ClientHello - skipping those checks
4413 * avoids code duplication and does not make cookie forging any easier.
4414 *
4415 * 0-0 ContentType type; copied, must be handshake
4416 * 1-2 ProtocolVersion version; copied
4417 * 3-4 uint16 epoch; copied, must be 0
4418 * 5-10 uint48 sequence_number; copied
4419 * 11-12 uint16 length; (ignored)
4420 *
4421 * 13-13 HandshakeType msg_type; (ignored)
4422 * 14-16 uint24 length; (ignored)
4423 * 17-18 uint16 message_seq; copied
4424 * 19-21 uint24 fragment_offset; copied, must be 0
4425 * 22-24 uint24 fragment_length; (ignored)
4426 *
4427 * 25-26 ProtocolVersion client_version; (ignored)
4428 * 27-58 Random random; (ignored)
4429 * 59-xx SessionID session_id; 1 byte len + sid_len content
4430 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4431 * ...
4432 *
4433 * Minimum length is 61 bytes.
4434 */
4435 if( in_len < 61 ||
4436 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4437 in[3] != 0 || in[4] != 0 ||
4438 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4439 {
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441 }
4442
4443 sid_len = in[59];
4444 if( sid_len > in_len - 61 )
4445 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4446
4447 cookie_len = in[60 + sid_len];
4448 if( cookie_len > in_len - 60 )
4449 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4450
4451 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4452 cli_id, cli_id_len ) == 0 )
4453 {
4454 /* Valid cookie */
4455 return( 0 );
4456 }
4457
4458 /*
4459 * If we get here, we've got an invalid cookie, let's prepare HVR.
4460 *
4461 * 0-0 ContentType type; copied
4462 * 1-2 ProtocolVersion version; copied
4463 * 3-4 uint16 epoch; copied
4464 * 5-10 uint48 sequence_number; copied
4465 * 11-12 uint16 length; olen - 13
4466 *
4467 * 13-13 HandshakeType msg_type; hello_verify_request
4468 * 14-16 uint24 length; olen - 25
4469 * 17-18 uint16 message_seq; copied
4470 * 19-21 uint24 fragment_offset; copied
4471 * 22-24 uint24 fragment_length; olen - 25
4472 *
4473 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4474 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4475 *
4476 * Minimum length is 28.
4477 */
4478 if( buf_len < 28 )
4479 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4480
4481 /* Copy most fields and adapt others */
4482 memcpy( obuf, in, 25 );
4483 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4484 obuf[25] = 0xfe;
4485 obuf[26] = 0xff;
4486
4487 /* Generate and write actual cookie */
4488 p = obuf + 28;
4489 if( f_cookie_write( p_cookie,
4490 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4491 {
4492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4493 }
4494
4495 *olen = p - obuf;
4496
4497 /* Go back and fill length fields */
4498 obuf[27] = (unsigned char)( *olen - 28 );
4499
4500 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4501 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4502 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4503
4504 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4505 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4506
4507 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4508}
4509
4510/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * Handle possible client reconnect with the same UDP quadruplet
4512 * (RFC 6347 Section 4.2.8).
4513 *
4514 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4515 * that looks like a ClientHello.
4516 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517 * - if the input looks like a ClientHello without cookies,
4518 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004519 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 * - if the input looks like a ClientHello with a valid cookie,
4521 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004522 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004526 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4527 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 */
4529static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4530{
4531 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004534 ret = ssl_check_dtls_clihlo_cookie(
4535 ssl->conf->f_cookie_write,
4536 ssl->conf->f_cookie_check,
4537 ssl->conf->p_cookie,
4538 ssl->cli_id, ssl->cli_id_len,
4539 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004540 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004541
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004542 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4543
4544 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004546 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 * If the error is permanent we'll catch it later,
4548 * if it's not, then hopefully it'll work next time. */
4549 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4550
4551 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 }
4553
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004556 /* Got a valid cookie, partially reset context */
4557 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4558 {
4559 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4560 return( ret );
4561 }
4562
4563 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004564 }
4565
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004566 return( ret );
4567}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004568#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004569
Hanno Becker46483f12019-05-03 13:25:54 +01004570static int ssl_check_record_type( uint8_t record_type )
4571{
4572 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4573 record_type != MBEDTLS_SSL_MSG_ALERT &&
4574 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4575 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4576 {
4577 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4578 }
4579
4580 return( 0 );
4581}
4582
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004583/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004584 * ContentType type;
4585 * ProtocolVersion version;
4586 * uint16 epoch; // DTLS only
4587 * uint48 sequence_number; // DTLS only
4588 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004589 *
4590 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004591 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004592 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4593 *
4594 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004595 * 1. proceed with the record if this function returns 0
4596 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4597 * 3. return CLIENT_RECONNECT if this function return that value
4598 * 4. drop the whole datagram if this function returns anything else.
4599 * Point 2 is needed when the peer is resending, and we have already received
4600 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004604 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004605 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004606
Hanno Becker8b09b732019-05-08 12:03:28 +01004607 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004608
Paul Bakker5121ce52009-01-03 21:22:43 +00004609 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004610 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004611
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004612 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004614 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004615 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4616 ssl->conf->cid_len != 0 )
4617 {
4618 /* Shift pointers to account for record header including CID
4619 * struct {
4620 * ContentType special_type = tls12_cid;
4621 * ProtocolVersion version;
4622 * uint16 epoch;
4623 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004624 * opaque cid[cid_length]; // Additional field compared to
4625 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004626 * uint16 length;
4627 * opaque enc_content[DTLSCiphertext.length];
4628 * } DTLSCiphertext;
4629 */
4630
4631 /* So far, we only support static CID lengths
4632 * fixed in the configuration. */
4633 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4634 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4635 }
4636 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004637#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004638 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004641
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004642#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004643 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004644 * Section 4.1.2.7, that is, send alert only with TLS */
4645 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4646 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4648 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004649 }
4650#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004653 }
4654
4655 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004656 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 }
4661
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004662 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4665 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 }
4667
Hanno Becker8b09b732019-05-08 12:03:28 +01004668 /* Now that the total length of the record header is known, ensure
4669 * that the current datagram is large enough to hold it.
4670 * This would fail, for example, if we received a datagram of
4671 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4672 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4673 if( ret != 0 )
4674 {
4675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4676 return( ret );
4677 }
4678 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4679
4680 /* Parse and validate record length
4681 * This must happen after the CID parsing because
4682 * its position in the record header depends on
4683 * the presence of a CID. */
4684
4685 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004686 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004687 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4690 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004691 }
4692
Hanno Becker8b09b732019-05-08 12:03:28 +01004693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004694 "version = [%d:%d], msglen = %d",
4695 ssl->in_msgtype,
4696 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004697
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004698 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004699 * DTLS-related tests.
4700 * Check epoch before checking length constraint because
4701 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4702 * message gets duplicated before the corresponding Finished message,
4703 * the second ChangeCipherSpec should be discarded because it belongs
4704 * to an old epoch, but not because its length is shorter than
4705 * the minimum record length for packets using the new record transform.
4706 * Note that these two kinds of failures are handled differently,
4707 * as an unexpected record is silently skipped but an invalid
4708 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004709 */
4710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004711 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004712 {
4713 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4714
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004715 /* Check epoch (and sequence number) with DTLS */
4716 if( rec_epoch != ssl->in_epoch )
4717 {
4718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4719 "expected %d, received %d",
4720 ssl->in_epoch, rec_epoch ) );
4721
4722#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4723 /*
4724 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4725 * access the first byte of record content (handshake type), as we
4726 * have an active transform (possibly iv_len != 0), so use the
4727 * fact that the record header len is 13 instead.
4728 */
4729 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4730 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4731 rec_epoch == 0 &&
4732 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4733 ssl->in_left > 13 &&
4734 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4735 {
4736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4737 "from the same port" ) );
4738 return( ssl_handle_possible_reconnect( ssl ) );
4739 }
4740 else
4741#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 {
4743 /* Consider buffering the record. */
4744 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4745 {
4746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4747 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4748 }
4749
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004750 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004751 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004752 }
4753
4754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4755 /* Replay detection only works for the current epoch */
4756 if( rec_epoch == ssl->in_epoch &&
4757 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4758 {
4759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4760 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4761 }
4762#endif
4763 }
4764#endif /* MBEDTLS_SSL_PROTO_DTLS */
4765
Hanno Becker52c6dc62017-05-26 16:07:36 +01004766
4767 /* Check length against bounds of the current transform and version */
4768 if( ssl->transform_in == NULL )
4769 {
4770 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004771 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4775 }
4776 }
4777 else
4778 {
4779 if( ssl->in_msglen < ssl->transform_in->minlen )
4780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4783 }
4784
4785#if defined(MBEDTLS_SSL_PROTO_SSL3)
4786 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004787 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004788 {
4789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4790 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4791 }
4792#endif
4793#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4794 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4795 /*
4796 * TLS encrypted messages can have up to 256 bytes of padding
4797 */
4798 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4799 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004800 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004801 {
4802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4803 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4804 }
4805#endif
4806 }
4807
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808 return( 0 );
4809}
Paul Bakker5121ce52009-01-03 21:22:43 +00004810
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004811/*
4812 * If applicable, decrypt (and decompress) record content
4813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004815{
4816 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004819 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4822 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 ret = mbedtls_ssl_hw_record_read( ssl );
4827 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4830 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004831 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004832
4833 if( ret == 0 )
4834 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004835 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004837 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004838 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004839 mbedtls_record rec;
4840
4841 rec.buf = ssl->in_iv;
4842 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4843 - ( ssl->in_iv - ssl->in_buf );
4844 rec.data_len = ssl->in_msglen;
4845 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004846#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004847 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004848 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004849#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004850
4851 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4852 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4853 ssl->conf->transport, rec.ver );
4854 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004855 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4856 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859
Hanno Beckera5a2b082019-05-15 14:03:01 +01004860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004861 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4862 ssl->conf->ignore_unexpected_cid
4863 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4864 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004866 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004869
Paul Bakker5121ce52009-01-03 21:22:43 +00004870 return( ret );
4871 }
4872
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004873 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004874 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004875 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4876 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004878
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004879 /* The record content type may change during decryption,
4880 * so re-read it. */
4881 ssl->in_msgtype = rec.type;
4882 /* Also update the input buffer, because unfortunately
4883 * the server-side ssl_parse_client_hello() reparses the
4884 * record header when receiving a ClientHello initiating
4885 * a renegotiation. */
4886 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004887 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004888 ssl->in_msglen = rec.data_len;
4889 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4890 ssl->in_len[1] = (unsigned char)( rec.data_len );
4891
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4893 ssl->in_msg, ssl->in_msglen );
4894
Hanno Beckera5a2b082019-05-15 14:03:01 +01004895#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004896 /* We have already checked the record content type
4897 * in ssl_parse_record_header(), failing or silently
4898 * dropping the record in the case of an unknown type.
4899 *
4900 * Since with the use of CIDs, the record content type
4901 * might change during decryption, re-check the record
4902 * content type, but treat a failure as fatal this time. */
4903 if( ssl_check_record_type( ssl->in_msgtype ) )
4904 {
4905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4906 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4907 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004908#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004909
Angus Grattond8213d02016-05-25 20:56:48 +10004910 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004914 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004915 else if( ssl->in_msglen == 0 )
4916 {
4917#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4918 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4919 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4920 {
4921 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4922 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4923 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4924 }
4925#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4926
4927 ssl->nb_zero++;
4928
4929 /*
4930 * Three or more empty messages may be a DoS attack
4931 * (excessive CPU consumption).
4932 */
4933 if( ssl->nb_zero > 3 )
4934 {
4935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004936 "messages, possible DoS attack" ) );
4937 /* Treat the records as if they were not properly authenticated,
4938 * thereby failing the connection if we see more than allowed
4939 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4941 }
4942 }
4943 else
4944 ssl->nb_zero = 0;
4945
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004946 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4947#if defined(MBEDTLS_SSL_PROTO_TLS)
4948 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 {
4950 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004951 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004952 if( ++ssl->in_ctr[i - 1] != 0 )
4953 break;
4954
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004955 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004956 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004957 {
4958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4959 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4960 }
4961 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004962#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004963
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 }
4965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004967 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004969 {
4970 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004973 return( ret );
4974 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004975 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004979 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004981 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004982 }
4983#endif
4984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985 return( 0 );
4986}
4987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004989
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004990/*
4991 * Read a record.
4992 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004993 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4994 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4995 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004996 */
Hanno Becker1097b342018-08-15 14:09:41 +01004997
4998/* Helper functions for mbedtls_ssl_read_record(). */
4999static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005000static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5001static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005002
Hanno Becker327c93b2018-08-15 13:56:18 +01005003int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005004 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005005{
5006 int ret;
5007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005009
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 if( ssl->keep_current_message == 0 )
5011 {
5012 do {
Simon Butcher99000142016-10-13 17:21:01 +01005013
Hanno Becker26994592018-08-15 14:14:59 +01005014 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005015 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005016 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005017
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005019 {
Hanno Becker40f50842018-08-15 14:48:01 +01005020#if defined(MBEDTLS_SSL_PROTO_DTLS)
5021 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005022
Hanno Becker40f50842018-08-15 14:48:01 +01005023 /* We only check for buffered messages if the
5024 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005025 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005026 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005027 {
Hanno Becker40f50842018-08-15 14:48:01 +01005028 if( ssl_load_buffered_message( ssl ) == 0 )
5029 have_buffered = 1;
5030 }
5031
5032 if( have_buffered == 0 )
5033#endif /* MBEDTLS_SSL_PROTO_DTLS */
5034 {
5035 ret = ssl_get_next_record( ssl );
5036 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5037 continue;
5038
5039 if( ret != 0 )
5040 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005041 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005042 return( ret );
5043 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005044 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005045 }
5046
5047 ret = mbedtls_ssl_handle_message_type( ssl );
5048
Hanno Becker40f50842018-08-15 14:48:01 +01005049#if defined(MBEDTLS_SSL_PROTO_DTLS)
5050 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5051 {
5052 /* Buffer future message */
5053 ret = ssl_buffer_message( ssl );
5054 if( ret != 0 )
5055 return( ret );
5056
5057 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5058 }
5059#endif /* MBEDTLS_SSL_PROTO_DTLS */
5060
Hanno Becker90333da2017-10-10 11:27:13 +01005061 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5062 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063
5064 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005065 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005066 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005067 return( ret );
5068 }
5069
Hanno Becker327c93b2018-08-15 13:56:18 +01005070 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005071 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005072 {
5073 mbedtls_ssl_update_handshake_status( ssl );
5074 }
Simon Butcher99000142016-10-13 17:21:01 +01005075 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005076 else
Simon Butcher99000142016-10-13 17:21:01 +01005077 {
Hanno Becker02f59072018-08-15 14:00:24 +01005078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005079 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005080 }
5081
5082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5083
5084 return( 0 );
5085}
5086
Hanno Becker40f50842018-08-15 14:48:01 +01005087#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005088static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005089{
Hanno Becker40f50842018-08-15 14:48:01 +01005090 if( ssl->in_left > ssl->next_record_offset )
5091 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005092
Hanno Becker40f50842018-08-15 14:48:01 +01005093 return( 0 );
5094}
5095
5096static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5097{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005099 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005100 int ret = 0;
5101
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005102 if( hs == NULL )
5103 return( -1 );
5104
Hanno Beckere00ae372018-08-20 09:39:42 +01005105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5106
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005107 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5108 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5109 {
5110 /* Check if we have seen a ChangeCipherSpec before.
5111 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005112 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005113 {
5114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5115 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005116 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005117 }
5118
Hanno Becker39b8bc92018-08-28 17:17:13 +01005119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5121 ssl->in_msglen = 1;
5122 ssl->in_msg[0] = 1;
5123
5124 /* As long as they are equal, the exact value doesn't matter. */
5125 ssl->in_left = 0;
5126 ssl->next_record_offset = 0;
5127
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005128 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005129 goto exit;
5130 }
Hanno Becker37f95322018-08-16 13:55:32 +01005131
Hanno Beckerb8f50142018-08-28 10:01:34 +01005132#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005133 /* Debug only */
5134 {
5135 unsigned offset;
5136 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5137 {
5138 hs_buf = &hs->buffering.hs[offset];
5139 if( hs_buf->is_valid == 1 )
5140 {
5141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5142 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005143 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005144 }
5145 }
5146 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005147#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005148
5149 /* Check if we have buffered and/or fully reassembled the
5150 * next handshake message. */
5151 hs_buf = &hs->buffering.hs[0];
5152 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5153 {
5154 /* Synthesize a record containing the buffered HS message. */
5155 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5156 ( hs_buf->data[2] << 8 ) |
5157 hs_buf->data[3];
5158
5159 /* Double-check that we haven't accidentally buffered
5160 * a message that doesn't fit into the input buffer. */
5161 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5165 }
5166
5167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5168 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5169 hs_buf->data, msg_len + 12 );
5170
5171 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5172 ssl->in_hslen = msg_len + 12;
5173 ssl->in_msglen = msg_len + 12;
5174 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5175
5176 ret = 0;
5177 goto exit;
5178 }
5179 else
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5182 hs->in_msg_seq ) );
5183 }
5184
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005185 ret = -1;
5186
5187exit:
5188
5189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5190 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005191}
5192
Hanno Beckera02b0b42018-08-21 17:20:27 +01005193static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5194 size_t desired )
5195{
5196 int offset;
5197 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5199 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005200
Hanno Becker01315ea2018-08-21 17:22:17 +01005201 /* Get rid of future records epoch first, if such exist. */
5202 ssl_free_buffered_record( ssl );
5203
5204 /* Check if we have enough space available now. */
5205 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5206 hs->buffering.total_bytes_buffered ) )
5207 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005209 return( 0 );
5210 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005211
Hanno Becker4f432ad2018-08-28 10:02:32 +01005212 /* We don't have enough space to buffer the next expected handshake
5213 * message. Remove buffers used for future messages to gain space,
5214 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005215 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5216 offset >= 0; offset-- )
5217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5219 offset ) );
5220
Hanno Beckerb309b922018-08-23 13:18:05 +01005221 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005222
5223 /* Check if we have enough space available now. */
5224 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5225 hs->buffering.total_bytes_buffered ) )
5226 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005228 return( 0 );
5229 }
5230 }
5231
5232 return( -1 );
5233}
5234
Hanno Becker40f50842018-08-15 14:48:01 +01005235static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5236{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005237 int ret = 0;
5238 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5239
5240 if( hs == NULL )
5241 return( 0 );
5242
5243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5244
5245 switch( ssl->in_msgtype )
5246 {
5247 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005249
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005250 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005251 break;
5252
5253 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005254 {
5255 unsigned recv_msg_seq_offset;
5256 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5257 mbedtls_ssl_hs_buffer *hs_buf;
5258 size_t msg_len = ssl->in_hslen - 12;
5259
5260 /* We should never receive an old handshake
5261 * message - double-check nonetheless. */
5262 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5263 {
5264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5266 }
5267
5268 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5269 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5270 {
5271 /* Silently ignore -- message too far in the future */
5272 MBEDTLS_SSL_DEBUG_MSG( 2,
5273 ( "Ignore future HS message with sequence number %u, "
5274 "buffering window %u - %u",
5275 recv_msg_seq, ssl->handshake->in_msg_seq,
5276 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5277
5278 goto exit;
5279 }
5280
5281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5282 recv_msg_seq, recv_msg_seq_offset ) );
5283
5284 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5285
5286 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005287 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005288 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 size_t reassembly_buf_sz;
5290
Hanno Becker37f95322018-08-16 13:55:32 +01005291 hs_buf->is_fragmented =
5292 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5293
5294 /* We copy the message back into the input buffer
5295 * after reassembly, so check that it's not too large.
5296 * This is an implementation-specific limitation
5297 * and not one from the standard, hence it is not
5298 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005299 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005300 {
5301 /* Ignore message */
5302 goto exit;
5303 }
5304
Hanno Beckere0b150f2018-08-21 15:51:03 +01005305 /* Check if we have enough space to buffer the message. */
5306 if( hs->buffering.total_bytes_buffered >
5307 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5308 {
5309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5311 }
5312
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005313 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5314 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005315
5316 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5317 hs->buffering.total_bytes_buffered ) )
5318 {
5319 if( recv_msg_seq_offset > 0 )
5320 {
5321 /* If we can't buffer a future message because
5322 * of space limitations -- ignore. */
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5324 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5325 (unsigned) hs->buffering.total_bytes_buffered ) );
5326 goto exit;
5327 }
Hanno Beckere1801392018-08-21 16:51:05 +01005328 else
5329 {
5330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5331 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5332 (unsigned) hs->buffering.total_bytes_buffered ) );
5333 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005334
Hanno Beckera02b0b42018-08-21 17:20:27 +01005335 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005336 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5338 (unsigned) msg_len,
5339 (unsigned) reassembly_buf_sz,
5340 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005341 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005342 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5343 goto exit;
5344 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 }
5346
5347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5348 msg_len ) );
5349
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005350 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5351 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005352 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005353 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005354 goto exit;
5355 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005357
5358 /* Prepare final header: copy msg_type, length and message_seq,
5359 * then add standardised fragment_offset and fragment_length */
5360 memcpy( hs_buf->data, ssl->in_msg, 6 );
5361 memset( hs_buf->data + 6, 0, 3 );
5362 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5363
5364 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005365
5366 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005367 }
5368 else
5369 {
5370 /* Make sure msg_type and length are consistent */
5371 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5374 /* Ignore */
5375 goto exit;
5376 }
5377 }
5378
Hanno Becker4422bbb2018-08-20 09:40:19 +01005379 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005380 {
5381 size_t frag_len, frag_off;
5382 unsigned char * const msg = hs_buf->data + 12;
5383
5384 /*
5385 * Check and copy current fragment
5386 */
5387
5388 /* Validation of header fields already done in
5389 * mbedtls_ssl_prepare_handshake_record(). */
5390 frag_off = ssl_get_hs_frag_off( ssl );
5391 frag_len = ssl_get_hs_frag_len( ssl );
5392
5393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5394 frag_off, frag_len ) );
5395 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5396
5397 if( hs_buf->is_fragmented )
5398 {
5399 unsigned char * const bitmask = msg + msg_len;
5400 ssl_bitmask_set( bitmask, frag_off, frag_len );
5401 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5402 msg_len ) == 0 );
5403 }
5404 else
5405 {
5406 hs_buf->is_complete = 1;
5407 }
5408
5409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5410 hs_buf->is_complete ? "" : "not yet " ) );
5411 }
5412
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005413 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005414 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005415
5416 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005417 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005418 break;
5419 }
5420
5421exit:
5422
5423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5424 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005425}
5426#endif /* MBEDTLS_SSL_PROTO_DTLS */
5427
Hanno Becker1097b342018-08-15 14:09:41 +01005428static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005429{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005431 * Consume last content-layer message and potentially
5432 * update in_msglen which keeps track of the contents'
5433 * consumption state.
5434 *
5435 * (1) Handshake messages:
5436 * Remove last handshake message, move content
5437 * and adapt in_msglen.
5438 *
5439 * (2) Alert messages:
5440 * Consume whole record content, in_msglen = 0.
5441 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005442 * (3) Change cipher spec:
5443 * Consume whole record content, in_msglen = 0.
5444 *
5445 * (4) Application data:
5446 * Don't do anything - the record layer provides
5447 * the application data as a stream transport
5448 * and consumes through mbedtls_ssl_read only.
5449 *
5450 */
5451
5452 /* Case (1): Handshake messages */
5453 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005454 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005455 /* Hard assertion to be sure that no application data
5456 * is in flight, as corrupting ssl->in_msglen during
5457 * ssl->in_offt != NULL is fatal. */
5458 if( ssl->in_offt != NULL )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5462 }
5463
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005464 /*
5465 * Get next Handshake message in the current record
5466 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005467
Hanno Becker4a810fb2017-05-24 16:27:30 +01005468 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005469 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005470 * current handshake content: If DTLS handshake
5471 * fragmentation is used, that's the fragment
5472 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005473 * size here is faulty and should be changed at
5474 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 * (2) While it doesn't seem to cause problems, one
5476 * has to be very careful not to assume that in_hslen
5477 * is always <= in_msglen in a sensible communication.
5478 * Again, it's wrong for DTLS handshake fragmentation.
5479 * The following check is therefore mandatory, and
5480 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005481 * Additionally, ssl->in_hslen might be arbitrarily out of
5482 * bounds after handling a DTLS message with an unexpected
5483 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005484 */
5485 if( ssl->in_hslen < ssl->in_msglen )
5486 {
5487 ssl->in_msglen -= ssl->in_hslen;
5488 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5489 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005490
Hanno Becker4a810fb2017-05-24 16:27:30 +01005491 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5492 ssl->in_msg, ssl->in_msglen );
5493 }
5494 else
5495 {
5496 ssl->in_msglen = 0;
5497 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005498
Hanno Becker4a810fb2017-05-24 16:27:30 +01005499 ssl->in_hslen = 0;
5500 }
5501 /* Case (4): Application data */
5502 else if( ssl->in_offt != NULL )
5503 {
5504 return( 0 );
5505 }
5506 /* Everything else (CCS & Alerts) */
5507 else
5508 {
5509 ssl->in_msglen = 0;
5510 }
5511
Hanno Becker1097b342018-08-15 14:09:41 +01005512 return( 0 );
5513}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005514
Hanno Beckere74d5562018-08-15 14:26:08 +01005515static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5516{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005517 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005518 return( 1 );
5519
5520 return( 0 );
5521}
5522
Hanno Becker5f066e72018-08-16 14:56:31 +01005523#if defined(MBEDTLS_SSL_PROTO_DTLS)
5524
5525static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5526{
5527 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5528 if( hs == NULL )
5529 return;
5530
Hanno Becker01315ea2018-08-21 17:22:17 +01005531 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005532 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005533 hs->buffering.total_bytes_buffered -=
5534 hs->buffering.future_record.len;
5535
5536 mbedtls_free( hs->buffering.future_record.data );
5537 hs->buffering.future_record.data = NULL;
5538 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005539}
5540
5541static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5542{
5543 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5544 unsigned char * rec;
5545 size_t rec_len;
5546 unsigned rec_epoch;
5547
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005548 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005549 return( 0 );
5550
5551 if( hs == NULL )
5552 return( 0 );
5553
Hanno Becker5f066e72018-08-16 14:56:31 +01005554 rec = hs->buffering.future_record.data;
5555 rec_len = hs->buffering.future_record.len;
5556 rec_epoch = hs->buffering.future_record.epoch;
5557
5558 if( rec == NULL )
5559 return( 0 );
5560
Hanno Becker4cb782d2018-08-20 11:19:05 +01005561 /* Only consider loading future records if the
5562 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005563 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005564 return( 0 );
5565
Hanno Becker5f066e72018-08-16 14:56:31 +01005566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5567
5568 if( rec_epoch != ssl->in_epoch )
5569 {
5570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5571 goto exit;
5572 }
5573
5574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5575
5576 /* Double-check that the record is not too large */
5577 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5578 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5579 {
5580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5582 }
5583
5584 memcpy( ssl->in_hdr, rec, rec_len );
5585 ssl->in_left = rec_len;
5586 ssl->next_record_offset = 0;
5587
5588 ssl_free_buffered_record( ssl );
5589
5590exit:
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5592 return( 0 );
5593}
5594
5595static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5596{
5597 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5598 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005599 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005600
5601 /* Don't buffer future records outside handshakes. */
5602 if( hs == NULL )
5603 return( 0 );
5604
5605 /* Only buffer handshake records (we are only interested
5606 * in Finished messages). */
5607 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5608 return( 0 );
5609
5610 /* Don't buffer more than one future epoch record. */
5611 if( hs->buffering.future_record.data != NULL )
5612 return( 0 );
5613
Hanno Becker01315ea2018-08-21 17:22:17 +01005614 /* Don't buffer record if there's not enough buffering space remaining. */
5615 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5616 hs->buffering.total_bytes_buffered ) )
5617 {
5618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5619 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5620 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005621 return( 0 );
5622 }
5623
Hanno Becker5f066e72018-08-16 14:56:31 +01005624 /* Buffer record */
5625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5626 ssl->in_epoch + 1 ) );
5627 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5628 rec_hdr_len + ssl->in_msglen );
5629
5630 /* ssl_parse_record_header() only considers records
5631 * of the next epoch as candidates for buffering. */
5632 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005633 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005634
5635 hs->buffering.future_record.data =
5636 mbedtls_calloc( 1, hs->buffering.future_record.len );
5637 if( hs->buffering.future_record.data == NULL )
5638 {
5639 /* If we run out of RAM trying to buffer a
5640 * record from the next epoch, just ignore. */
5641 return( 0 );
5642 }
5643
Hanno Becker01315ea2018-08-21 17:22:17 +01005644 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005645
Hanno Becker01315ea2018-08-21 17:22:17 +01005646 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005647 return( 0 );
5648}
5649
5650#endif /* MBEDTLS_SSL_PROTO_DTLS */
5651
Hanno Beckere74d5562018-08-15 14:26:08 +01005652static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005653{
5654 int ret;
5655
Hanno Becker5f066e72018-08-16 14:56:31 +01005656#if defined(MBEDTLS_SSL_PROTO_DTLS)
5657 /* We might have buffered a future record; if so,
5658 * and if the epoch matches now, load it.
5659 * On success, this call will set ssl->in_left to
5660 * the length of the buffered record, so that
5661 * the calls to ssl_fetch_input() below will
5662 * essentially be no-ops. */
5663 ret = ssl_load_buffered_record( ssl );
5664 if( ret != 0 )
5665 return( ret );
5666#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005667
Hanno Becker8b09b732019-05-08 12:03:28 +01005668 /* Reset in pointers to default state for TLS/DTLS records,
5669 * assuming no CID and no offset between record content and
5670 * record plaintext. */
5671 ssl_update_in_pointers( ssl );
5672
5673 /* Ensure that we have enough space available for the default form
5674 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5675 * with no space for CIDs counted in). */
5676 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5677 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005680 return( ret );
5681 }
5682
5683 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005686 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005687 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005688 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005689 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5690 {
5691 ret = ssl_buffer_future_record( ssl );
5692 if( ret != 0 )
5693 return( ret );
5694
5695 /* Fall through to handling of unexpected records */
5696 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5697 }
5698
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005699 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5700 {
5701 /* Skip unexpected record (but not whole datagram) */
5702 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005703 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005704
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5706 "(header)" ) );
5707 }
5708 else
5709 {
5710 /* Skip invalid record and the rest of the datagram */
5711 ssl->next_record_offset = 0;
5712 ssl->in_left = 0;
5713
5714 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5715 "(header)" ) );
5716 }
5717
5718 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005719 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005720 }
5721#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005723 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724
5725 /*
5726 * Read and optionally decrypt the message contents
5727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005729 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005732 return( ret );
5733 }
5734
5735 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005738 {
Hanno Becker43395762019-05-03 14:46:38 +01005739 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005740 if( ssl->next_record_offset < ssl->in_left )
5741 {
5742 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5743 }
5744 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005745 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747#if defined(MBEDTLS_SSL_PROTO_TLS)
5748 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005749 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005750 }
5751#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005752
5753 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005757 {
5758 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005760 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005761 /* Except when waiting for Finished as a bad mac here
5762 * probably means something went wrong in the handshake
5763 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5764 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5765 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5766 {
5767#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5768 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5769 {
5770 mbedtls_ssl_send_alert_message( ssl,
5771 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5772 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5773 }
5774#endif
5775 return( ret );
5776 }
5777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005778#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005779 if( ssl->conf->badmac_limit != 0 &&
5780 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5783 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005784 }
5785#endif
5786
Hanno Becker4a810fb2017-05-24 16:27:30 +01005787 /* As above, invalid records cause
5788 * dismissal of the whole datagram. */
5789
5790 ssl->next_record_offset = 0;
5791 ssl->in_left = 0;
5792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005794 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005795 }
5796
5797 return( ret );
5798 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005799 MBEDTLS_SSL_TRANSPORT_ELSE
5800#endif /* MBEDTLS_SSL_PROTO_DTLS */
5801#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 {
5803 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5805 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 mbedtls_ssl_send_alert_message( ssl,
5808 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5809 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005810 }
5811#endif
5812 return( ret );
5813 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005814#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005815 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816
Simon Butcher99000142016-10-13 17:21:01 +01005817 return( 0 );
5818}
5819
5820int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5821{
5822 int ret;
5823
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005824 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005825 * Handle particular types of records
5826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005828 {
Simon Butcher99000142016-10-13 17:21:01 +01005829 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5830 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005831 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005836 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005837 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005838 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5840 ssl->in_msglen ) );
5841 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842 }
5843
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 if( ssl->in_msg[0] != 1 )
5845 {
5846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5847 ssl->in_msg[0] ) );
5848 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5849 }
5850
5851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005852 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005853 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5854 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5855 {
5856 if( ssl->handshake == NULL )
5857 {
5858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5859 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5860 }
5861
5862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5863 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5864 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005866 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005869 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005870 if( ssl->in_msglen != 2 )
5871 {
5872 /* Note: Standard allows for more than one 2 byte alert
5873 to be packed in a single message, but Mbed TLS doesn't
5874 currently support this. */
5875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5876 ssl->in_msglen ) );
5877 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5878 }
5879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 ssl->in_msg[0], ssl->in_msg[1] ) );
5882
5883 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005884 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005889 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 }
5892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005893 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5894 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5897 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005898 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005899
5900#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5901 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5902 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5903 {
Hanno Becker90333da2017-10-10 11:27:13 +01005904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005905 /* Will be handled when trying to parse ServerHello */
5906 return( 0 );
5907 }
5908#endif
5909
5910#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5911 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5912 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5913 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5914 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5915 {
5916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5917 /* Will be handled in mbedtls_ssl_parse_certificate() */
5918 return( 0 );
5919 }
5920#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5921
5922 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005923 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 }
5925
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005927 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005928 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005929 /* Drop unexpected ApplicationData records,
5930 * except at the beginning of renegotiations */
5931 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5932 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5933#if defined(MBEDTLS_SSL_RENEGOTIATION)
5934 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5935 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005936#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005937 )
5938 {
5939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5940 return( MBEDTLS_ERR_SSL_NON_FATAL );
5941 }
5942
5943 if( ssl->handshake != NULL &&
5944 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5945 {
5946 ssl_handshake_wrapup_free_hs_transform( ssl );
5947 }
5948 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005950
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 return( 0 );
5952}
5953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005954int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005955{
5956 int ret;
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5959 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5960 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005961 {
5962 return( ret );
5963 }
5964
5965 return( 0 );
5966}
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005969 unsigned char level,
5970 unsigned char message )
5971{
5972 int ret;
5973
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005974 if( ssl == NULL || ssl->conf == NULL )
5975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005981 ssl->out_msglen = 2;
5982 ssl->out_msg[0] = level;
5983 ssl->out_msg[1] = message;
5984
Hanno Becker67bc7c32018-08-06 11:33:50 +01005985 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005988 return( ret );
5989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005991
5992 return( 0 );
5993}
5994
Hanno Becker17572472019-02-08 07:19:04 +00005995#if defined(MBEDTLS_X509_CRT_PARSE_C)
5996static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5997{
5998#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5999 if( session->peer_cert != NULL )
6000 {
6001 mbedtls_x509_crt_free( session->peer_cert );
6002 mbedtls_free( session->peer_cert );
6003 session->peer_cert = NULL;
6004 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006005#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006006 if( session->peer_cert_digest != NULL )
6007 {
6008 /* Zeroization is not necessary. */
6009 mbedtls_free( session->peer_cert_digest );
6010 session->peer_cert_digest = NULL;
6011 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6012 session->peer_cert_digest_len = 0;
6013 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006014#else
6015 ((void) session);
6016#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006017}
6018#endif /* MBEDTLS_X509_CRT_PARSE_C */
6019
Paul Bakker5121ce52009-01-03 21:22:43 +00006020/*
6021 * Handshake functions
6022 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006023#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006024/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006026{
Hanno Becker8759e162017-12-27 21:34:08 +00006027 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
Hanno Becker5097cba2019-02-05 13:36:46 +00006031 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006034 ssl->state++;
6035 return( 0 );
6036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040}
6041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006042int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006043{
Hanno Becker8759e162017-12-27 21:34:08 +00006044 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006047
Hanno Becker5097cba2019-02-05 13:36:46 +00006048 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006051 ssl->state++;
6052 return( 0 );
6053 }
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006057}
Gilles Peskinef9828522017-05-03 12:28:43 +02006058
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006059#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006060/* Some certificate support -> implement write and parse */
6061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006065 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006067 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006070
Hanno Becker5097cba2019-02-05 13:36:46 +00006071 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006074 ssl->state++;
6075 return( 0 );
6076 }
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006079 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 {
6081 if( ssl->client_auth == 0 )
6082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006084 ssl->state++;
6085 return( 0 );
6086 }
6087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 /*
6090 * If using SSLv3 and got no cert, send an Alert message
6091 * (otherwise an empty Certificate message will be sent).
6092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6094 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 {
6096 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6098 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6099 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 goto write_msg;
6103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006105 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#endif /* MBEDTLS_SSL_CLI_C */
6107#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006108 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6113 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 }
6115 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006116#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119
6120 /*
6121 * 0 . 0 handshake type
6122 * 1 . 3 handshake length
6123 * 4 . 6 length of all certs
6124 * 7 . 9 length of cert. 1
6125 * 10 . n-1 peer certificate
6126 * n . n+2 length of cert. 2
6127 * n+3 . ... upper level cert, etc.
6128 */
6129 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006131
Paul Bakker29087132010-03-21 21:03:34 +00006132 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
6134 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006135 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006138 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 }
6141
6142 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6143 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6144 ssl->out_msg[i + 2] = (unsigned char)( n );
6145
6146 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6147 i += n; crt = crt->next;
6148 }
6149
6150 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6151 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6152 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6153
6154 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006155 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6156 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006157
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006158#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006159write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
6162 ssl->state++;
6163
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006164 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 return( ret );
6168 }
6169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Paul Bakkered27a042013-04-18 22:46:23 +02006172 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173}
6174
Hanno Becker285ff0c2019-01-31 07:44:03 +00006175#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006176
6177#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006178static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6179 unsigned char *crt_buf,
6180 size_t crt_buf_len )
6181{
6182 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6183
6184 if( peer_crt == NULL )
6185 return( -1 );
6186
6187 if( peer_crt->raw.len != crt_buf_len )
6188 return( -1 );
6189
Hanno Becker68b856d2019-02-08 14:00:04 +00006190 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006191}
Hanno Becker5882dd02019-06-06 16:25:57 +01006192#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006193static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6194 unsigned char *crt_buf,
6195 size_t crt_buf_len )
6196{
6197 int ret;
6198 unsigned char const * const peer_cert_digest =
6199 ssl->session->peer_cert_digest;
6200 mbedtls_md_type_t const peer_cert_digest_type =
6201 ssl->session->peer_cert_digest_type;
6202 mbedtls_md_info_t const * const digest_info =
6203 mbedtls_md_info_from_type( peer_cert_digest_type );
6204 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6205 size_t digest_len;
6206
6207 if( peer_cert_digest == NULL || digest_info == NULL )
6208 return( -1 );
6209
6210 digest_len = mbedtls_md_get_size( digest_info );
6211 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6212 return( -1 );
6213
6214 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6215 if( ret != 0 )
6216 return( -1 );
6217
6218 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6219}
Hanno Becker5882dd02019-06-06 16:25:57 +01006220#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006221#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006222
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006223/*
6224 * Once the certificate message is read, parse it into a cert chain and
6225 * perform basic checks, but leave actual verification to the caller
6226 */
Hanno Becker35e41772019-02-05 15:37:23 +00006227static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6228 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006229{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006230 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006231#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6232 int crt_cnt=0;
6233#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006234 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006235 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6241 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 }
6244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006245 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6246 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006249 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6250 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 }
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006255
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006258 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006259 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006260
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006261 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6266 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006268 }
6269
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6271 i += 3;
6272
Hanno Becker33c3dc82019-01-30 14:46:46 +00006273 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 while( i < ssl->in_hslen )
6275 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006276 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006277 if ( i + 3 > ssl->in_hslen ) {
6278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006279 mbedtls_ssl_send_alert_message( ssl,
6280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006282 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6283 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006284 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6285 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 if( ssl->in_msg[i] != 0 )
6287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006289 mbedtls_ssl_send_alert_message( ssl,
6290 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6291 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 }
6294
Hanno Becker33c3dc82019-01-30 14:46:46 +00006295 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6297 | (unsigned int) ssl->in_msg[i + 2];
6298 i += 3;
6299
6300 if( n < 128 || i + n > ssl->in_hslen )
6301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006303 mbedtls_ssl_send_alert_message( ssl,
6304 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6305 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006307 }
6308
Hanno Becker33c3dc82019-01-30 14:46:46 +00006309 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006310#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6311 if( crt_cnt++ == 0 &&
6312 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6313 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006314 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006315 /* During client-side renegotiation, check that the server's
6316 * end-CRTs hasn't changed compared to the initial handshake,
6317 * mitigating the triple handshake attack. On success, reuse
6318 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006319 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6320 if( ssl_check_peer_crt_unchanged( ssl,
6321 &ssl->in_msg[i],
6322 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006323 {
Hanno Becker35e41772019-02-05 15:37:23 +00006324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6325 mbedtls_ssl_send_alert_message( ssl,
6326 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6327 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6328 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006329 }
Hanno Becker35e41772019-02-05 15:37:23 +00006330
6331 /* Now we can safely free the original chain. */
6332 ssl_clear_peer_cert( ssl->session );
6333 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006334#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6335
Hanno Becker33c3dc82019-01-30 14:46:46 +00006336 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006337#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006338 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006339#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006340 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006341 * it in-place from the input buffer instead of making a copy. */
6342 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6343#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006344 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006346 case 0: /*ok*/
6347 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6348 /* Ignore certificate with an unknown algorithm: maybe a
6349 prior certificate was already trusted. */
6350 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006351
Hanno Becker33c3dc82019-01-30 14:46:46 +00006352 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6353 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6354 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006355
Hanno Becker33c3dc82019-01-30 14:46:46 +00006356 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6357 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6358 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006359
Hanno Becker33c3dc82019-01-30 14:46:46 +00006360 default:
6361 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6362 crt_parse_der_failed:
6363 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6364 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6365 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 }
6367
6368 i += n;
6369 }
6370
Hanno Becker35e41772019-02-05 15:37:23 +00006371 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006372 return( 0 );
6373}
6374
Hanno Beckerb8a08572019-02-05 12:49:06 +00006375#if defined(MBEDTLS_SSL_SRV_C)
6376static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6377{
6378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6379 return( -1 );
6380
6381#if defined(MBEDTLS_SSL_PROTO_SSL3)
6382 /*
6383 * Check if the client sent an empty certificate
6384 */
6385 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6386 {
6387 if( ssl->in_msglen == 2 &&
6388 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6389 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6390 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6393 return( 0 );
6394 }
6395
6396 return( -1 );
6397 }
6398#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6399
6400#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6401 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6402 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6403 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6404 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6405 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6406 {
6407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6408 return( 0 );
6409 }
6410
Hanno Beckerb8a08572019-02-05 12:49:06 +00006411#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6412 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006413
6414 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006415}
6416#endif /* MBEDTLS_SSL_SRV_C */
6417
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006418/* Check if a certificate message is expected.
6419 * Return either
6420 * - SSL_CERTIFICATE_EXPECTED, or
6421 * - SSL_CERTIFICATE_SKIP
6422 * indicating whether a Certificate message is expected or not.
6423 */
6424#define SSL_CERTIFICATE_EXPECTED 0
6425#define SSL_CERTIFICATE_SKIP 1
6426static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6427 int authmode )
6428{
6429 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6430 ssl->handshake->ciphersuite_info;
6431
6432 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6433 return( SSL_CERTIFICATE_SKIP );
6434
6435#if defined(MBEDTLS_SSL_SRV_C)
6436 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6437 {
6438 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6439 return( SSL_CERTIFICATE_SKIP );
6440
6441 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6442 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006443 ssl->session_negotiate->verify_result =
6444 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6445 return( SSL_CERTIFICATE_SKIP );
6446 }
6447 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006448#else
6449 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006450#endif /* MBEDTLS_SSL_SRV_C */
6451
6452 return( SSL_CERTIFICATE_EXPECTED );
6453}
6454
Hanno Becker3cf50612019-02-05 14:36:34 +00006455static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6456 int authmode,
6457 mbedtls_x509_crt *chain,
6458 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006459{
Hanno Becker613d4902019-02-05 13:11:17 +00006460 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6462 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006463 mbedtls_x509_crt *ca_chain;
6464 mbedtls_x509_crl *ca_crl;
6465
6466 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6467 return( 0 );
6468
6469#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6470 if( ssl->handshake->sni_ca_chain != NULL )
6471 {
6472 ca_chain = ssl->handshake->sni_ca_chain;
6473 ca_crl = ssl->handshake->sni_ca_crl;
6474 }
6475 else
6476#endif
6477 {
6478 ca_chain = ssl->conf->ca_chain;
6479 ca_crl = ssl->conf->ca_crl;
6480 }
6481
6482 /*
6483 * Main check: verify certificate
6484 */
6485 ret = mbedtls_x509_crt_verify_restartable(
6486 chain,
6487 ca_chain, ca_crl,
6488 ssl->conf->cert_profile,
6489 ssl->hostname,
6490 &ssl->session_negotiate->verify_result,
6491 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6492
6493 if( ret != 0 )
6494 {
6495 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6496 }
6497
6498#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6499 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6500 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6501#endif
6502
6503 /*
6504 * Secondary checks: always done, but change 'ret' only if it was 0
6505 */
6506
6507#if defined(MBEDTLS_ECP_C)
6508 {
6509 const mbedtls_pk_context *pk = &chain->pk;
6510
6511 /* If certificate uses an EC key, make sure the curve is OK */
6512 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6513 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6514 {
6515 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6516
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6518 if( ret == 0 )
6519 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6520 }
6521 }
6522#endif /* MBEDTLS_ECP_C */
6523
6524 if( mbedtls_ssl_check_cert_usage( chain,
6525 ciphersuite_info,
6526 ! ssl->conf->endpoint,
6527 &ssl->session_negotiate->verify_result ) != 0 )
6528 {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6530 if( ret == 0 )
6531 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6532 }
6533
6534 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6535 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6536 * with details encoded in the verification flags. All other kinds
6537 * of error codes, including those from the user provided f_vrfy
6538 * functions, are treated as fatal and lead to a failure of
6539 * ssl_parse_certificate even if verification was optional. */
6540 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6541 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6542 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6543 {
6544 ret = 0;
6545 }
6546
6547 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6548 {
6549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6550 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6551 }
6552
6553 if( ret != 0 )
6554 {
6555 uint8_t alert;
6556
6557 /* The certificate may have been rejected for several reasons.
6558 Pick one and send the corresponding alert. Which alert to send
6559 may be a subject of debate in some cases. */
6560 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6561 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6563 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6565 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6567 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6569 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6573 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6574 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6575 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6576 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6577 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6578 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6579 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6580 else
6581 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6582 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6583 alert );
6584 }
6585
6586#if defined(MBEDTLS_DEBUG_C)
6587 if( ssl->session_negotiate->verify_result != 0 )
6588 {
6589 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6590 ssl->session_negotiate->verify_result ) );
6591 }
6592 else
6593 {
6594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6595 }
6596#endif /* MBEDTLS_DEBUG_C */
6597
6598 return( ret );
6599}
6600
Hanno Becker34106f62019-02-08 14:59:05 +00006601#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006602
6603#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006604static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6605 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006606{
6607 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006608 /* Remember digest of the peer's end-CRT. */
6609 ssl->session_negotiate->peer_cert_digest =
6610 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6611 if( ssl->session_negotiate->peer_cert_digest == NULL )
6612 {
6613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6614 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6615 mbedtls_ssl_send_alert_message( ssl,
6616 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6617 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6618
6619 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6620 }
6621
6622 ret = mbedtls_md( mbedtls_md_info_from_type(
6623 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6624 start, len,
6625 ssl->session_negotiate->peer_cert_digest );
6626
6627 ssl->session_negotiate->peer_cert_digest_type =
6628 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6629 ssl->session_negotiate->peer_cert_digest_len =
6630 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6631
6632 return( ret );
6633}
Hanno Becker5882dd02019-06-06 16:25:57 +01006634#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006635
6636static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6637 unsigned char *start, size_t len )
6638{
6639 unsigned char *end = start + len;
6640 int ret;
6641
6642 /* Make a copy of the peer's raw public key. */
6643 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6644 ret = mbedtls_pk_parse_subpubkey( &start, end,
6645 &ssl->handshake->peer_pubkey );
6646 if( ret != 0 )
6647 {
6648 /* We should have parsed the public key before. */
6649 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6650 }
6651
6652 return( 0 );
6653}
6654#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6655
Hanno Becker3cf50612019-02-05 14:36:34 +00006656int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6657{
6658 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006659 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006660#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6661 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6662 ? ssl->handshake->sni_authmode
6663 : ssl->conf->authmode;
6664#else
6665 const int authmode = ssl->conf->authmode;
6666#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006667 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006668 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006669
6670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6671
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006672 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6673 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006674 {
6675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006676 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006677 }
6678
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006679#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6680 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006681 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006682 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006683 chain = ssl->handshake->ecrs_peer_cert;
6684 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006685 goto crt_verify;
6686 }
6687#endif
6688
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006689 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006690 {
6691 /* mbedtls_ssl_read_record may have sent an alert already. We
6692 let it decide whether to alert. */
6693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006694 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006695 }
6696
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006697#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006698 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6699 {
6700 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701
Hanno Beckerb8a08572019-02-05 12:49:06 +00006702 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006703 ret = 0;
6704 else
6705 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006706
Hanno Becker613d4902019-02-05 13:11:17 +00006707 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006708 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006709#endif /* MBEDTLS_SSL_SRV_C */
6710
Hanno Becker35e41772019-02-05 15:37:23 +00006711 /* Clear existing peer CRT structure in case we tried to
6712 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006713 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006714
6715 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6716 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006717 {
6718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6719 sizeof( mbedtls_x509_crt ) ) );
6720 mbedtls_ssl_send_alert_message( ssl,
6721 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6722 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006723
Hanno Beckere4aeb762019-02-05 17:19:52 +00006724 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6725 goto exit;
6726 }
6727 mbedtls_x509_crt_init( chain );
6728
6729 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006730 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006731 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006732
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006733#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6734 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006735 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006736
6737crt_verify:
6738 if( ssl->handshake->ecrs_enabled)
6739 rs_ctx = &ssl->handshake->ecrs_ctx;
6740#endif
6741
Hanno Becker3cf50612019-02-05 14:36:34 +00006742 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006743 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006744 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006745 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006746
Hanno Becker3008d282019-02-05 17:02:28 +00006747#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006748 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006749 size_t pk_len;
6750 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006751
Hanno Becker34106f62019-02-08 14:59:05 +00006752 /* We parse the CRT chain without copying, so
6753 * these pointers point into the input buffer,
6754 * and are hence still valid after freeing the
6755 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006756
Hanno Becker5882dd02019-06-06 16:25:57 +01006757#if defined(MBEDTLS_SSL_RENEGOTIATION)
6758 unsigned char *crt_start;
6759 size_t crt_len;
6760
Hanno Becker34106f62019-02-08 14:59:05 +00006761 crt_start = chain->raw.p;
6762 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006763#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006764
Hanno Becker34106f62019-02-08 14:59:05 +00006765 pk_start = chain->pk_raw.p;
6766 pk_len = chain->pk_raw.len;
6767
6768 /* Free the CRT structures before computing
6769 * digest and copying the peer's public key. */
6770 mbedtls_x509_crt_free( chain );
6771 mbedtls_free( chain );
6772 chain = NULL;
6773
Hanno Becker5882dd02019-06-06 16:25:57 +01006774#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006775 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006777 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006778#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006779
Hanno Becker34106f62019-02-08 14:59:05 +00006780 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006782 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006783 }
Hanno Becker34106f62019-02-08 14:59:05 +00006784#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6785 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006786 ssl->session_negotiate->peer_cert = chain;
6787 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006788#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006791
Hanno Becker613d4902019-02-05 13:11:17 +00006792exit:
6793
Hanno Beckere4aeb762019-02-05 17:19:52 +00006794 if( ret == 0 )
6795 ssl->state++;
6796
6797#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6798 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6799 {
6800 ssl->handshake->ecrs_peer_cert = chain;
6801 chain = NULL;
6802 }
6803#endif
6804
6805 if( chain != NULL )
6806 {
6807 mbedtls_x509_crt_free( chain );
6808 mbedtls_free( chain );
6809 }
6810
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 return( ret );
6812}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006813#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006815int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006816{
6817 int ret;
6818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 ssl->out_msglen = 1;
6823 ssl->out_msg[0] = 1;
6824
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 ssl->state++;
6826
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006827 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006829 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 return( ret );
6831 }
6832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
6835 return( 0 );
6836}
6837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006839{
6840 int ret;
6841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006843
Hanno Becker327c93b2018-08-15 13:56:18 +01006844 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 return( ret );
6848 }
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006853 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6854 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 }
6857
Hanno Beckere678eaa2018-08-21 14:57:46 +01006858 /* CCS records are only accepted if they have length 1 and content '1',
6859 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006860
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006861 /*
6862 * Switch to our negotiated transform and session parameters for inbound
6863 * data.
6864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006866 ssl->transform_in = ssl->transform_negotiate;
6867 ssl->session_in = ssl->session_negotiate;
6868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006870 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006873 ssl_dtls_replay_reset( ssl );
6874#endif
6875
6876 /* Increment epoch */
6877 if( ++ssl->in_epoch == 0 )
6878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006880 /* This is highly unlikely to happen for legitimate reasons, so
6881 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006883 }
6884 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006885 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006887#if defined(MBEDTLS_SSL_PROTO_TLS)
6888 {
6889 memset( ssl->in_ctr, 0, 8 );
6890 }
6891#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006892
Hanno Beckerf5970a02019-05-08 09:38:41 +01006893 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006895#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6896 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006901 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6902 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006903 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006904 }
6905 }
6906#endif
6907
Paul Bakker5121ce52009-01-03 21:22:43 +00006908 ssl->state++;
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006911
6912 return( 0 );
6913}
6914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006915void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6916 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006917{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006918 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6921 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6922 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006923 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006924 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006925#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6927#if defined(MBEDTLS_SHA512_C)
6928 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006929 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6930 else
6931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932#if defined(MBEDTLS_SHA256_C)
6933 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006934 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006935 else
6936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006940 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006941 }
Paul Bakker380da532012-04-18 16:10:25 +00006942}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6947 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006948 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6949 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006950#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6952#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006953 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006956 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006959}
6960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006962 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006963{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6965 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006966 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6967 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6970#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006971 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006974 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006976#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006977}
6978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6980 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6981static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006982 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006983{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006984 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6985 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006986}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006987#endif
Paul Bakker380da532012-04-18 16:10:25 +00006988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6990#if defined(MBEDTLS_SHA256_C)
6991static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006992 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006993{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006994 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006996#endif
Paul Bakker380da532012-04-18 16:10:25 +00006997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998#if defined(MBEDTLS_SHA512_C)
6999static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007000 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007001{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007002 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007003}
Paul Bakker769075d2012-11-24 11:26:46 +01007004#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007008static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007009 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007010{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007011 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007012 mbedtls_md5_context md5;
7013 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007014
Paul Bakker5121ce52009-01-03 21:22:43 +00007015 unsigned char padbuf[48];
7016 unsigned char md5sum[16];
7017 unsigned char sha1sum[20];
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007020 if( !session )
7021 session = ssl->session;
7022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007024
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007025 mbedtls_md5_init( &md5 );
7026 mbedtls_sha1_init( &sha1 );
7027
7028 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7029 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007030
7031 /*
7032 * SSLv3:
7033 * hash =
7034 * MD5( master + pad2 +
7035 * MD5( handshake + sender + master + pad1 ) )
7036 * + SHA1( master + pad2 +
7037 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038 */
7039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007041 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7042 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007046 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7047 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007051 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Paul Bakker1ef83d62012-04-11 12:09:53 +00007053 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007054
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007055 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7056 mbedtls_md5_update_ret( &md5, session->master, 48 );
7057 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7058 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007060 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7061 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7062 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7063 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Paul Bakker1ef83d62012-04-11 12:09:53 +00007065 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007067 mbedtls_md5_starts_ret( &md5 );
7068 mbedtls_md5_update_ret( &md5, session->master, 48 );
7069 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7070 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7071 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007073 mbedtls_sha1_starts_ret( &sha1 );
7074 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7075 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7076 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7077 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007081 mbedtls_md5_free( &md5 );
7082 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007084 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7085 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7086 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007095{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007096 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007097 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007098 mbedtls_md5_context md5;
7099 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007100 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007103 if( !session )
7104 session = ssl->session;
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007108 mbedtls_md5_init( &md5 );
7109 mbedtls_sha1_init( &sha1 );
7110
7111 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7112 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007113
Paul Bakker1ef83d62012-04-11 12:09:53 +00007114 /*
7115 * TLSv1:
7116 * hash = PRF( master, finished_label,
7117 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7118 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007121 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7122 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007123#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007126 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7127 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007128#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007131 ? "client finished"
7132 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007133
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007134 mbedtls_md5_finish_ret( &md5, padbuf );
7135 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007136
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007137 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007138 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007141
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007142 mbedtls_md5_free( &md5 );
7143 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007145 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7152#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007153static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155{
7156 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007157 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007158 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007159 unsigned char padbuf[32];
7160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007162 if( !session )
7163 session = ssl->session;
7164
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007165 mbedtls_sha256_init( &sha256 );
7166
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007169 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170
7171 /*
7172 * TLSv1.2:
7173 * hash = PRF( master, finished_label,
7174 * Hash( handshake ) )[0.11]
7175 */
7176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#if !defined(MBEDTLS_SHA256_ALT)
7178 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007179 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007180#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007183 ? "client finished"
7184 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007186 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007188 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007189 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007193 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007195 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007202static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007204{
7205 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007206 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007207 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007208 unsigned char padbuf[48];
7209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007211 if( !session )
7212 session = ssl->session;
7213
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007214 mbedtls_sha512_init( &sha512 );
7215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007217
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007218 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007219
7220 /*
7221 * TLSv1.2:
7222 * hash = PRF( master, finished_label,
7223 * Hash( handshake ) )[0.11]
7224 */
7225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007226#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007227 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7228 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007229#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007232 ? "client finished"
7233 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007234
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007236
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007237 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007238 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007241
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007242 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007244 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#endif /* MBEDTLS_SHA512_C */
7249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007252{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007254
7255 /*
7256 * Free our handshake params
7257 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007258 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007260 ssl->handshake = NULL;
7261
7262 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007263 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007264 */
7265 if( ssl->transform )
7266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267 mbedtls_ssl_transform_free( ssl->transform );
7268 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007269 }
7270 ssl->transform = ssl->transform_negotiate;
7271 ssl->transform_negotiate = NULL;
7272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007274}
7275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_SSL_RENEGOTIATION)
7281 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007284 ssl->renego_records_seen = 0;
7285 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007286#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007287
7288 /*
7289 * Free the previous session and switch in the current one
7290 */
Paul Bakker0a597072012-09-25 21:55:46 +00007291 if( ssl->session )
7292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007294 /* RFC 7366 3.1: keep the EtM state */
7295 ssl->session_negotiate->encrypt_then_mac =
7296 ssl->session->encrypt_then_mac;
7297#endif
7298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 mbedtls_ssl_session_free( ssl->session );
7300 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007301 }
7302 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007303 ssl->session_negotiate = NULL;
7304
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007305#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Paul Bakker0a597072012-09-25 21:55:46 +00007306 /*
7307 * Add cache entry
7308 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007309 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007310 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007311 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007312 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007313 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007315 }
Manuel Pégourié-Gonnard7b80c642019-07-02 16:21:30 +02007316#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker0a597072012-09-25 21:55:46 +00007317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007319 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007320 ssl->handshake->flight != NULL )
7321 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007322 /* Cancel handshake timer */
7323 ssl_set_timer( ssl, 0 );
7324
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007325 /* Keep last flight around in case we need to resend it:
7326 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007328 }
7329 else
7330#endif
7331 ssl_handshake_wrapup_free_hs_transform( ssl );
7332
Paul Bakker48916f92012-09-16 19:57:18 +00007333 ssl->state++;
7334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007336}
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007340 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007343
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007344 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007345
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007346 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007348 /*
7349 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7350 * may define some other value. Currently (early 2016), no defined
7351 * ciphersuite does this (and this is unlikely to change as activity has
7352 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007357 ssl->verify_data_len = hash_len;
7358 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007359#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007360
Paul Bakker5121ce52009-01-03 21:22:43 +00007361 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7363 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007364
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007365#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker5121ce52009-01-03 21:22:43 +00007366 /*
7367 * In case of session resuming, invert the client and server
7368 * ChangeCipherSpec messages order.
7369 */
Paul Bakker0a597072012-09-25 21:55:46 +00007370 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007373 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007380 }
7381 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007382#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 ssl->state++;
7384
Paul Bakker48916f92012-09-16 19:57:18 +00007385 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007386 * Switch to our negotiated transform and session parameters for outbound
7387 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007392 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007393 {
7394 unsigned char i;
7395
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007396 /* Remember current epoch settings for resending */
7397 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007398 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007399
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007400 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007401 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007402
7403 /* Increment epoch */
7404 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007405 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007406 break;
7407
7408 /* The loop goes to its end iff the counter is wrapping */
7409 if( i == 0 )
7410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7412 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007413 }
7414 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007415 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007417#if defined(MBEDTLS_SSL_PROTO_TLS)
7418 {
7419 memset( ssl->cur_out_ctr, 0, 8 );
7420 }
7421#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007423 ssl->transform_out = ssl->transform_negotiate;
7424 ssl->session_out = ssl->session_negotiate;
7425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7427 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007428 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7432 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007433 }
7434 }
7435#endif
7436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007438 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007440#endif
7441
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007442 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007443 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007444 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007445 return( ret );
7446 }
7447
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007449 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007450 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7451 {
7452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7453 return( ret );
7454 }
7455#endif
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458
7459 return( 0 );
7460}
7461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007463#define SSL_MAX_HASH_LEN 36
7464#else
7465#define SSL_MAX_HASH_LEN 12
7466#endif
7467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469{
Paul Bakker23986e52011-04-24 08:57:21 +00007470 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007471 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007472 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007475
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007476 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007477
Hanno Becker327c93b2018-08-15 13:56:18 +01007478 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007481 return( ret );
7482 }
7483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7488 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007490 }
7491
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007492 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#if defined(MBEDTLS_SSL_PROTO_SSL3)
7494 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007495 hash_len = 36;
7496 else
7497#endif
7498 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7501 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7505 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 }
7508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007510 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7514 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 }
7517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007519 ssl->verify_data_len = hash_len;
7520 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007521#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007522
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007523#if !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Paul Bakker0a597072012-09-25 21:55:46 +00007524 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007527 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007531 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007533#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007534 }
7535 else
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03007536#endif /* !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 ssl->state++;
7538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007540 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007542#endif
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007545
7546 return( 0 );
7547}
7548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7554 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7555 mbedtls_md5_init( &handshake->fin_md5 );
7556 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007557 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7558 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007559#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7561#if defined(MBEDTLS_SHA256_C)
7562 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007563 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SHA512_C)
7566 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007567 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007570
7571 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007572
7573#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7574 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7575 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7576#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_DHM_C)
7579 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if defined(MBEDTLS_ECDH_C)
7582 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007583#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007584#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007585 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007586#if defined(MBEDTLS_SSL_CLI_C)
7587 handshake->ecjpake_cache = NULL;
7588 handshake->ecjpake_cache_len = 0;
7589#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007590#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007591
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007592#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007593 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007594#endif
7595
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007596#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7597 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7598#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007599
7600#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7601 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7602 mbedtls_pk_init( &handshake->peer_pubkey );
7603#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604}
7605
Hanno Becker611a83b2018-01-03 14:27:32 +00007606void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007607{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7611 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007612
Hanno Becker92231322018-01-03 15:32:51 +00007613#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 mbedtls_md_init( &transform->md_ctx_enc );
7615 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007616#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007617}
7618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007622}
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007626 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007627 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007629 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007632 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007633
7634 /*
7635 * Either the pointers are now NULL or cleared properly and can be freed.
7636 * Now allocate missing structures.
7637 */
7638 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007639 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007640 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007641 }
Paul Bakker48916f92012-09-16 19:57:18 +00007642
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007643 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007644 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007645 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007646 }
Paul Bakker48916f92012-09-16 19:57:18 +00007647
Paul Bakker82788fb2014-10-20 13:59:19 +02007648 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007649 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007650 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007651 }
Paul Bakker48916f92012-09-16 19:57:18 +00007652
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007653 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007654 if( ssl->handshake == NULL ||
7655 ssl->transform_negotiate == NULL ||
7656 ssl->session_negotiate == NULL )
7657 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007660 mbedtls_free( ssl->handshake );
7661 mbedtls_free( ssl->transform_negotiate );
7662 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007663
7664 ssl->handshake = NULL;
7665 ssl->transform_negotiate = NULL;
7666 ssl->session_negotiate = NULL;
7667
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007668 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007669 }
7670
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007671 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007672 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007673 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007674 ssl_handshake_params_init( ssl->handshake );
7675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007678 {
7679 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007680
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7682 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7683 else
7684 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007685
7686 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007687 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007688#endif
7689
Paul Bakker48916f92012-09-16 19:57:18 +00007690 return( 0 );
7691}
7692
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007693#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007694/* Dummy cookie callbacks for defaults */
7695static int ssl_cookie_write_dummy( void *ctx,
7696 unsigned char **p, unsigned char *end,
7697 const unsigned char *cli_id, size_t cli_id_len )
7698{
7699 ((void) ctx);
7700 ((void) p);
7701 ((void) end);
7702 ((void) cli_id);
7703 ((void) cli_id_len);
7704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007706}
7707
7708static int ssl_cookie_check_dummy( void *ctx,
7709 const unsigned char *cookie, size_t cookie_len,
7710 const unsigned char *cli_id, size_t cli_id_len )
7711{
7712 ((void) ctx);
7713 ((void) cookie);
7714 ((void) cookie_len);
7715 ((void) cli_id);
7716 ((void) cli_id_len);
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007719}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007720#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007721
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007722/* Once ssl->out_hdr as the address of the beginning of the
7723 * next outgoing record is set, deduce the other pointers.
7724 *
7725 * Note: For TLS, we save the implicit record sequence number
7726 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7727 * and the caller has to make sure there's space for this.
7728 */
7729
7730static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7731 mbedtls_ssl_transform *transform )
7732{
7733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007734 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007735 {
7736 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007738 ssl->out_cid = ssl->out_ctr + 8;
7739 ssl->out_len = ssl->out_cid;
7740 if( transform != NULL )
7741 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007742#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007743 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007745 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007746 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007747 MBEDTLS_SSL_TRANSPORT_ELSE
7748#endif /* MBEDTLS_SSL_PROTO_DTLS */
7749#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007750 {
7751 ssl->out_ctr = ssl->out_hdr - 8;
7752 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007753#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007754 ssl->out_cid = ssl->out_len;
7755#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007756 ssl->out_iv = ssl->out_hdr + 5;
7757 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007758#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007759
7760 /* Adjust out_msg to make space for explicit IV, if used. */
7761 if( transform != NULL &&
7762 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7763 {
7764 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7765 }
7766 else
7767 ssl->out_msg = ssl->out_iv;
7768}
7769
7770/* Once ssl->in_hdr as the address of the beginning of the
7771 * next incoming record is set, deduce the other pointers.
7772 *
7773 * Note: For TLS, we save the implicit record sequence number
7774 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7775 * and the caller has to make sure there's space for this.
7776 */
7777
Hanno Beckerf5970a02019-05-08 09:38:41 +01007778static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007779{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007780 /* This function sets the pointers to match the case
7781 * of unprotected TLS/DTLS records, with both ssl->in_iv
7782 * and ssl->in_msg pointing to the beginning of the record
7783 * content.
7784 *
7785 * When decrypting a protected record, ssl->in_msg
7786 * will be shifted to point to the beginning of the
7787 * record plaintext.
7788 */
7789
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007790#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007791 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007792 {
Hanno Becker70e79282019-05-03 14:34:53 +01007793 /* This sets the header pointers to match records
7794 * without CID. When we receive a record containing
7795 * a CID, the fields are shifted accordingly in
7796 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007797 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007798#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007799 ssl->in_cid = ssl->in_ctr + 8;
7800 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007801#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007802 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007803#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007804 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007805 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007806 MBEDTLS_SSL_TRANSPORT_ELSE
7807#endif /* MBEDTLS_SSL_PROTO_DTLS */
7808#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007809 {
7810 ssl->in_ctr = ssl->in_hdr - 8;
7811 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007812#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007813 ssl->in_cid = ssl->in_len;
7814#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007815 ssl->in_iv = ssl->in_hdr + 5;
7816 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007817#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007818
Hanno Beckerf5970a02019-05-08 09:38:41 +01007819 /* This will be adjusted at record decryption time. */
7820 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007821}
7822
Paul Bakker5121ce52009-01-03 21:22:43 +00007823/*
7824 * Initialize an SSL context
7825 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007826void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7827{
7828 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7829}
7830
7831/*
7832 * Setup an SSL context
7833 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007834
7835static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7836{
7837 /* Set the incoming and outgoing record pointers. */
7838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007839 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007840 {
7841 ssl->out_hdr = ssl->out_buf;
7842 ssl->in_hdr = ssl->in_buf;
7843 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007844 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007845#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007846#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007847 {
7848 ssl->out_hdr = ssl->out_buf + 8;
7849 ssl->in_hdr = ssl->in_buf + 8;
7850 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007851#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007852
7853 /* Derive other internal pointers. */
7854 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007855 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007856}
7857
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007858int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007859 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007860{
Paul Bakker48916f92012-09-16 19:57:18 +00007861 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007862
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007863 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007864
7865 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007866 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007867 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007868
7869 /* Set to NULL in case of an error condition */
7870 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007871
Angus Grattond8213d02016-05-25 20:56:48 +10007872 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7873 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 {
Angus Grattond8213d02016-05-25 20:56:48 +10007875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007876 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007877 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007878 }
7879
7880 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7881 if( ssl->out_buf == NULL )
7882 {
7883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007884 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007885 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007886 }
7887
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007888 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007889
Paul Bakker48916f92012-09-16 19:57:18 +00007890 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007891 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007892
7893 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007894
7895error:
7896 mbedtls_free( ssl->in_buf );
7897 mbedtls_free( ssl->out_buf );
7898
7899 ssl->conf = NULL;
7900
7901 ssl->in_buf = NULL;
7902 ssl->out_buf = NULL;
7903
7904 ssl->in_hdr = NULL;
7905 ssl->in_ctr = NULL;
7906 ssl->in_len = NULL;
7907 ssl->in_iv = NULL;
7908 ssl->in_msg = NULL;
7909
7910 ssl->out_hdr = NULL;
7911 ssl->out_ctr = NULL;
7912 ssl->out_len = NULL;
7913 ssl->out_iv = NULL;
7914 ssl->out_msg = NULL;
7915
k-stachowiak9f7798e2018-07-31 16:52:32 +02007916 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007917}
7918
7919/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007920 * Reset an initialized and used SSL context for re-use while retaining
7921 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007922 *
7923 * If partial is non-zero, keep data in the input buffer and client ID.
7924 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007925 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007926static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007927{
Paul Bakker48916f92012-09-16 19:57:18 +00007928 int ret;
7929
Hanno Becker7e772132018-08-10 12:38:21 +01007930#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7931 !defined(MBEDTLS_SSL_SRV_C)
7932 ((void) partial);
7933#endif
7934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007936
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007937 /* Cancel any possibly running timer */
7938 ssl_set_timer( ssl, 0 );
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940#if defined(MBEDTLS_SSL_RENEGOTIATION)
7941 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007942 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007943
7944 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7946 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007948 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007949
Paul Bakker7eb013f2011-10-06 12:37:39 +00007950 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007951 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007952
7953 ssl->in_msgtype = 0;
7954 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007955#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007956 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007957 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007960 ssl_dtls_replay_reset( ssl );
7961#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007962
7963 ssl->in_hslen = 0;
7964 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007965
7966 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007967
7968 ssl->out_msgtype = 0;
7969 ssl->out_msglen = 0;
7970 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7972 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007973 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007974#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007975
Hanno Becker19859472018-08-06 09:40:20 +01007976 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7977
Paul Bakker48916f92012-09-16 19:57:18 +00007978 ssl->transform_in = NULL;
7979 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007980
Hanno Becker78640902018-08-13 16:35:15 +01007981 ssl->session_in = NULL;
7982 ssl->session_out = NULL;
7983
Angus Grattond8213d02016-05-25 20:56:48 +10007984 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007985
7986#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007987 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007988#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7989 {
7990 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007991 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007992 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7995 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7998 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8001 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008002 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008003 }
8004#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008005
Paul Bakker48916f92012-09-16 19:57:18 +00008006 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008 mbedtls_ssl_transform_free( ssl->transform );
8009 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008010 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008011 }
Paul Bakker48916f92012-09-16 19:57:18 +00008012
Paul Bakkerc0463502013-02-14 11:19:38 +01008013 if( ssl->session )
8014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 mbedtls_ssl_session_free( ssl->session );
8016 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008017 ssl->session = NULL;
8018 }
8019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008021 ssl->alpn_chosen = NULL;
8022#endif
8023
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008024#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008025#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008026 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008027#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008028 {
8029 mbedtls_free( ssl->cli_id );
8030 ssl->cli_id = NULL;
8031 ssl->cli_id_len = 0;
8032 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008033#endif
8034
Paul Bakker48916f92012-09-16 19:57:18 +00008035 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8036 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008037
8038 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008039}
8040
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008041/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008042 * Reset an initialized and used SSL context for re-use while retaining
8043 * all application-set variables, function pointers and data.
8044 */
8045int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8046{
8047 return( ssl_session_reset_int( ssl, 0 ) );
8048}
8049
8050/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008051 * SSL set accessors
8052 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008053void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008055 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008056}
8057
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008058void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008060 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008061}
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008064void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008066 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008067}
8068#endif
8069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008071void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008073 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008074}
8075#endif
8076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008078
Hanno Becker1841b0a2018-08-24 11:13:57 +01008079void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8080 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008081{
8082 ssl->disable_datagram_packing = !allow_packing;
8083}
8084
8085void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8086 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008087{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008088 conf->hs_timeout_min = min;
8089 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008090}
8091#endif
8092
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008093void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008095 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008096}
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008099void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008100 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008101 void *p_vrfy )
8102{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008103 conf->f_vrfy = f_vrfy;
8104 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008106#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008107
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008108void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008109 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008110 void *p_rng )
8111{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008112 conf->f_rng = f_rng;
8113 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008114}
8115
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008116void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008117 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008118 void *p_dbg )
8119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008120 conf->f_dbg = f_dbg;
8121 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008122}
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008125 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008126 mbedtls_ssl_send_t *f_send,
8127 mbedtls_ssl_recv_t *f_recv,
8128 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008129{
8130 ssl->p_bio = p_bio;
8131 ssl->f_send = f_send;
8132 ssl->f_recv = f_recv;
8133 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008134}
8135
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008136#if defined(MBEDTLS_SSL_PROTO_DTLS)
8137void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8138{
8139 ssl->mtu = mtu;
8140}
8141#endif
8142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008143void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008144{
8145 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008146}
8147
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008148void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8149 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008150 mbedtls_ssl_set_timer_t *f_set_timer,
8151 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008152{
8153 ssl->p_timer = p_timer;
8154 ssl->f_set_timer = f_set_timer;
8155 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008156
8157 /* Make sure we start with no timer running */
8158 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008159}
8160
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008161#if defined(MBEDTLS_SSL_SRV_C) && !defined(MBEDTLS_SSL_NO_SESSION_CACHE)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008162void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008163 void *p_cache,
8164 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8165 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008166{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008167 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008168 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008169 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008170}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008171#endif /* MBEDTLS_SSL_SRV_C && !MBEDTLS_SSL_NO_SESSION_CACHE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008172
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008173#if defined(MBEDTLS_SSL_CLI_C) && !defined(MBEDTLS_SSL_NO_SESSION_RESUMPTION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008175{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008176 int ret;
8177
8178 if( ssl == NULL ||
8179 session == NULL ||
8180 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008181 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008184 }
8185
Hanno Becker58fccf22019-02-06 14:30:46 +00008186 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8187 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008188 return( ret );
8189
Paul Bakker0a597072012-09-25 21:55:46 +00008190 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008191
8192 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008193}
Jarno Lamsa29f2dd02019-06-20 15:31:52 +03008194#endif /* MBEDTLS_SSL_CLI_C && !MBEDTLS_SSL_NO_SESSION_RESUMPTION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008195
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008196void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008197 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008198{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008199 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8200 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8201 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8202 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008203}
8204
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008205void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008206 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008207 int major, int minor )
8208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008210 return;
8211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008213 return;
8214
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008215 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008216}
8217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008218#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008219void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008220 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008221{
8222 conf->cert_profile = profile;
8223}
8224
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008225/* Append a new keycert entry to a (possibly empty) list */
8226static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8227 mbedtls_x509_crt *cert,
8228 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008229{
niisato8ee24222018-06-25 19:05:48 +09008230 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008231
niisato8ee24222018-06-25 19:05:48 +09008232 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8233 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008234 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008235
niisato8ee24222018-06-25 19:05:48 +09008236 new_cert->cert = cert;
8237 new_cert->key = key;
8238 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008240 /* Update head is the list was null, else add to the end */
8241 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008242 {
niisato8ee24222018-06-25 19:05:48 +09008243 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008244 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008245 else
8246 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008247 mbedtls_ssl_key_cert *cur = *head;
8248 while( cur->next != NULL )
8249 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008250 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008251 }
8252
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008253 return( 0 );
8254}
8255
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008256int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008257 mbedtls_x509_crt *own_cert,
8258 mbedtls_pk_context *pk_key )
8259{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008260 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008261}
8262
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008263void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008264 mbedtls_x509_crt *ca_chain,
8265 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008266{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008267 conf->ca_chain = ca_chain;
8268 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008270#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008271
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008272#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8273int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8274 mbedtls_x509_crt *own_cert,
8275 mbedtls_pk_context *pk_key )
8276{
8277 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8278 own_cert, pk_key ) );
8279}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008280
8281void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8282 mbedtls_x509_crt *ca_chain,
8283 mbedtls_x509_crl *ca_crl )
8284{
8285 ssl->handshake->sni_ca_chain = ca_chain;
8286 ssl->handshake->sni_ca_crl = ca_crl;
8287}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008288
8289void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8290 int authmode )
8291{
8292 ssl->handshake->sni_authmode = authmode;
8293}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008294#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8295
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008296#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008297/*
8298 * Set EC J-PAKE password for current handshake
8299 */
8300int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8301 const unsigned char *pw,
8302 size_t pw_len )
8303{
8304 mbedtls_ecjpake_role role;
8305
Janos Follath8eb64132016-06-03 15:40:57 +01008306 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8308
8309 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8310 role = MBEDTLS_ECJPAKE_SERVER;
8311 else
8312 role = MBEDTLS_ECJPAKE_CLIENT;
8313
8314 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8315 role,
8316 MBEDTLS_MD_SHA256,
8317 MBEDTLS_ECP_DP_SECP256R1,
8318 pw, pw_len ) );
8319}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008320#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008323int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008324 const unsigned char *psk, size_t psk_len,
8325 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008326{
Paul Bakker6db455e2013-09-18 17:29:31 +02008327 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008330 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008332
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008333 /* Identity len will be encoded on two bytes */
8334 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008335 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008336 {
8337 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8338 }
8339
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008340 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008341 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008342 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008343
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008344 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008345 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008346 conf->psk_len = 0;
8347 }
8348 if( conf->psk_identity != NULL )
8349 {
8350 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008351 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008352 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008353 }
8354
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008355 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8356 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008357 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008358 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008359 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008360 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008361 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008362 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008363 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008364
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008365 conf->psk_len = psk_len;
8366 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008367
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008368 memcpy( conf->psk, psk, conf->psk_len );
8369 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008370
8371 return( 0 );
8372}
8373
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008374int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8375 const unsigned char *psk, size_t psk_len )
8376{
8377 if( psk == NULL || ssl->handshake == NULL )
8378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8379
8380 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8381 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8382
8383 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008384 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008385 mbedtls_platform_zeroize( ssl->handshake->psk,
8386 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008387 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008388 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008389 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008390
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008391 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008393
8394 ssl->handshake->psk_len = psk_len;
8395 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8396
8397 return( 0 );
8398}
8399
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008400void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008402 size_t),
8403 void *p_psk )
8404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->f_psk = f_psk;
8406 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008407}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008409
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008410#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008411
8412#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008413int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008414{
8415 int ret;
8416
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008417 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8418 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8419 {
8420 mbedtls_mpi_free( &conf->dhm_P );
8421 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008422 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008424
8425 return( 0 );
8426}
Hanno Becker470a8c42017-10-04 15:28:46 +01008427#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008428
Hanno Beckera90658f2017-10-04 15:29:08 +01008429int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8430 const unsigned char *dhm_P, size_t P_len,
8431 const unsigned char *dhm_G, size_t G_len )
8432{
8433 int ret;
8434
8435 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8436 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8437 {
8438 mbedtls_mpi_free( &conf->dhm_P );
8439 mbedtls_mpi_free( &conf->dhm_G );
8440 return( ret );
8441 }
8442
8443 return( 0 );
8444}
Paul Bakker5121ce52009-01-03 21:22:43 +00008445
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008446int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008447{
8448 int ret;
8449
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008450 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8451 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8452 {
8453 mbedtls_mpi_free( &conf->dhm_P );
8454 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008455 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008456 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008457
8458 return( 0 );
8459}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008460#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008461
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008462#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8463/*
8464 * Set the minimum length for Diffie-Hellman parameters
8465 */
8466void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8467 unsigned int bitlen )
8468{
8469 conf->dhm_min_bitlen = bitlen;
8470}
8471#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8472
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008473#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008474/*
8475 * Set allowed/preferred hashes for handshake signatures
8476 */
8477void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8478 const int *hashes )
8479{
8480 conf->sig_hashes = hashes;
8481}
Hanno Becker947194e2017-04-07 13:25:49 +01008482#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008483
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008484#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008485/*
8486 * Set the allowed elliptic curves
8487 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008488void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008489 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008492}
Hanno Becker947194e2017-04-07 13:25:49 +01008493#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008494
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008495#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008496int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008497{
Hanno Becker947194e2017-04-07 13:25:49 +01008498 /* Initialize to suppress unnecessary compiler warning */
8499 size_t hostname_len = 0;
8500
8501 /* Check if new hostname is valid before
8502 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008503 if( hostname != NULL )
8504 {
8505 hostname_len = strlen( hostname );
8506
8507 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8508 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8509 }
8510
8511 /* Now it's clear that we will overwrite the old hostname,
8512 * so we can free it safely */
8513
8514 if( ssl->hostname != NULL )
8515 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008516 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008517 mbedtls_free( ssl->hostname );
8518 }
8519
8520 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008521
Paul Bakker5121ce52009-01-03 21:22:43 +00008522 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008523 {
8524 ssl->hostname = NULL;
8525 }
8526 else
8527 {
8528 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008529 if( ssl->hostname == NULL )
8530 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008531
Hanno Becker947194e2017-04-07 13:25:49 +01008532 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008533
Hanno Becker947194e2017-04-07 13:25:49 +01008534 ssl->hostname[hostname_len] = '\0';
8535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008536
8537 return( 0 );
8538}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008539#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008540
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008541#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008542void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008544 const unsigned char *, size_t),
8545 void *p_sni )
8546{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008547 conf->f_sni = f_sni;
8548 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008550#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008553int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008554{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008555 size_t cur_len, tot_len;
8556 const char **p;
8557
8558 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008559 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8560 * MUST NOT be truncated."
8561 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008562 */
8563 tot_len = 0;
8564 for( p = protos; *p != NULL; p++ )
8565 {
8566 cur_len = strlen( *p );
8567 tot_len += cur_len;
8568
8569 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008571 }
8572
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008573 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008574
8575 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008576}
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008579{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008580 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008582#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008583
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008584void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008586 conf->max_major_ver = major;
8587 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008588}
8589
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008590void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008591{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008592 conf->min_major_ver = major;
8593 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008594}
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008597void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008598{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008599 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008600}
8601#endif
8602
Janos Follath088ce432017-04-10 12:42:31 +01008603#if defined(MBEDTLS_SSL_SRV_C)
8604void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8605 char cert_req_ca_list )
8606{
8607 conf->cert_req_ca_list = cert_req_ca_list;
8608}
8609#endif
8610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008611#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008612void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008613{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008614 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008615}
8616#endif
8617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008619#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008620void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008622 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008623}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008624#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8625#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008626void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008627 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008628{
8629 conf->enforce_extended_master_secret = ems_enf;
8630}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008631#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008632#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008633
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008634#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008635void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008636{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008637 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008638}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008639#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008642int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008645 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008648 }
8649
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008650 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008651
8652 return( 0 );
8653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008656#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008657void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008659 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008664void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008665{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008666 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008667}
8668#endif
8669
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008670void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008672 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008673}
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008676void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008677{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008679}
8680
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008681void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008683 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008684}
8685
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008686void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008687 const unsigned char period[8] )
8688{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008689 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008691#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008694#if defined(MBEDTLS_SSL_CLI_C)
8695void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008696{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008697 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008698}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008699#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008700
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008701#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008702void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8703 mbedtls_ssl_ticket_write_t *f_ticket_write,
8704 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8705 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008706{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008707 conf->f_ticket_write = f_ticket_write;
8708 conf->f_ticket_parse = f_ticket_parse;
8709 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008710}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008711#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008712#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008713
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008714#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8715void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8716 mbedtls_ssl_export_keys_t *f_export_keys,
8717 void *p_export_keys )
8718{
8719 conf->f_export_keys = f_export_keys;
8720 conf->p_export_keys = p_export_keys;
8721}
8722#endif
8723
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008724#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008725void mbedtls_ssl_conf_async_private_cb(
8726 mbedtls_ssl_config *conf,
8727 mbedtls_ssl_async_sign_t *f_async_sign,
8728 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8729 mbedtls_ssl_async_resume_t *f_async_resume,
8730 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008731 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008732{
8733 conf->f_async_sign_start = f_async_sign;
8734 conf->f_async_decrypt_start = f_async_decrypt;
8735 conf->f_async_resume = f_async_resume;
8736 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008737 conf->p_async_config_data = async_config_data;
8738}
8739
Gilles Peskine8f97af72018-04-26 11:46:10 +02008740void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8741{
8742 return( conf->p_async_config_data );
8743}
8744
Gilles Peskine1febfef2018-04-30 11:54:39 +02008745void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008746{
8747 if( ssl->handshake == NULL )
8748 return( NULL );
8749 else
8750 return( ssl->handshake->user_async_ctx );
8751}
8752
Gilles Peskine1febfef2018-04-30 11:54:39 +02008753void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008754 void *ctx )
8755{
8756 if( ssl->handshake != NULL )
8757 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008758}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008759#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008760
Paul Bakker5121ce52009-01-03 21:22:43 +00008761/*
8762 * SSL get accessors
8763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008764size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008765{
8766 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8767}
8768
Hanno Becker8b170a02017-10-10 11:51:19 +01008769int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8770{
8771 /*
8772 * Case A: We're currently holding back
8773 * a message for further processing.
8774 */
8775
8776 if( ssl->keep_current_message == 1 )
8777 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008778 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008779 return( 1 );
8780 }
8781
8782 /*
8783 * Case B: Further records are pending in the current datagram.
8784 */
8785
8786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008787 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008788 ssl->in_left > ssl->next_record_offset )
8789 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008790 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008791 return( 1 );
8792 }
8793#endif /* MBEDTLS_SSL_PROTO_DTLS */
8794
8795 /*
8796 * Case C: A handshake message is being processed.
8797 */
8798
Hanno Becker8b170a02017-10-10 11:51:19 +01008799 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8800 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008801 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008802 return( 1 );
8803 }
8804
8805 /*
8806 * Case D: An application data message is being processed
8807 */
8808 if( ssl->in_offt != NULL )
8809 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008811 return( 1 );
8812 }
8813
8814 /*
8815 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008816 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008817 * we implement support for multiple alerts in single records.
8818 */
8819
8820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8821 return( 0 );
8822}
8823
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008824uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008825{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008826 if( ssl->session != NULL )
8827 return( ssl->session->verify_result );
8828
8829 if( ssl->session_negotiate != NULL )
8830 return( ssl->session_negotiate->verify_result );
8831
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008832 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008833}
8834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008835const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008836{
Paul Bakker926c8e42013-03-06 10:23:34 +01008837 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008838 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008841}
8842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008846 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008847 {
8848 switch( ssl->minor_ver )
8849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008851 return( "DTLSv1.0" );
8852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008854 return( "DTLSv1.2" );
8855
8856 default:
8857 return( "unknown (DTLS)" );
8858 }
8859 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008860 MBEDTLS_SSL_TRANSPORT_ELSE
8861#endif /* MBEDTLS_SSL_PROTO_DTLS */
8862#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008863 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008864 switch( ssl->minor_ver )
8865 {
8866 case MBEDTLS_SSL_MINOR_VERSION_0:
8867 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008868
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008869 case MBEDTLS_SSL_MINOR_VERSION_1:
8870 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008871
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008872 case MBEDTLS_SSL_MINOR_VERSION_2:
8873 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008874
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008875 case MBEDTLS_SSL_MINOR_VERSION_3:
8876 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008877
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008878 default:
8879 return( "unknown" );
8880 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008881 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008882#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008883}
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008886{
Hanno Becker3136ede2018-08-17 15:28:19 +01008887 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008888 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008889 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008890
Hanno Becker43395762019-05-03 14:46:38 +01008891 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8892
Hanno Becker78640902018-08-13 16:35:15 +01008893 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008894 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008896#if defined(MBEDTLS_ZLIB_SUPPORT)
8897 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8898 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008899#endif
8900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008901 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 case MBEDTLS_MODE_GCM:
8904 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008905 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008907 transform_expansion = transform->minlen;
8908 break;
8909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008910 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008911
8912 block_size = mbedtls_cipher_get_block_size(
8913 &transform->cipher_ctx_enc );
8914
Hanno Becker3136ede2018-08-17 15:28:19 +01008915 /* Expansion due to the addition of the MAC. */
8916 transform_expansion += transform->maclen;
8917
8918 /* Expansion due to the addition of CBC padding;
8919 * Theoretically up to 256 bytes, but we never use
8920 * more than the block size of the underlying cipher. */
8921 transform_expansion += block_size;
8922
8923 /* For TLS 1.1 or higher, an explicit IV is added
8924 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008925#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8926 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008927 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008928#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008929
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008930 break;
8931
8932 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008935 }
8936
Hanno Beckera5a2b082019-05-15 14:03:01 +01008937#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008938 if( transform->out_cid_len != 0 )
8939 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008940#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008941
Hanno Becker43395762019-05-03 14:46:38 +01008942 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008943}
8944
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008945#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8946size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8947{
8948 size_t max_len;
8949
8950 /*
8951 * Assume mfl_code is correct since it was checked when set
8952 */
Angus Grattond8213d02016-05-25 20:56:48 +10008953 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008954
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008955 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008956 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008957 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008958 {
Angus Grattond8213d02016-05-25 20:56:48 +10008959 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008960 }
8961
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008962 /* During a handshake, use the value being negotiated */
8963 if( ssl->session_negotiate != NULL &&
8964 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8965 {
8966 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8967 }
8968
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008969 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008970}
8971#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8972
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008973#if defined(MBEDTLS_SSL_PROTO_DTLS)
8974static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8975{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008976 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8977 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8978 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8979 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8980 return ( 0 );
8981
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008982 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8983 return( ssl->mtu );
8984
8985 if( ssl->mtu == 0 )
8986 return( ssl->handshake->mtu );
8987
8988 return( ssl->mtu < ssl->handshake->mtu ?
8989 ssl->mtu : ssl->handshake->mtu );
8990}
8991#endif /* MBEDTLS_SSL_PROTO_DTLS */
8992
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008993int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8994{
8995 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8996
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008997#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8998 !defined(MBEDTLS_SSL_PROTO_DTLS)
8999 (void) ssl;
9000#endif
9001
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009002#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9003 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9004
9005 if( max_len > mfl )
9006 max_len = mfl;
9007#endif
9008
9009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009010 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009011 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009012 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009013 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9014 const size_t overhead = (size_t) ret;
9015
9016 if( ret < 0 )
9017 return( ret );
9018
9019 if( mtu <= overhead )
9020 {
9021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9022 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9023 }
9024
9025 if( max_len > mtu - overhead )
9026 max_len = mtu - overhead;
9027 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009028#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009029
Hanno Becker0defedb2018-08-10 12:35:02 +01009030#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9031 !defined(MBEDTLS_SSL_PROTO_DTLS)
9032 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009033#endif
9034
9035 return( (int) max_len );
9036}
9037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#if defined(MBEDTLS_X509_CRT_PARSE_C)
9039const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009040{
9041 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009042 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009043
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009044#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009045 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009046#else
9047 return( NULL );
9048#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009052#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009053int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9054 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009055{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009056 if( ssl == NULL ||
9057 dst == NULL ||
9058 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009059 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009062 }
9063
Hanno Becker58fccf22019-02-06 14:30:46 +00009064 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009067
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009068const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9069{
9070 if( ssl == NULL )
9071 return( NULL );
9072
9073 return( ssl->session );
9074}
9075
Paul Bakker5121ce52009-01-03 21:22:43 +00009076/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009077 * Define ticket header determining Mbed TLS version
9078 * and structure of the ticket.
9079 */
9080
Hanno Becker41527622019-05-16 12:50:45 +01009081/*
Hanno Becker26829e92019-05-28 14:30:45 +01009082 * Define bitflag determining compile-time settings influencing
9083 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009084 */
9085
Hanno Becker26829e92019-05-28 14:30:45 +01009086#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009087#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009088#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009089#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009090#endif /* MBEDTLS_HAVE_TIME */
9091
9092#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009093#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009094#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009095#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009096#endif /* MBEDTLS_X509_CRT_PARSE_C */
9097
9098#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009099#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009100#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009101#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009102#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9103
9104#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009105#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009106#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009107#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009108#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9109
9110#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009111#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009112#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009113#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009114#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9115
9116#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009117#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009118#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009119#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009120#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9121
Hanno Becker41527622019-05-16 12:50:45 +01009122#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9123#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9124#else
9125#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9126#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9127
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009128#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9129#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9130#else
9131#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9132#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9133
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009134#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9135#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9136#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9137#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9138#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9139#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9140#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009141#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009142
Hanno Becker26829e92019-05-28 14:30:45 +01009143#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009144 ( (uint16_t) ( \
9145 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9146 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9147 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9148 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9149 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9150 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009151 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9152 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009153
Hanno Becker557fe9f2019-05-16 12:41:07 +01009154static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009155 MBEDTLS_VERSION_MAJOR,
9156 MBEDTLS_VERSION_MINOR,
9157 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009158 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9159 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009160};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009161
9162/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009163 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009164 * (in the presentation language of TLS, RFC 8446 section 3)
9165 *
Hanno Becker26829e92019-05-28 14:30:45 +01009166 * opaque mbedtls_version[3]; // major, minor, patch
9167 * opaque session_format[2]; // version-specific 16-bit field determining
9168 * // the format of the remaining
9169 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009170 *
9171 * Note: When updating the format, remember to keep
9172 * these version+format bytes.
9173 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009174 * // In this version, `session_format` determines
9175 * // the setting of those compile-time
9176 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009177 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009178 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009179 * uint8 ciphersuite[2]; // defined by the standard
9180 * uint8 compression; // 0 or 1
9181 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009182 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009183 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009184 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009185 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9186 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9187 * case disabled: uint8_t peer_cert_digest_type;
9188 * opaque peer_cert_digest<0..2^8-1>;
9189 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009190 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009191 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009192 * uint8 mfl_code; // up to 255 according to standard
9193 * uint8 trunc_hmac; // 0 or 1
9194 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009195 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009196 * The order is the same as in the definition of the structure, except
9197 * verify_result is put before peer_cert so that all mandatory fields come
9198 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009199 */
9200int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9201 unsigned char *buf,
9202 size_t buf_len,
9203 size_t *olen )
9204{
9205 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009206 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009207#if defined(MBEDTLS_HAVE_TIME)
9208 uint64_t start;
9209#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009210#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009211#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009212 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009213#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009214#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009215
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009216 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009217 * Add version identifier
9218 */
9219
9220 used += sizeof( ssl_serialized_session_header );
9221
9222 if( used <= buf_len )
9223 {
9224 memcpy( p, ssl_serialized_session_header,
9225 sizeof( ssl_serialized_session_header ) );
9226 p += sizeof( ssl_serialized_session_header );
9227 }
9228
9229 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009230 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009231 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009232#if defined(MBEDTLS_HAVE_TIME)
9233 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009234
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009235 if( used <= buf_len )
9236 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009237 start = (uint64_t) session->start;
9238
9239 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9240 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9241 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9242 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9243 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9244 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9245 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9246 *p++ = (unsigned char)( ( start ) & 0xFF );
9247 }
9248#endif /* MBEDTLS_HAVE_TIME */
9249
9250 /*
9251 * Basic mandatory fields
9252 */
9253 used += 2 /* ciphersuite */
9254 + 1 /* compression */
9255 + 1 /* id_len */
9256 + sizeof( session->id )
9257 + sizeof( session->master )
9258 + 4; /* verify_result */
9259
9260 if( used <= buf_len )
9261 {
9262 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9263 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9264
9265 *p++ = (unsigned char)( session->compression & 0xFF );
9266
9267 *p++ = (unsigned char)( session->id_len & 0xFF );
9268 memcpy( p, session->id, 32 );
9269 p += 32;
9270
9271 memcpy( p, session->master, 48 );
9272 p += 48;
9273
9274 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9275 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9276 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9277 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009278 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009279
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009280 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009281 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009282 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009283#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009284#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009285 if( session->peer_cert == NULL )
9286 cert_len = 0;
9287 else
9288 cert_len = session->peer_cert->raw.len;
9289
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009290 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009291
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009292 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009293 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009294 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9295 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9296 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9297
9298 if( session->peer_cert != NULL )
9299 {
9300 memcpy( p, session->peer_cert->raw.p, cert_len );
9301 p += cert_len;
9302 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009303 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009304
Hanno Becker5882dd02019-06-06 16:25:57 +01009305#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009306 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009307 if( session->peer_cert_digest != NULL )
9308 {
9309 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9310 if( used <= buf_len )
9311 {
9312 *p++ = (unsigned char) session->peer_cert_digest_type;
9313 *p++ = (unsigned char) session->peer_cert_digest_len;
9314 memcpy( p, session->peer_cert_digest,
9315 session->peer_cert_digest_len );
9316 p += session->peer_cert_digest_len;
9317 }
9318 }
9319 else
9320 {
9321 used += 2;
9322 if( used <= buf_len )
9323 {
9324 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9325 *p++ = 0;
9326 }
9327 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009328#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009329#endif /* MBEDTLS_X509_CRT_PARSE_C */
9330
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009331 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009332 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009333 */
9334#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009335 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009336
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009337 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009338 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009339 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9340 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9341 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9342
9343 if( session->ticket != NULL )
9344 {
9345 memcpy( p, session->ticket, session->ticket_len );
9346 p += session->ticket_len;
9347 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009348
9349 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9350 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9351 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9352 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009353 }
9354#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9355
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009356 /*
9357 * Misc extension-related info
9358 */
9359#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9360 used += 1;
9361
9362 if( used <= buf_len )
9363 *p++ = session->mfl_code;
9364#endif
9365
9366#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9367 used += 1;
9368
9369 if( used <= buf_len )
9370 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9371#endif
9372
9373#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9374 used += 1;
9375
9376 if( used <= buf_len )
9377 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9378#endif
9379
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009380 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009381 *olen = used;
9382
9383 if( used > buf_len )
9384 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009385
9386 return( 0 );
9387}
9388
9389/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009390 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009391 *
9392 * This internal version is wrapped by a public function that cleans up in
9393 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009394 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009395static int ssl_session_load( mbedtls_ssl_session *session,
9396 const unsigned char *buf,
9397 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009398{
9399 const unsigned char *p = buf;
9400 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009401#if defined(MBEDTLS_HAVE_TIME)
9402 uint64_t start;
9403#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009405#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009406 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009407#endif
9408#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009409
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009410 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009411 * Check version identifier
9412 */
9413
9414 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009415 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009416
9417 if( memcmp( p, ssl_serialized_session_header,
9418 sizeof( ssl_serialized_session_header ) ) != 0 )
9419 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009420 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009421 }
9422 p += sizeof( ssl_serialized_session_header );
9423
9424 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009425 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009426 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009427#if defined(MBEDTLS_HAVE_TIME)
9428 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9430
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009431 start = ( (uint64_t) p[0] << 56 ) |
9432 ( (uint64_t) p[1] << 48 ) |
9433 ( (uint64_t) p[2] << 40 ) |
9434 ( (uint64_t) p[3] << 32 ) |
9435 ( (uint64_t) p[4] << 24 ) |
9436 ( (uint64_t) p[5] << 16 ) |
9437 ( (uint64_t) p[6] << 8 ) |
9438 ( (uint64_t) p[7] );
9439 p += 8;
9440
9441 session->start = (time_t) start;
9442#endif /* MBEDTLS_HAVE_TIME */
9443
9444 /*
9445 * Basic mandatory fields
9446 */
9447 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9449
9450 session->ciphersuite = ( p[0] << 8 ) | p[1];
9451 p += 2;
9452
9453 session->compression = *p++;
9454
9455 session->id_len = *p++;
9456 memcpy( session->id, p, 32 );
9457 p += 32;
9458
9459 memcpy( session->master, p, 48 );
9460 p += 48;
9461
9462 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9463 ( (uint32_t) p[1] << 16 ) |
9464 ( (uint32_t) p[2] << 8 ) |
9465 ( (uint32_t) p[3] );
9466 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009467
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009468 /* Immediately clear invalid pointer values that have been read, in case
9469 * we exit early before we replaced them with valid ones. */
9470#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009471#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009472 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009473#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009474 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009475#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009476#endif
9477#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9478 session->ticket = NULL;
9479#endif
9480
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009481 /*
9482 * Peer certificate
9483 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009484#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009485#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009486 if( 3 > (size_t)( end - p ) )
9487 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9488
9489 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9490 p += 3;
9491
9492 if( cert_len == 0 )
9493 {
9494 session->peer_cert = NULL;
9495 }
9496 else
9497 {
9498 int ret;
9499
9500 if( cert_len > (size_t)( end - p ) )
9501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9502
9503 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9504
9505 if( session->peer_cert == NULL )
9506 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9507
9508 mbedtls_x509_crt_init( session->peer_cert );
9509
9510 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9511 p, cert_len ) ) != 0 )
9512 {
9513 mbedtls_x509_crt_free( session->peer_cert );
9514 mbedtls_free( session->peer_cert );
9515 session->peer_cert = NULL;
9516 return( ret );
9517 }
9518
9519 p += cert_len;
9520 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009521#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009522 /* Deserialize CRT digest from the end of the ticket. */
9523 if( 2 > (size_t)( end - p ) )
9524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9525
9526 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9527 session->peer_cert_digest_len = (size_t) *p++;
9528
9529 if( session->peer_cert_digest_len != 0 )
9530 {
Hanno Becker2326d202019-06-06 14:54:55 +01009531 const mbedtls_md_info_t *md_info =
9532 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9533 if( md_info == NULL )
9534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9535 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9537
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009538 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9539 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9540
9541 session->peer_cert_digest =
9542 mbedtls_calloc( 1, session->peer_cert_digest_len );
9543 if( session->peer_cert_digest == NULL )
9544 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9545
9546 memcpy( session->peer_cert_digest, p,
9547 session->peer_cert_digest_len );
9548 p += session->peer_cert_digest_len;
9549 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009550#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009551#endif /* MBEDTLS_X509_CRT_PARSE_C */
9552
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009553 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009554 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009555 */
9556#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9557 if( 3 > (size_t)( end - p ) )
9558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9559
9560 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9561 p += 3;
9562
9563 if( session->ticket_len != 0 )
9564 {
9565 if( session->ticket_len > (size_t)( end - p ) )
9566 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9567
9568 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9569 if( session->ticket == NULL )
9570 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9571
9572 memcpy( session->ticket, p, session->ticket_len );
9573 p += session->ticket_len;
9574 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009575
9576 if( 4 > (size_t)( end - p ) )
9577 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9578
9579 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9580 ( (uint32_t) p[1] << 16 ) |
9581 ( (uint32_t) p[2] << 8 ) |
9582 ( (uint32_t) p[3] );
9583 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009584#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9585
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009586 /*
9587 * Misc extension-related info
9588 */
9589#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9590 if( 1 > (size_t)( end - p ) )
9591 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9592
9593 session->mfl_code = *p++;
9594#endif
9595
9596#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9597 if( 1 > (size_t)( end - p ) )
9598 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9599
9600 session->trunc_hmac = *p++;
9601#endif
9602
9603#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9604 if( 1 > (size_t)( end - p ) )
9605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9606
9607 session->encrypt_then_mac = *p++;
9608#endif
9609
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009610 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009611 if( p != end )
9612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9613
9614 return( 0 );
9615}
9616
9617/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009618 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009619 */
9620int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9621 const unsigned char *buf,
9622 size_t len )
9623{
9624 int ret = ssl_session_load( session, buf, len );
9625
9626 if( ret != 0 )
9627 mbedtls_ssl_session_free( session );
9628
9629 return( ret );
9630}
9631
9632/*
Paul Bakker1961b702013-01-25 14:49:24 +01009633 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009638
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009639 if( ssl == NULL || ssl->conf == NULL )
9640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009643 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009645#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009647 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009649#endif
9650
Paul Bakker1961b702013-01-25 14:49:24 +01009651 return( ret );
9652}
9653
9654/*
9655 * Perform the SSL handshake
9656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009658{
9659 int ret = 0;
9660
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009661 if( ssl == NULL || ssl->conf == NULL )
9662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009669
9670 if( ret != 0 )
9671 break;
9672 }
9673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009675
9676 return( ret );
9677}
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#if defined(MBEDTLS_SSL_RENEGOTIATION)
9680#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009681/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009682 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009685{
9686 int ret;
9687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009689
9690 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9692 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009693
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009694 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009695 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009697 return( ret );
9698 }
9699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009701
9702 return( 0 );
9703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009705
9706/*
9707 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 * - any side: calling mbedtls_ssl_renegotiate(),
9709 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9710 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009711 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009712 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009715static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009716{
9717 int ret;
9718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009720
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009721 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9722 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009723
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009724 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9725 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009727 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009729 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009730 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009731 ssl->handshake->out_msg_seq = 1;
9732 else
9733 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009734 }
9735#endif
9736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9738 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009743 return( ret );
9744 }
9745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009747
9748 return( 0 );
9749}
9750
9751/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009752 * Renegotiate current connection on client,
9753 * or request renegotiation on server
9754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009756{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009757 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009758
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009759 if( ssl == NULL || ssl->conf == NULL )
9760 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009763 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009764 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9767 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009770
9771 /* Did we already try/start sending HelloRequest? */
9772 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009774
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009775 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009776 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009780 /*
9781 * On client, either start the renegotiation process or,
9782 * if already in progress, continue the handshake
9783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009788
9789 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009792 return( ret );
9793 }
9794 }
9795 else
9796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009800 return( ret );
9801 }
9802 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009804
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009805 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009806}
9807
9808/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009809 * Check record counters and renegotiate if they're above the limit.
9810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009812{
Andres AG2196c7f2016-12-15 17:01:16 +00009813 size_t ep_len = ssl_ep_len( ssl );
9814 int in_ctr_cmp;
9815 int out_ctr_cmp;
9816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9818 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009819 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009820 {
9821 return( 0 );
9822 }
9823
Andres AG2196c7f2016-12-15 17:01:16 +00009824 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9825 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009826 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009827 ssl->conf->renego_period + ep_len, 8 - ep_len );
9828
9829 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009830 {
9831 return( 0 );
9832 }
9833
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009838
9839/*
9840 * Receive application data decrypted from the SSL layer
9841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009843{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009844 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009845 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009846
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009847 if( ssl == NULL || ssl->conf == NULL )
9848 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009853 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009856 return( ret );
9857
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009858 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009860 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009861 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009862 return( ret );
9863 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009864 }
9865#endif
9866
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867 /*
9868 * Check if renegotiation is necessary and/or handshake is
9869 * in process. If yes, perform/continue, and fall through
9870 * if an unexpected packet is received while the client
9871 * is waiting for the ServerHello.
9872 *
9873 * (There is no equivalent to the last condition on
9874 * the server-side as it is not treated as within
9875 * a handshake while waiting for the ClientHello
9876 * after a renegotiation request.)
9877 */
9878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009879#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009880 ret = ssl_check_ctr_renegotiate( ssl );
9881 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9882 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009884 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009885 return( ret );
9886 }
9887#endif
9888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009892 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9893 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009896 return( ret );
9897 }
9898 }
9899
Hanno Beckere41158b2017-10-23 13:30:32 +01009900 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009901 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009902 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009903 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009904 if( ssl->f_get_timer != NULL &&
9905 ssl->f_get_timer( ssl->p_timer ) == -1 )
9906 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009907 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009908 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009909
Hanno Becker327c93b2018-08-15 13:56:18 +01009910 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009911 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009912 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9913 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009914
Hanno Becker4a810fb2017-05-24 16:27:30 +01009915 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9916 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009917 }
9918
9919 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009921 {
9922 /*
9923 * OpenSSL sends empty messages to randomize the IV
9924 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009925 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009928 return( 0 );
9929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009931 return( ret );
9932 }
9933 }
9934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009938
Hanno Becker4a810fb2017-05-24 16:27:30 +01009939 /*
9940 * - For client-side, expect SERVER_HELLO_REQUEST.
9941 * - For server-side, expect CLIENT_HELLO.
9942 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9943 */
9944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009946 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009948 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009951
9952 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009954 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009955 {
9956 continue;
9957 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009958 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009959#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009960#if defined(MBEDTLS_SSL_PROTO_TLS)
9961 {
9962 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9963 }
9964#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009965 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009966#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009967
Hanno Becker4a810fb2017-05-24 16:27:30 +01009968#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009969 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009973
9974 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009976 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009977 {
9978 continue;
9979 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009980 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009981#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009982#if defined(MBEDTLS_SSL_PROTO_TLS)
9983 {
9984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9985 }
9986#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009987 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009988#endif /* MBEDTLS_SSL_SRV_C */
9989
Hanno Becker21df7f92017-10-17 11:03:26 +01009990#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009991 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009992 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9993 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9994 ssl->conf->allow_legacy_renegotiation ==
9995 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9996 {
9997 /*
9998 * Accept renegotiation request
9999 */
Paul Bakker48916f92012-09-16 19:57:18 +000010000
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010001 /* DTLS clients need to know renego is server-initiated */
10002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010003 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010004 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10005 {
10006 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10007 }
10008#endif
10009 ret = ssl_start_renegotiation( ssl );
10010 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10011 ret != 0 )
10012 {
10013 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10014 return( ret );
10015 }
10016 }
10017 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010018#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010019 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010020 /*
10021 * Refuse renegotiation
10022 */
10023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026#if defined(MBEDTLS_SSL_PROTO_SSL3)
10027 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010028 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010029 /* SSLv3 does not have a "no_renegotiation" warning, so
10030 we send a fatal alert and abort the connection. */
10031 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10032 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10033 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010034 }
10035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10039 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10042 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10043 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010044 {
10045 return( ret );
10046 }
Paul Bakker48916f92012-09-16 19:57:18 +000010047 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010048 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010049#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10050 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010054 }
Paul Bakker48916f92012-09-16 19:57:18 +000010055 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010056
Hanno Becker90333da2017-10-10 11:27:13 +010010057 /* At this point, we don't know whether the renegotiation has been
10058 * completed or not. The cases to consider are the following:
10059 * 1) The renegotiation is complete. In this case, no new record
10060 * has been read yet.
10061 * 2) The renegotiation is incomplete because the client received
10062 * an application data record while awaiting the ServerHello.
10063 * 3) The renegotiation is incomplete because the client received
10064 * a non-handshake, non-application data message while awaiting
10065 * the ServerHello.
10066 * In each of these case, looping will be the proper action:
10067 * - For 1), the next iteration will read a new record and check
10068 * if it's application data.
10069 * - For 2), the loop condition isn't satisfied as application data
10070 * is present, hence continue is the same as break
10071 * - For 3), the loop condition is satisfied and read_record
10072 * will re-deliver the message that was held back by the client
10073 * when expecting the ServerHello.
10074 */
10075 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010076 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010077#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010079 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010080 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010081 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010082 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010085 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010087 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010088 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10093 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010096 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010097 }
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10102 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010103 }
10104
10105 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010106
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010107 /* We're going to return something now, cancel timer,
10108 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010110 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010111
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010113 /* If we requested renego but received AppData, resend HelloRequest.
10114 * Do it now, after setting in_offt, to avoid taking this branch
10115 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010117 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010119 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010120 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010123 return( ret );
10124 }
10125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010127#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010128 }
10129
10130 n = ( len < ssl->in_msglen )
10131 ? len : ssl->in_msglen;
10132
10133 memcpy( buf, ssl->in_offt, n );
10134 ssl->in_msglen -= n;
10135
10136 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010137 {
10138 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010139 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010140 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010141 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010142 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010143 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010144 /* more data available */
10145 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010146 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010149
Paul Bakker23986e52011-04-24 08:57:21 +000010150 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010151}
10152
10153/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010154 * Send application data to be encrypted by the SSL layer, taking care of max
10155 * fragment length and buffer size.
10156 *
10157 * According to RFC 5246 Section 6.2.1:
10158 *
10159 * Zero-length fragments of Application data MAY be sent as they are
10160 * potentially useful as a traffic analysis countermeasure.
10161 *
10162 * Therefore, it is possible that the input message length is 0 and the
10163 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010164 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010165static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010166 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010167{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010168 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10169 const size_t max_len = (size_t) ret;
10170
10171 if( ret < 0 )
10172 {
10173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10174 return( ret );
10175 }
10176
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010177 if( len > max_len )
10178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010180 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010183 "maximum fragment length: %d > %d",
10184 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010186 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010187 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010188#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010189#if defined(MBEDTLS_SSL_PROTO_TLS)
10190 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010191 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010192 }
10193#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010194 }
Paul Bakker887bd502011-06-08 13:10:54 +000010195
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 if( ssl->out_left != 0 )
10197 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010198 /*
10199 * The user has previously tried to send the data and
10200 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10201 * written. In this case, we expect the high-level write function
10202 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 return( ret );
10208 }
10209 }
Paul Bakker887bd502011-06-08 13:10:54 +000010210 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010211 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010212 /*
10213 * The user is trying to send a message the first time, so we need to
10214 * copy the data into the internal buffers and setup the data structure
10215 * to keep track of partial writes
10216 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010217 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010219 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010220
Hanno Becker67bc7c32018-08-06 11:33:50 +010010221 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010224 return( ret );
10225 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010226 }
10227
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010228 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010229}
10230
10231/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010232 * Write application data, doing 1/n-1 splitting if necessary.
10233 *
10234 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010235 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010236 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010239static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010240 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010241{
10242 int ret;
10243
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010244 if( ssl->conf->cbc_record_splitting ==
10245 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010246 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10248 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10249 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010250 {
10251 return( ssl_write_real( ssl, buf, len ) );
10252 }
10253
10254 if( ssl->split_done == 0 )
10255 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010256 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010257 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010258 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010259 }
10260
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010261 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10262 return( ret );
10263 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010264
10265 return( ret + 1 );
10266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010268
10269/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010270 * Write application data (public-facing wrapper)
10271 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010272int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010273{
10274 int ret;
10275
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010277
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010278 if( ssl == NULL || ssl->conf == NULL )
10279 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10280
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010281#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010282 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10283 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010284 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010285 return( ret );
10286 }
10287#endif
10288
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010289 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010290 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010291 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010292 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010294 return( ret );
10295 }
10296 }
10297
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010298#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010299 ret = ssl_write_split( ssl, buf, len );
10300#else
10301 ret = ssl_write_real( ssl, buf, len );
10302#endif
10303
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010305
10306 return( ret );
10307}
10308
10309/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010310 * Notify the peer that the connection is being closed
10311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010313{
10314 int ret;
10315
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010316 if( ssl == NULL || ssl->conf == NULL )
10317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010320
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010321 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10327 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10328 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010331 return( ret );
10332 }
10333 }
10334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010336
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010337 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010338}
10339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010341{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010342 if( transform == NULL )
10343 return;
10344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010346 deflateEnd( &transform->ctx_deflate );
10347 inflateEnd( &transform->ctx_inflate );
10348#endif
10349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10351 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010352
Hanno Becker92231322018-01-03 15:32:51 +000010353#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 mbedtls_md_free( &transform->md_ctx_enc );
10355 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010356#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010358 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010359}
10360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361#if defined(MBEDTLS_X509_CRT_PARSE_C)
10362static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010363{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010365
10366 while( cur != NULL )
10367 {
10368 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010370 cur = next;
10371 }
10372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010374
Hanno Becker0271f962018-08-16 13:23:47 +010010375#if defined(MBEDTLS_SSL_PROTO_DTLS)
10376
10377static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10378{
10379 unsigned offset;
10380 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10381
10382 if( hs == NULL )
10383 return;
10384
Hanno Becker283f5ef2018-08-24 09:34:47 +010010385 ssl_free_buffered_record( ssl );
10386
Hanno Becker0271f962018-08-16 13:23:47 +010010387 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010388 ssl_buffering_free_slot( ssl, offset );
10389}
10390
10391static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10392 uint8_t slot )
10393{
10394 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10395 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010396
10397 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10398 return;
10399
Hanno Beckere605b192018-08-21 15:59:07 +010010400 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010401 {
Hanno Beckere605b192018-08-21 15:59:07 +010010402 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010403 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010404 mbedtls_free( hs_buf->data );
10405 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010406 }
10407}
10408
10409#endif /* MBEDTLS_SSL_PROTO_DTLS */
10410
Gilles Peskine9b562d52018-04-25 20:32:43 +020010411void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010412{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010413 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10414
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010415 if( handshake == NULL )
10416 return;
10417
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010418#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10419 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10420 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010421 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010422 handshake->async_in_progress = 0;
10423 }
10424#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10425
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010426#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10427 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10428 mbedtls_md5_free( &handshake->fin_md5 );
10429 mbedtls_sha1_free( &handshake->fin_sha1 );
10430#endif
10431#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10432#if defined(MBEDTLS_SHA256_C)
10433 mbedtls_sha256_free( &handshake->fin_sha256 );
10434#endif
10435#if defined(MBEDTLS_SHA512_C)
10436 mbedtls_sha512_free( &handshake->fin_sha512 );
10437#endif
10438#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440#if defined(MBEDTLS_DHM_C)
10441 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443#if defined(MBEDTLS_ECDH_C)
10444 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010445#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010446#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010447 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010448#if defined(MBEDTLS_SSL_CLI_C)
10449 mbedtls_free( handshake->ecjpake_cache );
10450 handshake->ecjpake_cache = NULL;
10451 handshake->ecjpake_cache_len = 0;
10452#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010453#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010454
Janos Follath4ae5c292016-02-10 11:27:43 +000010455#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10456 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010457 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010459#endif
10460
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010461#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10462 if( handshake->psk != NULL )
10463 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010464 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010465 mbedtls_free( handshake->psk );
10466 }
10467#endif
10468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10470 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010471 /*
10472 * Free only the linked list wrapper, not the keys themselves
10473 * since the belong to the SNI callback
10474 */
10475 if( handshake->sni_key_cert != NULL )
10476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010478
10479 while( cur != NULL )
10480 {
10481 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010483 cur = next;
10484 }
10485 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010487
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010488#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010489 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010490 if( handshake->ecrs_peer_cert != NULL )
10491 {
10492 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10493 mbedtls_free( handshake->ecrs_peer_cert );
10494 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010495#endif
10496
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010497#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10498 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10499 mbedtls_pk_free( &handshake->peer_pubkey );
10500#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010502#if defined(MBEDTLS_SSL_PROTO_DTLS)
10503 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010504 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010505 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010506#endif
10507
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010508 mbedtls_platform_zeroize( handshake,
10509 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010510}
10511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010513{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010514 if( session == NULL )
10515 return;
10516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010517#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010518 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010519#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010520
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010521#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010523#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010524
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010525 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010526}
10527
Paul Bakker5121ce52009-01-03 21:22:43 +000010528/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010529 * Serialize a full SSL context
10530 */
10531int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10532 unsigned char *buf,
10533 size_t buf_len,
10534 size_t *olen )
10535{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010536 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010537 (void) ssl;
10538
10539 if( buf != NULL )
10540 memset( buf, 0, buf_len );
10541
10542 *olen = 0;
10543
10544 return( 0 );
10545}
10546
10547/*
10548 * Deserialize a full SSL context
10549 */
10550int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10551 const unsigned char *buf,
10552 size_t len )
10553{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010554 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010555 (void) ssl;
10556 (void) buf;
10557 (void) len;
10558
10559 return( 0 );
10560}
10561
10562/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010563 * Free an SSL context
10564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010566{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010567 if( ssl == NULL )
10568 return;
10569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010571
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010572 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010573 {
Angus Grattond8213d02016-05-25 20:56:48 +100010574 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010576 }
10577
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010578 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010579 {
Angus Grattond8213d02016-05-25 20:56:48 +100010580 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010582 }
10583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010584#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010585 if( ssl->compress_buf != NULL )
10586 {
Angus Grattond8213d02016-05-25 20:56:48 +100010587 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010589 }
10590#endif
10591
Paul Bakker48916f92012-09-16 19:57:18 +000010592 if( ssl->transform )
10593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010594 mbedtls_ssl_transform_free( ssl->transform );
10595 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010596 }
10597
10598 if( ssl->handshake )
10599 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010600 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10602 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 mbedtls_free( ssl->handshake );
10605 mbedtls_free( ssl->transform_negotiate );
10606 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010607 }
10608
Paul Bakkerc0463502013-02-14 11:19:38 +010010609 if( ssl->session )
10610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611 mbedtls_ssl_session_free( ssl->session );
10612 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010613 }
10614
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010615#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010616 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010617 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010618 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010619 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010620 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10624 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10627 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010628 }
10629#endif
10630
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010631#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010633#endif
10634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010636
Paul Bakker86f04f42013-02-14 11:20:09 +010010637 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010638 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010639}
10640
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010641/*
10642 * Initialze mbedtls_ssl_config
10643 */
10644void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10645{
10646 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010647
10648#if !defined(MBEDTLS_SSL_PROTO_TLS)
10649 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10650#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010651}
10652
Simon Butcherc97b6972015-12-27 23:48:17 +000010653#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010654static int ssl_preset_default_hashes[] = {
10655#if defined(MBEDTLS_SHA512_C)
10656 MBEDTLS_MD_SHA512,
10657 MBEDTLS_MD_SHA384,
10658#endif
10659#if defined(MBEDTLS_SHA256_C)
10660 MBEDTLS_MD_SHA256,
10661 MBEDTLS_MD_SHA224,
10662#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010663#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010664 MBEDTLS_MD_SHA1,
10665#endif
10666 MBEDTLS_MD_NONE
10667};
Simon Butcherc97b6972015-12-27 23:48:17 +000010668#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010669
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010670static int ssl_preset_suiteb_ciphersuites[] = {
10671 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10672 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10673 0
10674};
10675
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010676#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010677static int ssl_preset_suiteb_hashes[] = {
10678 MBEDTLS_MD_SHA256,
10679 MBEDTLS_MD_SHA384,
10680 MBEDTLS_MD_NONE
10681};
10682#endif
10683
10684#if defined(MBEDTLS_ECP_C)
10685static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10686 MBEDTLS_ECP_DP_SECP256R1,
10687 MBEDTLS_ECP_DP_SECP384R1,
10688 MBEDTLS_ECP_DP_NONE
10689};
10690#endif
10691
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010692/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010693 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010694 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010695int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010696 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010697{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010698#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010699 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010700#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010701
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010702 /* Use the functions here so that they are covered in tests,
10703 * but otherwise access member directly for efficiency */
10704 mbedtls_ssl_conf_endpoint( conf, endpoint );
10705 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010706
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010707 /*
10708 * Things that are common to all presets
10709 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010710#if defined(MBEDTLS_SSL_CLI_C)
10711 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10712 {
10713 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10714#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10715 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10716#endif
10717 }
10718#endif
10719
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010720#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010722#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723
10724#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10725 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10726#endif
10727
10728#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010729#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010731#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10732#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010733 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010734 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010735#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010736#endif
10737
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010738#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10739 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10740#endif
10741
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010742#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010743 conf->f_cookie_write = ssl_cookie_write_dummy;
10744 conf->f_cookie_check = ssl_cookie_check_dummy;
10745#endif
10746
10747#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10748 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10749#endif
10750
Janos Follath088ce432017-04-10 12:42:31 +010010751#if defined(MBEDTLS_SSL_SRV_C)
10752 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10753#endif
10754
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010755#if defined(MBEDTLS_SSL_PROTO_DTLS)
10756 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10757 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10758#endif
10759
10760#if defined(MBEDTLS_SSL_RENEGOTIATION)
10761 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010762 memset( conf->renego_period, 0x00, 2 );
10763 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010764#endif
10765
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010766#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10767 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10768 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010769 const unsigned char dhm_p[] =
10770 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10771 const unsigned char dhm_g[] =
10772 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10773
Hanno Beckera90658f2017-10-04 15:29:08 +010010774 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10775 dhm_p, sizeof( dhm_p ),
10776 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010777 {
10778 return( ret );
10779 }
10780 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010781#endif
10782
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010783 /*
10784 * Preset-specific defaults
10785 */
10786 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010787 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010788 /*
10789 * NSA Suite B
10790 */
10791 case MBEDTLS_SSL_PRESET_SUITEB:
10792 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10793 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10794 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10795 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10796
10797 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10798 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10799 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10800 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10801 ssl_preset_suiteb_ciphersuites;
10802
10803#if defined(MBEDTLS_X509_CRT_PARSE_C)
10804 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010805#endif
10806
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010807#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010808 conf->sig_hashes = ssl_preset_suiteb_hashes;
10809#endif
10810
10811#if defined(MBEDTLS_ECP_C)
10812 conf->curve_list = ssl_preset_suiteb_curves;
10813#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010814 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010815
10816 /*
10817 * Default
10818 */
10819 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010820 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10821 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10822 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10823 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10824 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10825 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10826 MBEDTLS_SSL_MIN_MINOR_VERSION :
10827 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010828 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10829 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10830
10831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010832 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010833 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10834#endif
10835
10836 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10837 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10838 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10839 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10840 mbedtls_ssl_list_ciphersuites();
10841
10842#if defined(MBEDTLS_X509_CRT_PARSE_C)
10843 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10844#endif
10845
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010846#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010847 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010848#endif
10849
10850#if defined(MBEDTLS_ECP_C)
10851 conf->curve_list = mbedtls_ecp_grp_id_list();
10852#endif
10853
10854#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10855 conf->dhm_min_bitlen = 1024;
10856#endif
10857 }
10858
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010859 return( 0 );
10860}
10861
10862/*
10863 * Free mbedtls_ssl_config
10864 */
10865void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10866{
10867#if defined(MBEDTLS_DHM_C)
10868 mbedtls_mpi_free( &conf->dhm_P );
10869 mbedtls_mpi_free( &conf->dhm_G );
10870#endif
10871
10872#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10873 if( conf->psk != NULL )
10874 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010875 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010876 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010877 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010878 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010879 }
10880
10881 if( conf->psk_identity != NULL )
10882 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010883 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010884 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010885 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010886 conf->psk_identity_len = 0;
10887 }
10888#endif
10889
10890#if defined(MBEDTLS_X509_CRT_PARSE_C)
10891 ssl_key_cert_free( conf->key_cert );
10892#endif
10893
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010894 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010895}
10896
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010897#if defined(MBEDTLS_PK_C) && \
10898 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010899/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010904#if defined(MBEDTLS_RSA_C)
10905 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10906 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908#if defined(MBEDTLS_ECDSA_C)
10909 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10910 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010913}
10914
Hanno Becker7e5437a2017-04-28 17:15:26 +010010915unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10916{
10917 switch( type ) {
10918 case MBEDTLS_PK_RSA:
10919 return( MBEDTLS_SSL_SIG_RSA );
10920 case MBEDTLS_PK_ECDSA:
10921 case MBEDTLS_PK_ECKEY:
10922 return( MBEDTLS_SSL_SIG_ECDSA );
10923 default:
10924 return( MBEDTLS_SSL_SIG_ANON );
10925 }
10926}
10927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010929{
10930 switch( sig )
10931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932#if defined(MBEDTLS_RSA_C)
10933 case MBEDTLS_SSL_SIG_RSA:
10934 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010935#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936#if defined(MBEDTLS_ECDSA_C)
10937 case MBEDTLS_SSL_SIG_ECDSA:
10938 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010939#endif
10940 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010941 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010942 }
10943}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010944#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010945
Hanno Becker7e5437a2017-04-28 17:15:26 +010010946#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10947 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10948
10949/* Find an entry in a signature-hash set matching a given hash algorithm. */
10950mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10951 mbedtls_pk_type_t sig_alg )
10952{
10953 switch( sig_alg )
10954 {
10955 case MBEDTLS_PK_RSA:
10956 return( set->rsa );
10957 case MBEDTLS_PK_ECDSA:
10958 return( set->ecdsa );
10959 default:
10960 return( MBEDTLS_MD_NONE );
10961 }
10962}
10963
10964/* Add a signature-hash-pair to a signature-hash set */
10965void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10966 mbedtls_pk_type_t sig_alg,
10967 mbedtls_md_type_t md_alg )
10968{
10969 switch( sig_alg )
10970 {
10971 case MBEDTLS_PK_RSA:
10972 if( set->rsa == MBEDTLS_MD_NONE )
10973 set->rsa = md_alg;
10974 break;
10975
10976 case MBEDTLS_PK_ECDSA:
10977 if( set->ecdsa == MBEDTLS_MD_NONE )
10978 set->ecdsa = md_alg;
10979 break;
10980
10981 default:
10982 break;
10983 }
10984}
10985
10986/* Allow exactly one hash algorithm for each signature. */
10987void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10988 mbedtls_md_type_t md_alg )
10989{
10990 set->rsa = md_alg;
10991 set->ecdsa = md_alg;
10992}
10993
10994#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10995 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10996
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010997/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010998 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010999 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011000mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011001{
11002 switch( hash )
11003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004#if defined(MBEDTLS_MD5_C)
11005 case MBEDTLS_SSL_HASH_MD5:
11006 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008#if defined(MBEDTLS_SHA1_C)
11009 case MBEDTLS_SSL_HASH_SHA1:
11010 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_SHA256_C)
11013 case MBEDTLS_SSL_HASH_SHA224:
11014 return( MBEDTLS_MD_SHA224 );
11015 case MBEDTLS_SSL_HASH_SHA256:
11016 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011017#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011018#if defined(MBEDTLS_SHA512_C)
11019 case MBEDTLS_SSL_HASH_SHA384:
11020 return( MBEDTLS_MD_SHA384 );
11021 case MBEDTLS_SSL_HASH_SHA512:
11022 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011023#endif
11024 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011026 }
11027}
11028
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011029/*
11030 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11031 */
11032unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11033{
11034 switch( md )
11035 {
11036#if defined(MBEDTLS_MD5_C)
11037 case MBEDTLS_MD_MD5:
11038 return( MBEDTLS_SSL_HASH_MD5 );
11039#endif
11040#if defined(MBEDTLS_SHA1_C)
11041 case MBEDTLS_MD_SHA1:
11042 return( MBEDTLS_SSL_HASH_SHA1 );
11043#endif
11044#if defined(MBEDTLS_SHA256_C)
11045 case MBEDTLS_MD_SHA224:
11046 return( MBEDTLS_SSL_HASH_SHA224 );
11047 case MBEDTLS_MD_SHA256:
11048 return( MBEDTLS_SSL_HASH_SHA256 );
11049#endif
11050#if defined(MBEDTLS_SHA512_C)
11051 case MBEDTLS_MD_SHA384:
11052 return( MBEDTLS_SSL_HASH_SHA384 );
11053 case MBEDTLS_MD_SHA512:
11054 return( MBEDTLS_SSL_HASH_SHA512 );
11055#endif
11056 default:
11057 return( MBEDTLS_SSL_HASH_NONE );
11058 }
11059}
11060
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011061#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011062/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011063 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011064 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011065 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011066int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011069
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011070 if( ssl->conf->curve_list == NULL )
11071 return( -1 );
11072
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011073 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011074 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011075 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011076
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011077 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011078}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011079#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011080
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011081#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011082/*
11083 * Check if a hash proposed by the peer is in our list.
11084 * Return 0 if we're willing to use it, -1 otherwise.
11085 */
11086int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11087 mbedtls_md_type_t md )
11088{
11089 const int *cur;
11090
11091 if( ssl->conf->sig_hashes == NULL )
11092 return( -1 );
11093
11094 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11095 if( *cur == (int) md )
11096 return( 0 );
11097
11098 return( -1 );
11099}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011100#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102#if defined(MBEDTLS_X509_CRT_PARSE_C)
11103int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11104 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011105 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011106 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011107{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011108 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011109#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011110 int usage = 0;
11111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011112#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011113 const char *ext_oid;
11114 size_t ext_len;
11115#endif
11116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011117#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11118 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011119 ((void) cert);
11120 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011121 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011122#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11125 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011126 {
11127 /* Server part of the key exchange */
11128 switch( ciphersuite->key_exchange )
11129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011130 case MBEDTLS_KEY_EXCHANGE_RSA:
11131 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011132 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011133 break;
11134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11136 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11137 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11138 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011139 break;
11140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11142 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011143 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011144 break;
11145
11146 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011147 case MBEDTLS_KEY_EXCHANGE_NONE:
11148 case MBEDTLS_KEY_EXCHANGE_PSK:
11149 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11150 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011151 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011152 usage = 0;
11153 }
11154 }
11155 else
11156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011157 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11158 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011159 }
11160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011161 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011162 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011163 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011164 ret = -1;
11165 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011166#else
11167 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11171 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011173 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11174 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011175 }
11176 else
11177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011178 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11179 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011180 }
11181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011183 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011184 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011185 ret = -1;
11186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011187#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011188
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011189 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011192
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011193/*
11194 * Convert version numbers to/from wire format
11195 * and, for DTLS, to/from TLS equivalent.
11196 *
11197 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011198 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011199 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11200 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011202void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011203 unsigned char ver[2] )
11204{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011205#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11206 ((void) transport);
11207#endif
11208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011210 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011213 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11214
11215 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11216 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11217 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011218 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011219#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011220#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011221 {
11222 ver[0] = (unsigned char) major;
11223 ver[1] = (unsigned char) minor;
11224 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011225#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011226}
11227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011229 const unsigned char ver[2] )
11230{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011231#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11232 ((void) transport);
11233#endif
11234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011236 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011237 {
11238 *major = 255 - ver[0] + 2;
11239 *minor = 255 - ver[1] + 1;
11240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011241 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011242 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11243 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011244 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011246#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011247 {
11248 *major = ver[0];
11249 *minor = ver[1];
11250 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011251#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011252}
11253
Simon Butcher99000142016-10-13 17:21:01 +010011254int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11255{
11256#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11257 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11258 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11259
11260 switch( md )
11261 {
11262#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11263#if defined(MBEDTLS_MD5_C)
11264 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011265 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011266#endif
11267#if defined(MBEDTLS_SHA1_C)
11268 case MBEDTLS_SSL_HASH_SHA1:
11269 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11270 break;
11271#endif
11272#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11273#if defined(MBEDTLS_SHA512_C)
11274 case MBEDTLS_SSL_HASH_SHA384:
11275 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11276 break;
11277#endif
11278#if defined(MBEDTLS_SHA256_C)
11279 case MBEDTLS_SSL_HASH_SHA256:
11280 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11281 break;
11282#endif
11283 default:
11284 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11285 }
11286
11287 return 0;
11288#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11289 (void) ssl;
11290 (void) md;
11291
11292 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11293#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11294}
11295
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11297 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11298int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11299 unsigned char *output,
11300 unsigned char *data, size_t data_len )
11301{
11302 int ret = 0;
11303 mbedtls_md5_context mbedtls_md5;
11304 mbedtls_sha1_context mbedtls_sha1;
11305
11306 mbedtls_md5_init( &mbedtls_md5 );
11307 mbedtls_sha1_init( &mbedtls_sha1 );
11308
11309 /*
11310 * digitally-signed struct {
11311 * opaque md5_hash[16];
11312 * opaque sha_hash[20];
11313 * };
11314 *
11315 * md5_hash
11316 * MD5(ClientHello.random + ServerHello.random
11317 * + ServerParams);
11318 * sha_hash
11319 * SHA(ClientHello.random + ServerHello.random
11320 * + ServerParams);
11321 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011322 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011323 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011325 goto exit;
11326 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011327 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011328 ssl->handshake->randbytes, 64 ) ) != 0 )
11329 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 goto exit;
11332 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011333 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011334 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011336 goto exit;
11337 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011338 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011339 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011341 goto exit;
11342 }
11343
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011344 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 goto exit;
11348 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011349 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011350 ssl->handshake->randbytes, 64 ) ) != 0 )
11351 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011353 goto exit;
11354 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 data_len ) ) != 0 )
11357 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 goto exit;
11360 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 output + 16 ) ) != 0 )
11363 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 goto exit;
11366 }
11367
11368exit:
11369 mbedtls_md5_free( &mbedtls_md5 );
11370 mbedtls_sha1_free( &mbedtls_sha1 );
11371
11372 if( ret != 0 )
11373 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11374 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11375
11376 return( ret );
11377
11378}
11379#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11380 MBEDTLS_SSL_PROTO_TLS1_1 */
11381
11382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11384int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011385 unsigned char *hash, size_t *hashlen,
11386 unsigned char *data, size_t data_len,
11387 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011388{
11389 int ret = 0;
11390 mbedtls_md_context_t ctx;
11391 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011392 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393
11394 mbedtls_md_init( &ctx );
11395
11396 /*
11397 * digitally-signed struct {
11398 * opaque client_random[32];
11399 * opaque server_random[32];
11400 * ServerDHParams params;
11401 * };
11402 */
11403 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11404 {
11405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11406 goto exit;
11407 }
11408 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11409 {
11410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11411 goto exit;
11412 }
11413 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11414 {
11415 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11416 goto exit;
11417 }
11418 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11419 {
11420 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11421 goto exit;
11422 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011423 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424 {
11425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11426 goto exit;
11427 }
11428
11429exit:
11430 mbedtls_md_free( &ctx );
11431
11432 if( ret != 0 )
11433 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11434 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11435
11436 return( ret );
11437}
11438#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11439 MBEDTLS_SSL_PROTO_TLS1_2 */
11440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011441#endif /* MBEDTLS_SSL_TLS_C */