blob: 2fa48b6c0837d1415ee9a88c6636a0f7ca628d6d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 if( src->peer_cert != NULL )
379 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200380 int ret;
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200389 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 dst->peer_cert = NULL;
393 return( ret );
394 }
395 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000396
397#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
398 if( src->peer_cert_digest != NULL )
399 {
400 dst->peer_cert_digest_len = src->peer_cert_digest_len;
401 dst->peer_cert_digest =
402 mbedtls_calloc( 1, dst->peer_cert_digest_len );
403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
409 }
410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200415 if( src->ticket != NULL )
416 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200417 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 memcpy( dst->ticket, src->ticket, src->ticket_len );
422 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 return( 0 );
426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
429int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200430 const unsigned char *key_enc, const unsigned char *key_dec,
431 size_t keylen,
432 const unsigned char *iv_enc, const unsigned char *iv_dec,
433 size_t ivlen,
434 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200435 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
437int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
438int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Key material generation
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200447static int ssl3_prf( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char *dstbuf, size_t dlen )
451{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100452 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_context md5;
455 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 unsigned char padding[16];
457 unsigned char sha1sum[20];
458 ((void)label);
459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200460 mbedtls_md5_init( &md5 );
461 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200462
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 /*
464 * SSLv3:
465 * block =
466 * MD5( secret + SHA1( 'A' + secret + random ) ) +
467 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
468 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
469 * ...
470 */
471 for( i = 0; i < dlen / 16; i++ )
472 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200473 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000494 }
495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200497 mbedtls_md5_free( &md5 );
498 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500500 mbedtls_platform_zeroize( padding, sizeof( padding ) );
501 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int tls1_prf( const unsigned char *secret, size_t slen,
509 const char *label,
510 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t nb, hs;
514 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 unsigned char tmp[128];
517 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 const mbedtls_md_info_t *md_info;
519 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100520 int ret;
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 hs = ( slen + 1 ) / 2;
528 S1 = secret;
529 S2 = secret + slen - hs;
530
531 nb = strlen( label );
532 memcpy( tmp + 20, label, nb );
533 memcpy( tmp + 20 + nb, random, rlen );
534 nb += rlen;
535
536 /*
537 * First compute P_md5(secret,label+random)[0..dlen]
538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
540 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 return( ret );
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
546 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 for( i = 0; i < dlen; i += 16 )
550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_md_hmac_reset ( &md_ctx );
552 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
553 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_hmac_reset ( &md_ctx );
556 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
557 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
560
561 for( j = 0; j < k; j++ )
562 dstbuf[i + j] = h_i[j];
563 }
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 /*
568 * XOR out with P_sha1(secret,label+random)[0..dlen]
569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100574 return( ret );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
577 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
578 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580 for( i = 0; i < dlen; i += 20 )
581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
584 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_md_hmac_reset ( &md_ctx );
587 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
588 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
591
592 for( j = 0; j < k; j++ )
593 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
594 }
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500598 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
599 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 return( 0 );
602}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
606static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100607 const unsigned char *secret, size_t slen,
608 const char *label,
609 const unsigned char *random, size_t rlen,
610 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000611{
612 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100613 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
616 const mbedtls_md_info_t *md_info;
617 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618 int ret;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626
627 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000629
630 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100631 memcpy( tmp + md_len, label, nb );
632 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 nb += rlen;
634
635 /*
636 * Compute P_<hash>(secret, label + random)[0..dlen]
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100639 return( ret );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
642 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
643 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100644
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_md_hmac_reset ( &md_ctx );
648 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
649 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_md_hmac_reset ( &md_ctx );
652 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
653 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000654
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656
657 for( j = 0; j < k; j++ )
658 dstbuf[i + j] = h_i[j];
659 }
660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100662
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500663 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
664 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000665
666 return( 0 );
667}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100670static int tls_prf_sha256( const unsigned char *secret, size_t slen,
671 const char *label,
672 const unsigned char *random, size_t rlen,
673 unsigned char *dstbuf, size_t dlen )
674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100676 label, random, rlen, dstbuf, dlen ) );
677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200681static int tls_prf_sha384( const unsigned char *secret, size_t slen,
682 const char *label,
683 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 unsigned char *dstbuf, size_t dlen )
685{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000688}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#endif /* MBEDTLS_SHA512_C */
690#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
695 defined(MBEDTLS_SSL_PROTO_TLS1_1)
696static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200697#endif
Paul Bakker380da532012-04-18 16:10:25 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200700static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200702#endif
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200705static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#endif
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
710#if defined(MBEDTLS_SHA256_C)
711static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200712static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SHA512_C)
717static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200718static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200723/* Type for the TLS PRF */
724typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
725 const unsigned char *, size_t,
726 unsigned char *, size_t);
727
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200728/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200729 * Populate a transform structure with session keys and all the other
730 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200731 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200732 * Parameters:
733 * - [in/out]: transform: structure to populate
734 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200735 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 * - [in] ciphersuite
737 * - [in] master
738 * - [in] encrypt_then_mac
739 * - [in] trunc_hmac
740 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200741 * - [in] tls_prf: pointer to PRF to use for key derivation
742 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200743 * - [in] minor_ver: SSL/TLS minor version
744 * - [in] endpoint: client or server
745 * - [in] ssl: optionally used for:
746 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
747 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
748 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200749 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200750static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200751 int ciphersuite,
752 const unsigned char master[48],
753#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
754 int encrypt_then_mac,
755#endif
756#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
757 int trunc_hmac,
758#endif
759#if defined(MBEDTLS_ZLIB_SUPPORT)
760 int compression,
761#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200762 ssl_tls_prf_t tls_prf,
763 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200764 int minor_ver,
765 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200766 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200768 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769 unsigned char keyblk[256];
770 unsigned char *key1;
771 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100772 unsigned char *mac_enc;
773 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000774 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200775 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000776 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 const mbedtls_cipher_info_t *cipher_info;
779 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100780
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200781#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
782 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
783 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200784 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200785 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000786#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000787
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200788 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200790 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000791#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200792 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200794 /*
795 * Get various info structures
796 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200797 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 if( ciphersuite_info == NULL )
799 {
800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
803 }
804
Hanno Becker8759e162017-12-27 21:34:08 +0000805 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100806 if( cipher_info == NULL )
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000809 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100811 }
812
Hanno Becker8759e162017-12-27 21:34:08 +0000813 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100814 if( md_info == NULL )
815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000817 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100819 }
820
Hanno Beckera5a2b082019-05-15 14:03:01 +0100821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822 /* Copy own and peer's CID if the use of the CID
823 * extension has been negotiated. */
824 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
825 {
826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100827
Hanno Becker4932f9f2019-05-03 15:23:51 +0100828 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100830 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100831 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100832
833 transform->out_cid_len = ssl->handshake->peer_cid_len;
834 memcpy( transform->out_cid, ssl->handshake->peer_cid,
835 ssl->handshake->peer_cid_len );
836 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
837 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100838 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200842 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000843 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200844 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100845 if( ret != 0 )
846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100848 return( ret );
849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200852 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200853 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200854 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 /*
858 * Determine the appropriate key, IV and MAC length.
859 */
Paul Bakker68884e32013-01-07 18:20:04 +0100860
Hanno Beckere7f2df02017-12-27 08:17:40 +0000861 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200862
Hanno Beckerf1229442018-01-03 15:32:31 +0000863#if defined(MBEDTLS_GCM_C) || \
864 defined(MBEDTLS_CCM_C) || \
865 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 cipher_info->mode == MBEDTLS_MODE_CCM ||
868 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 {
Hanno Becker8759e162017-12-27 21:34:08 +0000870 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200872 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000873 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->taglen =
875 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200877 /* All modes haves 96-bit IVs;
878 * GCM and CCM has 4 implicit and 8 explicit bytes
879 * ChachaPoly has all 12 bytes implicit
880 */
Paul Bakker68884e32013-01-07 18:20:04 +0100881 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200882 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
883 transform->fixed_ivlen = 12;
884 else
885 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200886
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200887 /* Minimum length of encrypted record */
888 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000889 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100890 }
891 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000892#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
893#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
894 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
895 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100896 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200897 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
899 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100903 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200905 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000906 mac_key_len = mbedtls_md_get_size( md_info );
907 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200910 /*
911 * If HMAC is to be truncated, we shall keep the leftmost bytes,
912 * (rfc 6066 page 13 or rfc 2104 section 4),
913 * so we only need to adjust the length here.
914 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200915 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000918
919#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
920 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000921 * HMAC implementation which also truncates the key
922 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000923 mac_key_len = transform->maclen;
924#endif
925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927
928 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100929 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200931 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200933 transform->minlen = transform->maclen;
934 else
Paul Bakker68884e32013-01-07 18:20:04 +0100935 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200936 /*
937 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100938 * 1. if EtM is in use: one block plus MAC
939 * otherwise: * first multiple of blocklen greater than maclen
940 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200943 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100944 {
945 transform->minlen = transform->maclen
946 + cipher_info->block_size;
947 }
948 else
949#endif
950 {
951 transform->minlen = transform->maclen
952 + cipher_info->block_size
953 - transform->maclen % cipher_info->block_size;
954 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200957 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
958 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100960 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200961#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200963 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
964 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965 {
966 transform->minlen += transform->ivlen;
967 }
968 else
969#endif
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200973 }
Paul Bakker68884e32013-01-07 18:20:04 +0100974 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000976 else
977#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
978 {
979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Hanno Beckere7f2df02017-12-27 08:17:40 +0000983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
984 (unsigned) keylen,
985 (unsigned) transform->minlen,
986 (unsigned) transform->ivlen,
987 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000988
989 /*
990 * Finally setup the cipher contexts, IVs and MAC secrets.
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200993 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000995 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000996 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000997
Paul Bakker68884e32013-01-07 18:20:04 +0100998 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001001 /*
1002 * This is not used in TLS v1.1.
1003 */
Paul Bakker48916f92012-09-16 19:57:18 +00001004 iv_copy_len = ( transform->fixed_ivlen ) ?
1005 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001006 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1007 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 }
1010 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_SSL_CLI_C */
1012#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001013 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001015 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001016 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001017
Hanno Becker81c7b182017-11-09 18:39:33 +00001018 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001019 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001021 /*
1022 * This is not used in TLS v1.1.
1023 */
Paul Bakker48916f92012-09-16 19:57:18 +00001024 iv_copy_len = ( transform->fixed_ivlen ) ?
1025 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001026 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1027 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Hanno Becker92231322018-01-03 15:32:51 +00001037#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Hanno Becker92231322018-01-03 15:32:51 +00001041 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1044 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001045 }
1046
Hanno Becker81c7b182017-11-09 18:39:33 +00001047 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1048 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001049 }
1050 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1052#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1053 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001054 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001055 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001056 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1057 For AEAD-based ciphersuites, there is nothing to do here. */
1058 if( mac_key_len != 0 )
1059 {
1060 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1061 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1062 }
Paul Bakker68884e32013-01-07 18:20:04 +01001063 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001064 else
1065#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1068 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001069 }
Hanno Becker92231322018-01-03 15:32:51 +00001070#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1073 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001074 {
1075 int ret = 0;
1076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Hanno Beckere7f2df02017-12-27 08:17:40 +00001079 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001080 transform->iv_enc, transform->iv_dec,
1081 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001082 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001083 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1086 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001087 }
1088 }
Hanno Becker92231322018-01-03 15:32:51 +00001089#else
1090 ((void) mac_dec);
1091 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001093
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001094#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1095 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001096 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001097 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001098 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001099 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 iv_copy_len );
1101 }
1102#endif
1103
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001104 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001106 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001110
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 cipher_info ) ) != 0 )
1113 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115 return( ret );
1116 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001119 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001123 return( ret );
1124 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001127 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 return( ret );
1132 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_CIPHER_MODE_CBC)
1135 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1138 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001142 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1145 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001148 return( ret );
1149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001153 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001155 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001157 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001160
Paul Bakker48916f92012-09-16 19:57:18 +00001161 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1162 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001163
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 if( deflateInit( &transform->ctx_deflate,
1165 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001166 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1169 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001170 }
1171 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 return( 0 );
1175}
1176
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001177/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001178 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001179 *
1180 * Inputs:
1181 * - SSL/TLS minor version
1182 * - hash associated with the ciphersuite (only used by TLS 1.2)
1183 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001184 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001185 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1186 */
1187static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1188 int minor_ver,
1189 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001190{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001191#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1192 (void) hash;
1193#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001196 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001197 {
1198 handshake->tls_prf = ssl3_prf;
1199 handshake->calc_verify = ssl_calc_verify_ssl;
1200 handshake->calc_finished = ssl_calc_finished_ssl;
1201 }
1202 else
1203#endif
1204#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001206 {
1207 handshake->tls_prf = tls1_prf;
1208 handshake->calc_verify = ssl_calc_verify_tls;
1209 handshake->calc_finished = ssl_calc_finished_tls;
1210 }
1211 else
1212#endif
1213#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1214#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001215 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1216 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217 {
1218 handshake->tls_prf = tls_prf_sha384;
1219 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1220 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1221 }
1222 else
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001226 {
1227 handshake->tls_prf = tls_prf_sha256;
1228 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1229 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1230 }
1231 else
1232#endif
1233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1234 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001235 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1236 }
1237
1238 return( 0 );
1239}
1240
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001241/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001242 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001243 *
1244 * Parameters:
1245 * [in/out] handshake
1246 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1247 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001248 * [out] master
1249 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001250 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001251static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001253 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254{
1255 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256
1257#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001258 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001259 (void) ssl;
1260#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001263 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1265 return( 0 );
1266 }
1267
1268 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1269 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001270
1271#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001272 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273 {
1274 unsigned char session_hash[48];
1275 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001277 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001279 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1280 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001281
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001282 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283 "extended master secret",
1284 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001285 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001286 }
1287 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001289 {
1290 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1291 "master secret",
1292 handshake->randbytes, 64,
1293 master, 48 );
1294 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001295 if( ret != 0 )
1296 {
1297 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1298 return( ret );
1299 }
1300
1301 mbedtls_platform_zeroize( handshake->premaster,
1302 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001303
1304 return( 0 );
1305}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001307int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1308{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001310 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1311 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001312
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1314
1315 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001316 ret = ssl_set_handshake_prfs( ssl->handshake,
1317 ssl->minor_ver,
1318 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001319 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001320 {
1321 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001322 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001323 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001325 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001326 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ssl->session_negotiate->master,
1328 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001329 if( ret != 0 )
1330 {
1331 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1332 return( ret );
1333 }
1334
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001335 /* Swap the client and server random values:
1336 * - MS derivation wanted client+server (RFC 5246 8.1)
1337 * - key derivation wants server+client (RFC 5246 6.3) */
1338 {
1339 unsigned char tmp[64];
1340 memcpy( tmp, ssl->handshake->randbytes, 64 );
1341 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1342 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1343 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1344 }
1345
1346 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001347 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001348 ssl->session_negotiate->ciphersuite,
1349 ssl->session_negotiate->master,
1350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1351 ssl->session_negotiate->encrypt_then_mac,
1352#endif
1353#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1354 ssl->session_negotiate->trunc_hmac,
1355#endif
1356#if defined(MBEDTLS_ZLIB_SUPPORT)
1357 ssl->session_negotiate->compression,
1358#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001359 ssl->handshake->tls_prf,
1360 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001361 ssl->minor_ver,
1362 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001363 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001364 if( ret != 0 )
1365 {
1366 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1367 return( ret );
1368 }
1369
1370 /* We no longer need Server/ClientHello.random values */
1371 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1372 sizeof( ssl->handshake->randbytes ) );
1373
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001374 /* Allocate compression buffer */
1375#if defined(MBEDTLS_ZLIB_SUPPORT)
1376 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1377 ssl->compress_buf == NULL )
1378 {
1379 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1380 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1381 if( ssl->compress_buf == NULL )
1382 {
1383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001384 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1386 }
1387 }
1388#endif
1389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1391
1392 return( 0 );
1393}
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001396void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1397 unsigned char hash[36],
1398 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001399{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001400 mbedtls_md5_context md5;
1401 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 unsigned char pad_1[48];
1403 unsigned char pad_2[48];
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001407 mbedtls_md5_init( &md5 );
1408 mbedtls_sha1_init( &sha1 );
1409
1410 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1411 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001412
Paul Bakker380da532012-04-18 16:10:25 +00001413 memset( pad_1, 0x36, 48 );
1414 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001416 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1417 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1418 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001420 mbedtls_md5_starts_ret( &md5 );
1421 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1422 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1423 mbedtls_md5_update_ret( &md5, hash, 16 );
1424 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001426 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1427 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1428 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001430 mbedtls_sha1_starts_ret( &sha1 );
1431 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1432 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1433 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1434 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001435
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001436 *hlen = 36;
1437
1438 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001441 mbedtls_md5_free( &md5 );
1442 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001443
Paul Bakker380da532012-04-18 16:10:25 +00001444 return;
1445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001449void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1450 unsigned char hash[36],
1451 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001452{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001453 mbedtls_md5_context md5;
1454 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001457
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001458 mbedtls_md5_init( &md5 );
1459 mbedtls_sha1_init( &sha1 );
1460
1461 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1462 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001463
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001464 mbedtls_md5_finish_ret( &md5, hash );
1465 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001466
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001467 *hlen = 36;
1468
1469 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001472 mbedtls_md5_free( &md5 );
1473 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001474
Paul Bakker380da532012-04-18 16:10:25 +00001475 return;
1476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1480#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001481void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1482 unsigned char hash[32],
1483 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001484{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001485 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001487 mbedtls_sha256_init( &sha256 );
1488
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001491 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001492 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001494 *hlen = 32;
1495
1496 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001498
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001499 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001500
Paul Bakker380da532012-04-18 16:10:25 +00001501 return;
1502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001506void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1507 unsigned char hash[48],
1508 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001509{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha512_init( &sha512 );
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001516 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001519 *hlen = 48;
1520
1521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001525
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA512_C */
1529#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1532int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001533{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001534 unsigned char *p = ssl->handshake->premaster;
1535 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001536 const unsigned char *psk = ssl->conf->psk;
1537 size_t psk_len = ssl->conf->psk_len;
1538
1539 /* If the psk callback was called, use its result */
1540 if( ssl->handshake->psk != NULL )
1541 {
1542 psk = ssl->handshake->psk;
1543 psk_len = ssl->handshake->psk_len;
1544 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001545
1546 /*
1547 * PMS = struct {
1548 * opaque other_secret<0..2^16-1>;
1549 * opaque psk<0..2^16-1>;
1550 * };
1551 * with "other_secret" depending on the particular key exchange
1552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1554 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001555 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001556 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001558
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001559 *(p++) = (unsigned char)( psk_len >> 8 );
1560 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001561
1562 if( end < p || (size_t)( end - p ) < psk_len )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
1565 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001567 }
1568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1570#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001572 {
1573 /*
1574 * other_secret already set by the ClientKeyExchange message,
1575 * and is 48 bytes long
1576 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001577 if( end - p < 2 )
1578 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1579
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001580 *p++ = 0;
1581 *p++ = 48;
1582 p += 48;
1583 }
1584 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1586#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1587 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001588 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001589 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001590 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001591
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001592 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001594 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001595 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598 return( ret );
1599 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001600 *(p++) = (unsigned char)( len >> 8 );
1601 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 p += len;
1603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 }
1606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1608#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1609 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001610 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001611 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 size_t zlen;
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001615 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001616 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 return( ret );
1620 }
1621
1622 *(p++) = (unsigned char)( zlen >> 8 );
1623 *(p++) = (unsigned char)( zlen );
1624 p += zlen;
1625
Janos Follath3fbdada2018-08-15 10:26:53 +01001626 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1627 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001628 }
1629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635
1636 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001637 if( end - p < 2 )
1638 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001639
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001640 *(p++) = (unsigned char)( psk_len >> 8 );
1641 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001642
1643 if( end < p || (size_t)( end - p ) < psk_len )
1644 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1645
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001646 memcpy( p, psk, psk_len );
1647 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648
1649 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1650
1651 return( 0 );
1652}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656/*
1657 * SSLv3.0 MAC functions
1658 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001659#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001660static void ssl_mac( mbedtls_md_context_t *md_ctx,
1661 const unsigned char *secret,
1662 const unsigned char *buf, size_t len,
1663 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001664 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001665{
1666 unsigned char header[11];
1667 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001668 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1670 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001671
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001672 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001674 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 else
Paul Bakker68884e32013-01-07 18:20:04 +01001676 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678 memcpy( header, ctr, 8 );
1679 header[ 8] = (unsigned char) type;
1680 header[ 9] = (unsigned char)( len >> 8 );
1681 header[10] = (unsigned char)( len );
1682
Paul Bakker68884e32013-01-07 18:20:04 +01001683 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_md_starts( md_ctx );
1685 mbedtls_md_update( md_ctx, secret, md_size );
1686 mbedtls_md_update( md_ctx, padding, padlen );
1687 mbedtls_md_update( md_ctx, header, 11 );
1688 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001689 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Paul Bakker68884e32013-01-07 18:20:04 +01001691 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 mbedtls_md_starts( md_ctx );
1693 mbedtls_md_update( md_ctx, secret, md_size );
1694 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001695 mbedtls_md_update( md_ctx, out, md_size );
1696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001699
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001701 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001703 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1704 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1706/* This function makes sure every byte in the memory region is accessed
1707 * (in ascending addresses order) */
1708static void ssl_read_memory( unsigned char *p, size_t len )
1709{
1710 unsigned char acc = 0;
1711 volatile unsigned char force;
1712
1713 for( ; len != 0; p++, len-- )
1714 acc ^= *p;
1715
1716 force = acc;
1717 (void) force;
1718}
1719#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1720
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001721/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001723 */
Hanno Becker3307b532017-12-27 21:37:21 +00001724
Hanno Beckera5a2b082019-05-15 14:03:01 +01001725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001726/* This functions transforms a DTLS plaintext fragment and a record content
1727 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001728 *
1729 * struct {
1730 * opaque content[DTLSPlaintext.length];
1731 * ContentType real_type;
1732 * uint8 zeros[length_of_padding];
1733 * } DTLSInnerPlaintext;
1734 *
1735 * Input:
1736 * - `content`: The beginning of the buffer holding the
1737 * plaintext to be wrapped.
1738 * - `*content_size`: The length of the plaintext in Bytes.
1739 * - `max_len`: The number of Bytes available starting from
1740 * `content`. This must be `>= *content_size`.
1741 * - `rec_type`: The desired record content type.
1742 *
1743 * Output:
1744 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1745 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1746 *
1747 * Returns:
1748 * - `0` on success.
1749 * - A negative error code if `max_len` didn't offer enough space
1750 * for the expansion.
1751 */
1752static int ssl_cid_build_inner_plaintext( unsigned char *content,
1753 size_t *content_size,
1754 size_t remaining,
1755 uint8_t rec_type )
1756{
1757 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001758 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1759 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1760 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001761
1762 /* Write real content type */
1763 if( remaining == 0 )
1764 return( -1 );
1765 content[ len ] = rec_type;
1766 len++;
1767 remaining--;
1768
1769 if( remaining < pad )
1770 return( -1 );
1771 memset( content + len, 0, pad );
1772 len += pad;
1773 remaining -= pad;
1774
1775 *content_size = len;
1776 return( 0 );
1777}
1778
Hanno Becker7dc25772019-05-20 15:08:01 +01001779/* This function parses a DTLSInnerPlaintext structure.
1780 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001781static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1782 size_t *content_size,
1783 uint8_t *rec_type )
1784{
1785 size_t remaining = *content_size;
1786
1787 /* Determine length of padding by skipping zeroes from the back. */
1788 do
1789 {
1790 if( remaining == 0 )
1791 return( -1 );
1792 remaining--;
1793 } while( content[ remaining ] == 0 );
1794
1795 *content_size = remaining;
1796 *rec_type = content[ remaining ];
1797
1798 return( 0 );
1799}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001800#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001801
Hanno Becker99abf512019-05-20 14:50:53 +01001802/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001803 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001804static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001805 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001806 mbedtls_record *rec )
1807{
Hanno Becker99abf512019-05-20 14:50:53 +01001808 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001809 *
1810 * additional_data = seq_num + TLSCompressed.type +
1811 * TLSCompressed.version + TLSCompressed.length;
1812 *
Hanno Becker99abf512019-05-20 14:50:53 +01001813 * For the CID extension, this is extended as follows
1814 * (quoting draft-ietf-tls-dtls-connection-id-05,
1815 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + DTLSPlaintext.type +
1818 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001819 * cid +
1820 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001821 * length_of_DTLSInnerPlaintext;
1822 */
1823
Hanno Becker3307b532017-12-27 21:37:21 +00001824 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1825 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001826 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001827
Hanno Beckera5a2b082019-05-15 14:03:01 +01001828#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001829 if( rec->cid_len != 0 )
1830 {
1831 memcpy( add_data + 11, rec->cid, rec->cid_len );
1832 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1833 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1834 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1835 *add_data_len = 13 + 1 + rec->cid_len;
1836 }
1837 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001838#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001839 {
1840 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13;
1843 }
Hanno Becker3307b532017-12-27 21:37:21 +00001844}
1845
Hanno Becker611a83b2018-01-03 14:27:32 +00001846int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1847 mbedtls_ssl_transform *transform,
1848 mbedtls_record *rec,
1849 int (*f_rng)(void *, unsigned char *, size_t),
1850 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001854 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001855 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001856 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001857 size_t post_avail;
1858
1859 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001860#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001861 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001862 ((void) ssl);
1863#endif
1864
1865 /* The PRNG is used for dynamic IV generation that's used
1866 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1867#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1868 ( defined(MBEDTLS_AES_C) || \
1869 defined(MBEDTLS_ARIA_C) || \
1870 defined(MBEDTLS_CAMELLIA_C) ) && \
1871 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1872 ((void) f_rng);
1873 ((void) p_rng);
1874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Hanno Becker3307b532017-12-27 21:37:21 +00001878 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 {
Hanno Becker3307b532017-12-27 21:37:21 +00001880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1881 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1882 }
Hanno Becker505089d2019-05-01 09:45:57 +01001883 if( rec == NULL
1884 || rec->buf == NULL
1885 || rec->buf_len < rec->data_offset
1886 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001888 || rec->cid_len != 0
1889#endif
1890 )
Hanno Becker3307b532017-12-27 21:37:21 +00001891 {
1892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001894 }
1895
Hanno Becker3307b532017-12-27 21:37:21 +00001896 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001897 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001899 data, rec->data_len );
1900
1901 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1902
1903 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1904 {
1905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1906 (unsigned) rec->data_len,
1907 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1908 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1909 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001910
Hanno Beckera5a2b082019-05-15 14:03:01 +01001911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001912 /*
1913 * Add CID information
1914 */
1915 rec->cid_len = transform->out_cid_len;
1916 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1917 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001918
1919 if( rec->cid_len != 0 )
1920 {
1921 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001922 * Wrap plaintext into DTLSInnerPlaintext structure.
1923 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001924 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001925 * Note that this changes `rec->data_len`, and hence
1926 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001927 */
1928 if( ssl_cid_build_inner_plaintext( data,
1929 &rec->data_len,
1930 post_avail,
1931 rec->type ) != 0 )
1932 {
1933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1934 }
1935
1936 rec->type = MBEDTLS_SSL_MSG_CID;
1937 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001938#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001939
Hanno Becker92c930f2019-04-29 17:31:37 +01001940 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1941
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001943 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001945#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 if( mode == MBEDTLS_MODE_STREAM ||
1947 ( mode == MBEDTLS_MODE_CBC
1948#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001949 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001950#endif
1951 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 {
Hanno Becker3307b532017-12-27 21:37:21 +00001953 if( post_avail < transform->maclen )
1954 {
1955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1957 }
1958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001961 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001962 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001963 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1964 data, rec->data_len, rec->ctr, rec->type, mac );
1965 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001966 }
1967 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1970 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001971 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Hanno Becker992b6872017-11-09 18:57:39 +00001973 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1974
Hanno Beckere83efe62019-04-29 13:52:53 +01001975 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001978 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001979 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1980 data, rec->data_len );
1981 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1982 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1983
1984 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001985 }
1986 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001987#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1990 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992
Hanno Becker3307b532017-12-27 21:37:21 +00001993 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1994 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995
Hanno Becker3307b532017-12-27 21:37:21 +00001996 rec->data_len += transform->maclen;
1997 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001998 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001999 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002000#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002 /*
2003 * Encrypt
2004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2006 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002007 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002009 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002011 "including %d bytes of padding",
2012 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Hanno Becker3307b532017-12-27 21:37:21 +00002014 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2015 transform->iv_enc, transform->ivlen,
2016 data, rec->data_len,
2017 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002020 return( ret );
2021 }
2022
Hanno Becker3307b532017-12-27 21:37:21 +00002023 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2026 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002028 }
Paul Bakker68884e32013-01-07 18:20:04 +01002029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002031
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002032#if defined(MBEDTLS_GCM_C) || \
2033 defined(MBEDTLS_CCM_C) || \
2034 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002036 mode == MBEDTLS_MODE_CCM ||
2037 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002038 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002039 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002041 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002042
Hanno Becker3307b532017-12-27 21:37:21 +00002043 /* Check that there's space for both the authentication tag
2044 * and the explicit IV before and after the record content. */
2045 if( post_avail < transform->taglen ||
2046 rec->data_offset < explicit_iv_len )
2047 {
2048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2049 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2050 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002051
Paul Bakker68884e32013-01-07 18:20:04 +01002052 /*
2053 * Generate IV
2054 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002055 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2056 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002057 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002059 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2060 explicit_iv_len );
2061 /* Prefix record content with explicit IV. */
2062 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002063 }
2064 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2065 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002066 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 unsigned char i;
2068
2069 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2070
2071 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002072 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002073 }
2074 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002075 {
2076 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002079 }
2080
Hanno Beckere83efe62019-04-29 13:52:53 +01002081 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002082
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002083 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2084 iv, transform->ivlen );
2085 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002086 data - explicit_iv_len, explicit_iv_len );
2087 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002091 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002092
Paul Bakker68884e32013-01-07 18:20:04 +01002093 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002094 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095 */
Hanno Becker3307b532017-12-27 21:37:21 +00002096
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002098 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002099 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002100 data, rec->data_len, /* source */
2101 data, &rec->data_len, /* destination */
2102 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002105 return( ret );
2106 }
2107
Hanno Becker3307b532017-12-27 21:37:21 +00002108 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2109 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 rec->data_len += transform->taglen + explicit_iv_len;
2112 rec->data_offset -= explicit_iv_len;
2113 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002114 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002115 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2118#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002119 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002122 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002123 size_t padlen, i;
2124 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002125
Hanno Becker3307b532017-12-27 21:37:21 +00002126 /* Currently we're always using minimal padding
2127 * (up to 255 bytes would be allowed). */
2128 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2129 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 padlen = 0;
2131
Hanno Becker3307b532017-12-27 21:37:21 +00002132 /* Check there's enough space in the buffer for the padding. */
2133 if( post_avail < padlen + 1 )
2134 {
2135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2136 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2137 }
2138
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002140 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker3307b532017-12-27 21:37:21 +00002142 rec->data_len += padlen + 1;
2143 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002146 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002147 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2148 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 */
Hanno Becker3307b532017-12-27 21:37:21 +00002150 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151 {
Hanno Becker3307b532017-12-27 21:37:21 +00002152 if( f_rng == NULL )
2153 {
2154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2156 }
2157
2158 if( rec->data_offset < transform->ivlen )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2161 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2162 }
2163
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002164 /*
2165 * Generate IV
2166 */
Hanno Becker3307b532017-12-27 21:37:21 +00002167 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002168 if( ret != 0 )
2169 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 memcpy( data - transform->ivlen, transform->iv_enc,
2172 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002179 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002180 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181
Hanno Becker3307b532017-12-27 21:37:21 +00002182 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2183 transform->iv_enc,
2184 transform->ivlen,
2185 data, rec->data_len,
2186 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002189 return( ret );
2190 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002191
Hanno Becker3307b532017-12-27 21:37:21 +00002192 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
2201 /*
2202 * Save IV in SSL3 and TLS1
2203 */
Hanno Becker3307b532017-12-27 21:37:21 +00002204 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2205 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 }
Hanno Becker3307b532017-12-27 21:37:21 +00002207 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002208#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002209 {
2210 data -= transform->ivlen;
2211 rec->data_offset -= transform->ivlen;
2212 rec->data_len += transform->ivlen;
2213 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002216 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002217 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002218 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2219
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220 /*
2221 * MAC(MAC_write_key, seq_num +
2222 * TLSCipherText.type +
2223 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002224 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002225 * IV + // except for TLS 1.0
2226 * ENC(content + padding + padding_length));
2227 */
Hanno Becker3307b532017-12-27 21:37:21 +00002228
2229 if( post_avail < transform->maclen)
2230 {
2231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2232 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2233 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Beckere83efe62019-04-29 13:52:53 +01002235 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002238 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002239 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002240
Hanno Becker3307b532017-12-27 21:37:21 +00002241 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2244 data, rec->data_len );
2245 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2246 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249
Hanno Becker3307b532017-12-27 21:37:21 +00002250 rec->data_len += transform->maclen;
2251 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002252 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002256 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002258 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 /* Make extra sure authentication was performed, exactly once */
2265 if( auth_done != 1 )
2266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269 }
2270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002272
2273 return( 0 );
2274}
2275
Hanno Becker611a83b2018-01-03 14:27:32 +00002276int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2277 mbedtls_ssl_transform *transform,
2278 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002279{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002282 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002283#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002284 size_t padlen = 0, correct = 1;
2285#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002286 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002287 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002288 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289
Hanno Becker611a83b2018-01-03 14:27:32 +00002290#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002291 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002292 ((void) ssl);
2293#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002297 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2300 }
2301 if( rec == NULL ||
2302 rec->buf == NULL ||
2303 rec->buf_len < rec->data_offset ||
2304 rec->buf_len - rec->data_offset < rec->data_len )
2305 {
2306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002308 }
2309
Hanno Becker4c6876b2017-12-27 21:28:58 +00002310 data = rec->buf + rec->data_offset;
2311 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
Hanno Beckera5a2b082019-05-15 14:03:01 +01002313#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002314 /*
2315 * Match record's CID with incoming CID.
2316 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002317 if( rec->cid_len != transform->in_cid_len ||
2318 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2319 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002320 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002321 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002322#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2325 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002326 {
2327 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002328 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2329 transform->iv_dec,
2330 transform->ivlen,
2331 data, rec->data_len,
2332 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002335 return( ret );
2336 }
2337
Hanno Becker4c6876b2017-12-27 21:28:58 +00002338 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
Paul Bakker68884e32013-01-07 18:20:04 +01002344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346#if defined(MBEDTLS_GCM_C) || \
2347 defined(MBEDTLS_CCM_C) || \
2348 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 mode == MBEDTLS_MODE_CCM ||
2351 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002352 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 /*
2357 * Compute and update sizes
2358 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002362 "+ taglen (%d)", rec->data_len,
2363 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002364 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002365 }
Paul Bakker68884e32013-01-07 18:20:04 +01002366
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002367 /*
2368 * Prepare IV
2369 */
2370 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2371 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002372 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002373 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002374 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002375
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002376 }
2377 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 unsigned char i;
2381
2382 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2383
2384 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002386 }
2387 else
2388 {
2389 /* Reminder if we ever add an AEAD mode with a different size */
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2391 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2392 }
2393
Hanno Becker4c6876b2017-12-27 21:28:58 +00002394 data += explicit_iv_len;
2395 rec->data_offset += explicit_iv_len;
2396 rec->data_len -= explicit_iv_len + transform->taglen;
2397
Hanno Beckere83efe62019-04-29 13:52:53 +01002398 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002399 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401
2402 memcpy( transform->iv_dec + transform->fixed_ivlen,
2403 data - explicit_iv_len, explicit_iv_len );
2404
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002407 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002408
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002411 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002412 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2414 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002415 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002416 data, rec->data_len,
2417 data, &olen,
2418 data + rec->data_len,
2419 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002425
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002426 return( ret );
2427 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002429
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002434 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2438#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002439 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002440 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002441 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002442 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Paul Bakker5121ce52009-01-03 21:22:43 +00002444 /*
Paul Bakker45829992013-01-03 14:52:21 +01002445 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002448 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2449 {
2450 /* The ciphertext is prefixed with the CBC IV. */
2451 minlen += transform->ivlen;
2452 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002453#endif
Paul Bakker45829992013-01-03 14:52:21 +01002454
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 /* Size considerations:
2456 *
2457 * - The CBC cipher text must not be empty and hence
2458 * at least of size transform->ivlen.
2459 *
2460 * Together with the potential IV-prefix, this explains
2461 * the first of the two checks below.
2462 *
2463 * - The record must contain a MAC, either in plain or
2464 * encrypted, depending on whether Encrypt-then-MAC
2465 * is used or not.
2466 * - If it is, the message contains the IV-prefix,
2467 * the CBC ciphertext, and the MAC.
2468 * - If it is not, the padded plaintext, and hence
2469 * the CBC ciphertext, has at least length maclen + 1
2470 * because there is at least the padding length byte.
2471 *
2472 * As the CBC ciphertext is not empty, both cases give the
2473 * lower bound minlen + maclen + 1 on the record size, which
2474 * we test for in the second check below.
2475 */
2476 if( rec->data_len < minlen + transform->ivlen ||
2477 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 "+ 1 ) ( + expl IV )", rec->data_len,
2481 transform->ivlen,
2482 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002484 }
2485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 /*
2487 * Authenticate before decrypt if enabled
2488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 {
Hanno Becker992b6872017-11-09 18:57:39 +00002492 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002495
Hanno Becker4c6876b2017-12-27 21:28:58 +00002496 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2497 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Beckere83efe62019-04-29 13:52:53 +01002499 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002500
Hanno Beckere83efe62019-04-29 13:52:53 +01002501 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2502 add_data_len );
2503 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2504 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2506 data, rec->data_len );
2507 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2508 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002509
Hanno Becker4c6876b2017-12-27 21:28:58 +00002510 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2511 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002512 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2516 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524
2525 /*
2526 * Check length sanity
2527 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002528 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002537 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002538 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002540 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002541 /* This is safe because data_len >= minlen + maclen + 1 initially,
2542 * and at this point we have at most subtracted maclen (note that
2543 * minlen == transform->ivlen here). */
2544 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 data += transform->ivlen;
2547 rec->data_offset += transform->ivlen;
2548 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002549 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002551
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2553 transform->iv_dec, transform->ivlen,
2554 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002557 return( ret );
2558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002559
Hanno Becker4c6876b2017-12-27 21:28:58 +00002560 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
2569 /*
2570 * Save IV in SSL3 and TLS1
2571 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002572 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2573 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002576
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577 /* Safe since data_len >= minlen + maclen + 1, so after having
2578 * subtracted at most minlen and maclen up to this point,
2579 * data_len > 0. */
2580 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002581
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582 if( auth_done == 1 )
2583 {
2584 correct *= ( rec->data_len >= padlen + 1 );
2585 padlen *= ( rec->data_len >= padlen + 1 );
2586 }
2587 else
Paul Bakker45829992013-01-03 14:52:21 +01002588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002590 if( rec->data_len < transform->maclen + padlen + 1 )
2591 {
2592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2593 rec->data_len,
2594 transform->maclen,
2595 padlen + 1 ) );
2596 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002597#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002598
2599 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2600 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602
Hanno Becker4c6876b2017-12-27 21:28:58 +00002603 padlen++;
2604
2605 /* Regardless of the validity of the padding,
2606 * we have data_len >= padlen here. */
2607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002609 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613#if defined(MBEDTLS_SSL_DEBUG_ALL)
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 "should be no more than %d",
2616 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002617#endif
Paul Bakker45829992013-01-03 14:52:21 +01002618 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
2621 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2623#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2624 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002625 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002627 /* The padding check involves a series of up to 256
2628 * consecutive memory reads at the end of the record
2629 * plaintext buffer. In order to hide the length and
2630 * validity of the padding, always perform exactly
2631 * `min(256,plaintext_len)` reads (but take into account
2632 * only the last `padlen` bytes for the padding check). */
2633 size_t pad_count = 0;
2634 size_t real_count = 0;
2635 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002636
Hanno Becker4c6876b2017-12-27 21:28:58 +00002637 /* Index of first padding byte; it has been ensured above
2638 * that the subtraction is safe. */
2639 size_t const padding_idx = rec->data_len - padlen;
2640 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2641 size_t const start_idx = rec->data_len - num_checks;
2642 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002645 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002646 real_count |= ( idx >= padding_idx );
2647 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002648 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002652 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002654#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002655 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002657 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2659 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002663 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002664
Hanno Becker4c6876b2017-12-27 21:28:58 +00002665 /* If the padding was found to be invalid, padlen == 0
2666 * and the subtraction is safe. If the padding was found valid,
2667 * padlen hasn't been changed and the previous assertion
2668 * data_len >= padlen still holds. */
2669 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002671 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002673 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2676 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002678
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002679#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002681 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002682#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002685 * Authenticate if not done yet.
2686 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002688#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002689 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002690 {
Hanno Becker992b6872017-11-09 18:57:39 +00002691 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002692
Hanno Becker4c6876b2017-12-27 21:28:58 +00002693 /* If the initial value of padlen was such that
2694 * data_len < maclen + padlen + 1, then padlen
2695 * got reset to 1, and the initial check
2696 * data_len >= minlen + maclen + 1
2697 * guarantees that at this point we still
2698 * have at least data_len >= maclen.
2699 *
2700 * If the initial value of padlen was such that
2701 * data_len >= maclen + padlen + 1, then we have
2702 * subtracted either padlen + 1 (if the padding was correct)
2703 * or 0 (if the padding was incorrect) since then,
2704 * hence data_len >= maclen in any case.
2705 */
2706 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Hanno Beckere83efe62019-04-29 13:52:53 +01002708 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002711 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002713 ssl_mac( &transform->md_ctx_dec,
2714 transform->mac_dec,
2715 data, rec->data_len,
2716 rec->ctr, rec->type,
2717 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002718 }
2719 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2721#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2722 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002723 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002724 {
2725 /*
2726 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002727 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002728 *
2729 * Known timing attacks:
2730 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2731 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002732 * To compensate for different timings for the MAC calculation
2733 * depending on how much padding was removed (which is determined
2734 * by padlen), process extra_run more blocks through the hash
2735 * function.
2736 *
2737 * The formula in the paper is
2738 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2739 * where L1 is the size of the header plus the decrypted message
2740 * plus CBC padding and L2 is the size of the header plus the
2741 * decrypted message. This is for an underlying hash function
2742 * with 64-byte blocks.
2743 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2744 * correctly. We round down instead of up, so -56 is the correct
2745 * value for our calculations instead of -55.
2746 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002747 * Repeat the formula rather than defining a block_size variable.
2748 * This avoids requiring division by a variable at runtime
2749 * (which would be marginally less efficient and would require
2750 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002751 */
2752 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002754
2755 /*
2756 * The next two sizes are the minimum and maximum values of
2757 * in_msglen over all padlen values.
2758 *
2759 * They're independent of padlen, since we previously did
2760 * in_msglen -= padlen.
2761 *
2762 * Note that max_len + maclen is never more than the buffer
2763 * length, as we previously did in_msglen -= maclen too.
2764 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002765 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002766 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2767
Hanno Becker4c6876b2017-12-27 21:28:58 +00002768 memset( tmp, 0, sizeof( tmp ) );
2769
2770 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002771 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002772#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2773 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 case MBEDTLS_MD_MD5:
2775 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002776 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002778 extra_run =
2779 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2780 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 break;
2782#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002783#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002786 extra_run =
2787 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2788 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002789 break;
2790#endif
2791 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002793 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2794 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002795
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002796 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Beckere83efe62019-04-29 13:52:53 +01002798 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2799 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002800 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2801 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002802 /* Make sure we access everything even when padlen > 0. This
2803 * makes the synchronisation requirements for just-in-time
2804 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002805 ssl_read_memory( data + rec->data_len, padlen );
2806 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002807
2808 /* Call mbedtls_md_process at least once due to cache attacks
2809 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002810 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002811 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002812
Hanno Becker4c6876b2017-12-27 21:28:58 +00002813 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Make sure we access all the memory that could contain the MAC,
2816 * before we check it in the next code block. This makes the
2817 * synchronisation requirements for just-in-time Prime+Probe
2818 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 ssl_read_memory( data + min_len,
2820 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 }
2822 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2824 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2827 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002829
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002830#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002831 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2832 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Hanno Becker4c6876b2017-12-27 21:28:58 +00002835 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2836 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_DEBUG_ALL)
2839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002840#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002841 correct = 0;
2842 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002845
2846 /*
2847 * Finally check the correct flag
2848 */
2849 if( correct == 0 )
2850 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002851#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002852
2853 /* Make extra sure authentication was performed, exactly once */
2854 if( auth_done != 1 )
2855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002858 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Beckera5a2b082019-05-15 14:03:01 +01002860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002861 if( rec->cid_len != 0 )
2862 {
2863 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2864 &rec->type );
2865 if( ret != 0 )
2866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2867 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
2872 return( 0 );
2873}
2874
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002875#undef MAC_NONE
2876#undef MAC_PLAINTEXT
2877#undef MAC_CIPHERTEXT
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880/*
2881 * Compression/decompression functions
2882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884{
2885 int ret;
2886 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002887 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002888 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002889 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002893 if( len_pre == 0 )
2894 return( 0 );
2895
Paul Bakker2770fbd2012-07-03 13:30:23 +00002896 memcpy( msg_pre, ssl->out_msg, len_pre );
2897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 ssl->out_msglen ) );
2900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902 ssl->out_msg, ssl->out_msglen );
2903
Paul Bakker48916f92012-09-16 19:57:18 +00002904 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2905 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2906 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002907 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908
Paul Bakker48916f92012-09-16 19:57:18 +00002909 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910 if( ret != Z_OK )
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2913 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002914 }
2915
Angus Grattond8213d02016-05-25 20:56:48 +10002916 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002917 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 ssl->out_msglen ) );
2921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923 ssl->out_msg, ssl->out_msglen );
2924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002926
2927 return( 0 );
2928}
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931{
2932 int ret;
2933 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002934 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002936 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002940 if( len_pre == 0 )
2941 return( 0 );
2942
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 memcpy( msg_pre, ssl->in_msg, len_pre );
2944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946 ssl->in_msglen ) );
2947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 ssl->in_msg, ssl->in_msglen );
2950
Paul Bakker48916f92012-09-16 19:57:18 +00002951 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2952 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2953 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002954 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002955 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958 if( ret != Z_OK )
2959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2961 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962 }
2963
Angus Grattond8213d02016-05-25 20:56:48 +10002964 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002965 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 ssl->in_msglen ) );
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 ssl->in_msg, ssl->in_msglen );
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974
2975 return( 0 );
2976}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2980static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_PROTO_DTLS)
2983static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002984{
2985 /* If renegotiation is not enforced, retransmit until we would reach max
2986 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002987 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002989 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002990 unsigned char doublings = 1;
2991
2992 while( ratio != 0 )
2993 {
2994 ++doublings;
2995 ratio >>= 1;
2996 }
2997
2998 if( ++ssl->renego_records_seen > doublings )
2999 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003001 return( 0 );
3002 }
3003 }
3004
3005 return( ssl_write_hello_request( ssl ) );
3006}
3007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003011 * Fill the input message buffer by appending data to it.
3012 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003013 *
3014 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3015 * available (from this read and/or a previous one). Otherwise, an error code
3016 * is returned (possibly EOF or WANT_READ).
3017 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3019 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3020 * since we always read a whole datagram at once.
3021 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003022 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003023 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026{
Paul Bakker23986e52011-04-24 08:57:21 +00003027 int ret;
3028 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003032 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003035 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003037 }
3038
Angus Grattond8213d02016-05-25 20:56:48 +10003039 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003043 }
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003046 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003048 uint32_t timeout;
3049
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003050 /* Just to be sure */
3051 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3052 {
3053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3054 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3056 }
3057
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 /*
3059 * The point is, we need to always read a full datagram at once, so we
3060 * sometimes read more then requested, and handle the additional data.
3061 * It could be the rest of the current record (while fetching the
3062 * header) and/or some other records in the same datagram.
3063 */
3064
3065 /*
3066 * Move to the next record in the already read datagram if applicable
3067 */
3068 if( ssl->next_record_offset != 0 )
3069 {
3070 if( ssl->in_left < ssl->next_record_offset )
3071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3073 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074 }
3075
3076 ssl->in_left -= ssl->next_record_offset;
3077
3078 if( ssl->in_left != 0 )
3079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 ssl->next_record_offset ) );
3082 memmove( ssl->in_hdr,
3083 ssl->in_hdr + ssl->next_record_offset,
3084 ssl->in_left );
3085 }
3086
3087 ssl->next_record_offset = 0;
3088 }
3089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003092
3093 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003094 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 */
3096 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003100 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003101
3102 /*
3103 * A record can't be split accross datagrams. If we need to read but
3104 * are not at the beginning of a new record, the caller did something
3105 * wrong.
3106 */
3107 if( ssl->in_left != 0 )
3108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3110 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003111 }
3112
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 /*
3114 * Don't even try to read if time's out already.
3115 * This avoids by-passing the timer when repeatedly receiving messages
3116 * that will end up being dropped.
3117 */
3118 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003119 {
3120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003121 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003122 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003123 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003124 {
Angus Grattond8213d02016-05-25 20:56:48 +10003125 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 timeout = ssl->handshake->retransmit_timeout;
3129 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003130 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003134 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3136 timeout );
3137 else
3138 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003141
3142 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 }
3145
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003146 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003149 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003153 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003156 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003157 }
3158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003162 return( ret );
3163 }
3164
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003165 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003166 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003168 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003170 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003171 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003174 return( ret );
3175 }
3176
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003177 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 }
3181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182 if( ret < 0 )
3183 return( ret );
3184
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003185 ssl->in_left = ret;
3186 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003187 MBEDTLS_SSL_TRANSPORT_ELSE
3188#endif /* MBEDTLS_SSL_PROTO_DTLS */
3189#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003192 ssl->in_left, nb_want ) );
3193
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003194 while( ssl->in_left < nb_want )
3195 {
3196 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003197
3198 if( ssl_check_timer( ssl ) != 0 )
3199 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3200 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 {
3202 if( ssl->f_recv_timeout != NULL )
3203 {
3204 ret = ssl->f_recv_timeout( ssl->p_bio,
3205 ssl->in_hdr + ssl->in_left, len,
3206 ssl->conf->read_timeout );
3207 }
3208 else
3209 {
3210 ret = ssl->f_recv( ssl->p_bio,
3211 ssl->in_hdr + ssl->in_left, len );
3212 }
3213 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003216 ssl->in_left, nb_want ) );
3217 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218
3219 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221
3222 if( ret < 0 )
3223 return( ret );
3224
mohammad160352aecb92018-03-28 23:41:40 -07003225 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003226 {
Darryl Green11999bb2018-03-13 15:22:58 +00003227 MBEDTLS_SSL_DEBUG_MSG( 1,
3228 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003229 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003230 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3231 }
3232
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003233 ssl->in_left += ret;
3234 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003236#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003239
3240 return( 0 );
3241}
3242
3243/*
3244 * Flush any data not yet written
3245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003247{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003248 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003249 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003253 if( ssl->f_send == NULL )
3254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003256 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 }
3259
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003260 /* Avoid incrementing counter if data is flushed */
3261 if( ssl->out_left == 0 )
3262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003264 return( 0 );
3265 }
3266
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 while( ssl->out_left > 0 )
3268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003270 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003271
Hanno Becker2b1e3542018-08-06 11:19:13 +01003272 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003273 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276
3277 if( ret <= 0 )
3278 return( ret );
3279
mohammad160352aecb92018-03-28 23:41:40 -07003280 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003281 {
Darryl Green11999bb2018-03-13 15:22:58 +00003282 MBEDTLS_SSL_DEBUG_MSG( 1,
3283 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003284 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3286 }
3287
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 ssl->out_left -= ret;
3289 }
3290
Hanno Becker2b1e3542018-08-06 11:19:13 +01003291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003292 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003293 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003294 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003295 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003296 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003298#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003299 {
3300 ssl->out_hdr = ssl->out_buf + 8;
3301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003302#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
3307 return( 0 );
3308}
3309
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003310/*
3311 * Functions to handle the DTLS retransmission state machine
3312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314/*
3315 * Append current handshake message to current outgoing flight
3316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3321 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3322 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003323
3324 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003325 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003326 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003329 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330 }
3331
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
3339 /* Copy current handshake message with headers */
3340 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3341 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003342 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343 msg->next = NULL;
3344
3345 /* Append to the current flight */
3346 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003347 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348 else
3349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351 while( cur->next != NULL )
3352 cur = cur->next;
3353 cur->next = msg;
3354 }
3355
Hanno Becker3b235902018-08-06 09:54:53 +01003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 return( 0 );
3358}
3359
3360/*
3361 * Free the current flight of handshake messages
3362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 mbedtls_ssl_flight_item *cur = flight;
3366 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367
3368 while( cur != NULL )
3369 {
3370 next = cur->next;
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_free( cur->p );
3373 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 cur = next;
3376 }
3377}
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3380static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003381#endif
3382
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003383/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003384 * Swap transform_out and out_ctr with the alternative ones
3385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003387{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003389 unsigned char tmp_out_ctr[8];
3390
3391 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394 return;
3395 }
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003398
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003400 tmp_transform = ssl->transform_out;
3401 ssl->transform_out = ssl->handshake->alt_transform_out;
3402 ssl->handshake->alt_transform_out = tmp_transform;
3403
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003405 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3406 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003408
3409 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003410 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3413 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3418 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003419 }
3420 }
3421#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003422}
3423
3424/*
3425 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003426 */
3427int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3428{
3429 int ret = 0;
3430
3431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3432
3433 ret = mbedtls_ssl_flight_transmit( ssl );
3434
3435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3436
3437 return( ret );
3438}
3439
3440/*
3441 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003442 *
3443 * Need to remember the current message in case flush_output returns
3444 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003447int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003448{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003453 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003455
3456 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003457 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003458 ssl_swap_epochs( ssl );
3459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003461 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003462
3463 while( ssl->handshake->cur_msg != NULL )
3464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003466 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003467
Hanno Beckere1dcb032018-08-17 16:47:58 +01003468 int const is_finished =
3469 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3470 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3471
Hanno Becker04da1892018-08-14 13:22:10 +01003472 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3473 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3474
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003475 /* Swap epochs before sending Finished: we can't do it after
3476 * sending ChangeCipherSpec, in case write returns WANT_READ.
3477 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003478 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003479 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003481 ssl_swap_epochs( ssl );
3482 }
3483
Hanno Becker67bc7c32018-08-06 11:33:50 +01003484 ret = ssl_get_remaining_payload_in_datagram( ssl );
3485 if( ret < 0 )
3486 return( ret );
3487 max_frag_len = (size_t) ret;
3488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* CCS is copied as is, while HS messages may need fragmentation */
3490 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3491 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492 if( max_frag_len == 0 )
3493 {
3494 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3495 return( ret );
3496
3497 continue;
3498 }
3499
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003500 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003501 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003502 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003504 /* Update position inside current message */
3505 ssl->handshake->cur_msg_p += cur->len;
3506 }
3507 else
3508 {
3509 const unsigned char * const p = ssl->handshake->cur_msg_p;
3510 const size_t hs_len = cur->len - 12;
3511 const size_t frag_off = p - ( cur->p + 12 );
3512 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003513 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003514
Hanno Beckere1dcb032018-08-17 16:47:58 +01003515 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003516 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003517 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003519
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3521 return( ret );
3522
3523 continue;
3524 }
3525 max_hs_frag_len = max_frag_len - 12;
3526
3527 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3528 max_hs_frag_len : rem_len;
3529
3530 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003531 {
3532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003533 (unsigned) cur_hs_frag_len,
3534 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003535 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003536
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003537 /* Messages are stored with handshake headers as if not fragmented,
3538 * copy beginning of headers then fill fragmentation fields.
3539 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3540 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003541
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003542 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3543 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3544 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3545
Hanno Becker67bc7c32018-08-06 11:33:50 +01003546 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3547 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3548 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549
3550 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3551
Hanno Becker3f7b9732018-08-28 09:53:25 +01003552 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3554 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003555 ssl->out_msgtype = cur->type;
3556
3557 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003559 }
3560
3561 /* If done with the current message move to the next one if any */
3562 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3563 {
3564 if( cur->next != NULL )
3565 {
3566 ssl->handshake->cur_msg = cur->next;
3567 ssl->handshake->cur_msg_p = cur->next->p + 12;
3568 }
3569 else
3570 {
3571 ssl->handshake->cur_msg = NULL;
3572 ssl->handshake->cur_msg_p = NULL;
3573 }
3574 }
3575
3576 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003577 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580 return( ret );
3581 }
3582 }
3583
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3585 return( ret );
3586
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003587 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3589 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003590 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003593 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3594 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003595
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597
3598 return( 0 );
3599}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003600
3601/*
3602 * To be called when the last message of an incoming flight is received.
3603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605{
3606 /* We won't need to resend that one any more */
3607 ssl_flight_free( ssl->handshake->flight );
3608 ssl->handshake->flight = NULL;
3609 ssl->handshake->cur_msg = NULL;
3610
3611 /* The next incoming flight will start with this msg_seq */
3612 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3613
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003614 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003615 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003616
Hanno Becker0271f962018-08-16 13:23:47 +01003617 /* Clear future message buffering structure. */
3618 ssl_buffering_free( ssl );
3619
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003620 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621 ssl_set_timer( ssl, 0 );
3622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3624 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003627 }
3628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003630}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003631
3632/*
3633 * To be called when the last message of an outgoing flight is send.
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003636{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003637 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003638 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3641 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003644 }
3645 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
Paul Bakker5121ce52009-01-03 21:22:43 +00003650/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003651 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653
3654/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003655 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656 *
3657 * - fill in handshake headers
3658 * - update handshake checksum
3659 * - DTLS: save message for resending
3660 * - then pass to the record layer
3661 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * DTLS: except for HelloRequest, messages are only queued, and will only be
3663 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003664 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003665 * Inputs:
3666 * - ssl->out_msglen: 4 + actual handshake message len
3667 * (4 is the size of handshake headers for TLS)
3668 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3669 * - ssl->out_msg + 4: the handshake message body
3670 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003671 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * - ssl->out_msglen: the length of the record contents
3673 * (including handshake headers but excluding record headers)
3674 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003675 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003677{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003678 int ret;
3679 const size_t hs_len = ssl->out_msglen - 4;
3680 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3683
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003684 /*
3685 * Sanity checks
3686 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003687 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003688 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3689 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003690 /* In SSLv3, the client might send a NoCertificate alert. */
3691#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3692 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3693 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3694 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3695#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3696 {
3697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3699 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003701
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003702 /* Whenever we send anything different from a
3703 * HelloRequest we should be in a handshake - double check. */
3704 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3705 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706 ssl->handshake == NULL )
3707 {
3708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3718 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003721
Hanno Beckerb50a2532018-08-06 11:52:54 +01003722 /* Double-check that we did not exceed the bounds
3723 * of the outgoing record buffer.
3724 * This should never fail as the various message
3725 * writing functions must obey the bounds of the
3726 * outgoing record buffer, but better be safe.
3727 *
3728 * Note: We deliberately do not check for the MTU or MFL here.
3729 */
3730 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3731 {
3732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3733 "size %u, maximum %u",
3734 (unsigned) ssl->out_msglen,
3735 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3737 }
3738
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003739 /*
3740 * Fill handshake headers
3741 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003742 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003744 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3745 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3746 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003748 /*
3749 * DTLS has additional fields in the Handshake layer,
3750 * between the length field and the actual payload:
3751 * uint16 message_seq;
3752 * uint24 fragment_offset;
3753 * uint24 fragment_length;
3754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003756 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003757 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003758 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003759 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003760 {
3761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3762 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003763 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003764 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003765 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3766 }
3767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003769 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003771 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003772 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003773 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003774 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3775 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3776 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003777 }
3778 else
3779 {
3780 ssl->out_msg[4] = 0;
3781 ssl->out_msg[5] = 0;
3782 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003783
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003784 /* Handshake hashes are computed without fragmentation,
3785 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003786 memset( ssl->out_msg + 6, 0x00, 3 );
3787 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003790
Hanno Becker0207e532018-08-28 10:28:28 +01003791 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003792 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3793 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003798 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003799 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3800 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801 {
3802 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805 return( ret );
3806 }
3807 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003808 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003809#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003810 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003811 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003812 {
3813 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3814 return( ret );
3815 }
3816 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003817
3818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3819
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003820 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821}
3822
3823/*
3824 * Record layer functions
3825 */
3826
3827/*
3828 * Write current record.
3829 *
3830 * Uses:
3831 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3832 * - ssl->out_msglen: length of the record content (excl headers)
3833 * - ssl->out_msg: record content
3834 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003836{
3837 int ret, done = 0;
3838 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003839 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003840
3841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003844 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003846 {
3847 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003850 return( ret );
3851 }
3852
3853 len = ssl->out_msglen;
3854 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3858 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862 ret = mbedtls_ssl_hw_record_write( ssl );
3863 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3866 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003867 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003868
3869 if( ret == 0 )
3870 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 if( !done )
3874 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003875 unsigned i;
3876 size_t protected_record_size;
3877
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003878 /* Skip writing the record content type to after the encryption,
3879 * as it may change when using the CID extension. */
3880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003882 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003883
Hanno Becker19859472018-08-06 09:40:20 +01003884 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003885 ssl->out_len[0] = (unsigned char)( len >> 8 );
3886 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887
Paul Bakker48916f92012-09-16 19:57:18 +00003888 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003889 {
Hanno Becker3307b532017-12-27 21:37:21 +00003890 mbedtls_record rec;
3891
3892 rec.buf = ssl->out_iv;
3893 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3894 ( ssl->out_iv - ssl->out_buf );
3895 rec.data_len = ssl->out_msglen;
3896 rec.data_offset = ssl->out_msg - rec.buf;
3897
3898 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3899 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3900 ssl->conf->transport, rec.ver );
3901 rec.type = ssl->out_msgtype;
3902
Hanno Beckera5a2b082019-05-15 14:03:01 +01003903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003904 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003905 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003907
Hanno Becker611a83b2018-01-03 14:27:32 +00003908 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003909 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912 return( ret );
3913 }
3914
Hanno Becker3307b532017-12-27 21:37:21 +00003915 if( rec.data_offset != 0 )
3916 {
3917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3919 }
3920
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003921 /* Update the record content type and CID. */
3922 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003924 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003925#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003926 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003927 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3928 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003929 }
3930
Hanno Becker43395762019-05-03 14:46:38 +01003931 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003932
3933#if defined(MBEDTLS_SSL_PROTO_DTLS)
3934 /* In case of DTLS, double-check that we don't exceed
3935 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003936 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003937 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003938 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939 if( ret < 0 )
3940 return( ret );
3941
3942 if( protected_record_size > (size_t) ret )
3943 {
3944 /* Should never happen */
3945 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3946 }
3947 }
3948#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003949
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003950 /* Now write the potentially updated record content type. */
3951 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003954 "version = [%d:%d], msglen = %d",
3955 ssl->out_hdr[0], ssl->out_hdr[1],
3956 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003959 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003960
3961 ssl->out_left += protected_record_size;
3962 ssl->out_hdr += protected_record_size;
3963 ssl_update_out_pointers( ssl, ssl->transform_out );
3964
Hanno Becker04484622018-08-06 09:49:38 +01003965 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3966 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3967 break;
3968
3969 /* The loop goes to its end iff the counter is wrapping */
3970 if( i == ssl_ep_len( ssl ) )
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3973 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3974 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003978 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003979 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003981 size_t remaining;
3982 ret = ssl_get_remaining_payload_in_datagram( ssl );
3983 if( ret < 0 )
3984 {
3985 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3986 ret );
3987 return( ret );
3988 }
3989
3990 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003992 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003994 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003995 else
3996 {
Hanno Becker513815a2018-08-20 11:56:09 +01003997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 }
3999 }
4000#endif /* MBEDTLS_SSL_PROTO_DTLS */
4001
4002 if( ( flush == SSL_FORCE_FLUSH ) &&
4003 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 return( ret );
4007 }
4008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
4011 return( 0 );
4012}
4013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004015
4016static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4017{
4018 if( ssl->in_msglen < ssl->in_hslen ||
4019 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4020 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4021 {
4022 return( 1 );
4023 }
4024 return( 0 );
4025}
Hanno Becker44650b72018-08-16 12:51:11 +01004026
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004027static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004028{
4029 return( ( ssl->in_msg[9] << 16 ) |
4030 ( ssl->in_msg[10] << 8 ) |
4031 ssl->in_msg[11] );
4032}
4033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[6] << 16 ) |
4037 ( ssl->in_msg[7] << 8 ) |
4038 ssl->in_msg[8] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 uint32_t msg_len, frag_off, frag_len;
4044
4045 msg_len = ssl_get_hs_total_len( ssl );
4046 frag_off = ssl_get_hs_frag_off( ssl );
4047 frag_len = ssl_get_hs_frag_len( ssl );
4048
4049 if( frag_off > msg_len )
4050 return( -1 );
4051
4052 if( frag_len > msg_len - frag_off )
4053 return( -1 );
4054
4055 if( frag_len + 12 > ssl->in_msglen )
4056 return( -1 );
4057
4058 return( 0 );
4059}
4060
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004061/*
4062 * Mark bits in bitmask (used for DTLS HS reassembly)
4063 */
4064static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4065{
4066 unsigned int start_bits, end_bits;
4067
4068 start_bits = 8 - ( offset % 8 );
4069 if( start_bits != 8 )
4070 {
4071 size_t first_byte_idx = offset / 8;
4072
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004073 /* Special case */
4074 if( len <= start_bits )
4075 {
4076 for( ; len != 0; len-- )
4077 mask[first_byte_idx] |= 1 << ( start_bits - len );
4078
4079 /* Avoid potential issues with offset or len becoming invalid */
4080 return;
4081 }
4082
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004083 offset += start_bits; /* Now offset % 8 == 0 */
4084 len -= start_bits;
4085
4086 for( ; start_bits != 0; start_bits-- )
4087 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4088 }
4089
4090 end_bits = len % 8;
4091 if( end_bits != 0 )
4092 {
4093 size_t last_byte_idx = ( offset + len ) / 8;
4094
4095 len -= end_bits; /* Now len % 8 == 0 */
4096
4097 for( ; end_bits != 0; end_bits-- )
4098 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4099 }
4100
4101 memset( mask + offset / 8, 0xFF, len / 8 );
4102}
4103
4104/*
4105 * Check that bitmask is full
4106 */
4107static int ssl_bitmask_check( unsigned char *mask, size_t len )
4108{
4109 size_t i;
4110
4111 for( i = 0; i < len / 8; i++ )
4112 if( mask[i] != 0xFF )
4113 return( -1 );
4114
4115 for( i = 0; i < len % 8; i++ )
4116 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4117 return( -1 );
4118
4119 return( 0 );
4120}
4121
Hanno Becker56e205e2018-08-16 09:06:12 +01004122/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004123static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004124 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004125{
Hanno Becker56e205e2018-08-16 09:06:12 +01004126 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004127
Hanno Becker56e205e2018-08-16 09:06:12 +01004128 alloc_len = 12; /* Handshake header */
4129 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004130
Hanno Beckerd07df862018-08-16 09:14:58 +01004131 if( add_bitmap )
4132 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004133
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004134 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004135}
Hanno Becker56e205e2018-08-16 09:06:12 +01004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004138
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004139static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004140{
4141 return( ( ssl->in_msg[1] << 16 ) |
4142 ( ssl->in_msg[2] << 8 ) |
4143 ssl->in_msg[3] );
4144}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004145
Simon Butcher99000142016-10-13 17:21:01 +01004146int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004147{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004151 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004153 }
4154
Hanno Becker12555c62018-08-16 12:47:53 +01004155 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004158 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004159 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004162 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004164 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004165 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004166
Hanno Becker44650b72018-08-16 12:51:11 +01004167 if( ssl_check_hs_header( ssl ) != 0 )
4168 {
4169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4171 }
4172
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004173 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004174 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4175 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4176 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4177 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004178 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004179 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4180 {
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4182 recv_msg_seq,
4183 ssl->handshake->in_msg_seq ) );
4184 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4185 }
4186
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004187 /* Retransmit only on last message from previous flight, to avoid
4188 * too many retransmissions.
4189 * Besides, No sane server ever retransmits HelloVerifyRequest */
4190 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004194 "message_seq = %d, start_of_flight = %d",
4195 recv_msg_seq,
4196 ssl->handshake->in_flight_start_seq ) );
4197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 return( ret );
4202 }
4203 }
4204 else
4205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004207 "message_seq = %d, expected = %d",
4208 recv_msg_seq,
4209 ssl->handshake->in_msg_seq ) );
4210 }
4211
Hanno Becker90333da2017-10-10 11:27:13 +01004212 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004213 }
4214 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004215
Hanno Becker6d97ef52018-08-16 13:09:04 +01004216 /* Message reassembly is handled alongside buffering of future
4217 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004218 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004219 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004220 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004224 }
4225 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004226 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004228#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004229 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004230 /* With TLS we don't handle fragmentation (for now) */
4231 if( ssl->in_msglen < ssl->in_hslen )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4234 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4235 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004236 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004237#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004238
Simon Butcher99000142016-10-13 17:21:01 +01004239 return( 0 );
4240}
4241
4242void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4243{
Hanno Becker0271f962018-08-16 13:23:47 +01004244 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004245
Hanno Becker0271f962018-08-16 13:23:47 +01004246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004247 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004248 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004249 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004250
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004251 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004253 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004254 ssl->handshake != NULL )
4255 {
Hanno Becker0271f962018-08-16 13:23:47 +01004256 unsigned offset;
4257 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004258
Hanno Becker0271f962018-08-16 13:23:47 +01004259 /* Increment handshake sequence number */
4260 hs->in_msg_seq++;
4261
4262 /*
4263 * Clear up handshake buffering and reassembly structure.
4264 */
4265
4266 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004267 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004268
4269 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004270 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4271 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004272 offset++, hs_buf++ )
4273 {
4274 *hs_buf = *(hs_buf + 1);
4275 }
4276
4277 /* Create a fresh last entry */
4278 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004279 }
4280#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281}
4282
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004283/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004284 * DTLS anti-replay: RFC 6347 4.1.2.6
4285 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004286 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4287 * Bit n is set iff record number in_window_top - n has been seen.
4288 *
4289 * Usually, in_window_top is the last record number seen and the lsb of
4290 * in_window is set. The only exception is the initial state (record number 0
4291 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004293#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4294static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004295{
4296 ssl->in_window_top = 0;
4297 ssl->in_window = 0;
4298}
4299
4300static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4301{
4302 return( ( (uint64_t) buf[0] << 40 ) |
4303 ( (uint64_t) buf[1] << 32 ) |
4304 ( (uint64_t) buf[2] << 24 ) |
4305 ( (uint64_t) buf[3] << 16 ) |
4306 ( (uint64_t) buf[4] << 8 ) |
4307 ( (uint64_t) buf[5] ) );
4308}
4309
4310/*
4311 * Return 0 if sequence number is acceptable, -1 otherwise
4312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004313int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004314{
4315 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4316 uint64_t bit;
4317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004319 return( 0 );
4320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321 if( rec_seqnum > ssl->in_window_top )
4322 return( 0 );
4323
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004324 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004325
4326 if( bit >= 64 )
4327 return( -1 );
4328
4329 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4330 return( -1 );
4331
4332 return( 0 );
4333}
4334
4335/*
4336 * Update replay window on new validated record
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004342 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004343 return;
4344
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 if( rec_seqnum > ssl->in_window_top )
4346 {
4347 /* Update window_top and the contents of the window */
4348 uint64_t shift = rec_seqnum - ssl->in_window_top;
4349
4350 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004351 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004353 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004355 ssl->in_window |= 1;
4356 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004357
4358 ssl->in_window_top = rec_seqnum;
4359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004360 else
4361 {
4362 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004363 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 if( bit < 64 ) /* Always true, but be extra sure */
4366 ssl->in_window |= (uint64_t) 1 << bit;
4367 }
4368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004369#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004370
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004372/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004373static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4374
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004375/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004376 * Without any SSL context, check if a datagram looks like a ClientHello with
4377 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004378 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379 *
4380 * - if cookie is valid, return 0
4381 * - if ClientHello looks superficially valid but cookie is not,
4382 * fill obuf and set olen, then
4383 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4384 * - otherwise return a specific error code
4385 */
4386static int ssl_check_dtls_clihlo_cookie(
4387 mbedtls_ssl_cookie_write_t *f_cookie_write,
4388 mbedtls_ssl_cookie_check_t *f_cookie_check,
4389 void *p_cookie,
4390 const unsigned char *cli_id, size_t cli_id_len,
4391 const unsigned char *in, size_t in_len,
4392 unsigned char *obuf, size_t buf_len, size_t *olen )
4393{
4394 size_t sid_len, cookie_len;
4395 unsigned char *p;
4396
4397 if( f_cookie_write == NULL || f_cookie_check == NULL )
4398 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4399
4400 /*
4401 * Structure of ClientHello with record and handshake headers,
4402 * and expected values. We don't need to check a lot, more checks will be
4403 * done when actually parsing the ClientHello - skipping those checks
4404 * avoids code duplication and does not make cookie forging any easier.
4405 *
4406 * 0-0 ContentType type; copied, must be handshake
4407 * 1-2 ProtocolVersion version; copied
4408 * 3-4 uint16 epoch; copied, must be 0
4409 * 5-10 uint48 sequence_number; copied
4410 * 11-12 uint16 length; (ignored)
4411 *
4412 * 13-13 HandshakeType msg_type; (ignored)
4413 * 14-16 uint24 length; (ignored)
4414 * 17-18 uint16 message_seq; copied
4415 * 19-21 uint24 fragment_offset; copied, must be 0
4416 * 22-24 uint24 fragment_length; (ignored)
4417 *
4418 * 25-26 ProtocolVersion client_version; (ignored)
4419 * 27-58 Random random; (ignored)
4420 * 59-xx SessionID session_id; 1 byte len + sid_len content
4421 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4422 * ...
4423 *
4424 * Minimum length is 61 bytes.
4425 */
4426 if( in_len < 61 ||
4427 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4428 in[3] != 0 || in[4] != 0 ||
4429 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4430 {
4431 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4432 }
4433
4434 sid_len = in[59];
4435 if( sid_len > in_len - 61 )
4436 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4437
4438 cookie_len = in[60 + sid_len];
4439 if( cookie_len > in_len - 60 )
4440 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4441
4442 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4443 cli_id, cli_id_len ) == 0 )
4444 {
4445 /* Valid cookie */
4446 return( 0 );
4447 }
4448
4449 /*
4450 * If we get here, we've got an invalid cookie, let's prepare HVR.
4451 *
4452 * 0-0 ContentType type; copied
4453 * 1-2 ProtocolVersion version; copied
4454 * 3-4 uint16 epoch; copied
4455 * 5-10 uint48 sequence_number; copied
4456 * 11-12 uint16 length; olen - 13
4457 *
4458 * 13-13 HandshakeType msg_type; hello_verify_request
4459 * 14-16 uint24 length; olen - 25
4460 * 17-18 uint16 message_seq; copied
4461 * 19-21 uint24 fragment_offset; copied
4462 * 22-24 uint24 fragment_length; olen - 25
4463 *
4464 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4465 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4466 *
4467 * Minimum length is 28.
4468 */
4469 if( buf_len < 28 )
4470 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4471
4472 /* Copy most fields and adapt others */
4473 memcpy( obuf, in, 25 );
4474 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4475 obuf[25] = 0xfe;
4476 obuf[26] = 0xff;
4477
4478 /* Generate and write actual cookie */
4479 p = obuf + 28;
4480 if( f_cookie_write( p_cookie,
4481 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4482 {
4483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4484 }
4485
4486 *olen = p - obuf;
4487
4488 /* Go back and fill length fields */
4489 obuf[27] = (unsigned char)( *olen - 28 );
4490
4491 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4492 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4493 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4494
4495 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4496 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4497
4498 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4499}
4500
4501/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004502 * Handle possible client reconnect with the same UDP quadruplet
4503 * (RFC 6347 Section 4.2.8).
4504 *
4505 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4506 * that looks like a ClientHello.
4507 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508 * - if the input looks like a ClientHello without cookies,
4509 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004511 * - if the input looks like a ClientHello with a valid cookie,
4512 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004513 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004514 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004516 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004517 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4518 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004519 */
4520static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4521{
4522 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004524
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004525 ret = ssl_check_dtls_clihlo_cookie(
4526 ssl->conf->f_cookie_write,
4527 ssl->conf->f_cookie_check,
4528 ssl->conf->p_cookie,
4529 ssl->cli_id, ssl->cli_id_len,
4530 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004531 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004532
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004533 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4534
4535 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004536 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004537 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004538 * If the error is permanent we'll catch it later,
4539 * if it's not, then hopefully it'll work next time. */
4540 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4541
4542 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 }
4544
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 /* Got a valid cookie, partially reset context */
4548 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4549 {
4550 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4551 return( ret );
4552 }
4553
4554 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555 }
4556
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004557 return( ret );
4558}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004559#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004560
Hanno Becker46483f12019-05-03 13:25:54 +01004561static int ssl_check_record_type( uint8_t record_type )
4562{
4563 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4564 record_type != MBEDTLS_SSL_MSG_ALERT &&
4565 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4566 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4567 {
4568 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4569 }
4570
4571 return( 0 );
4572}
4573
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004574/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004575 * ContentType type;
4576 * ProtocolVersion version;
4577 * uint16 epoch; // DTLS only
4578 * uint48 sequence_number; // DTLS only
4579 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004580 *
4581 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004582 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004583 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4584 *
4585 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004586 * 1. proceed with the record if this function returns 0
4587 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4588 * 3. return CLIENT_RECONNECT if this function return that value
4589 * 4. drop the whole datagram if this function returns anything else.
4590 * Point 2 is needed when the peer is resending, and we have already received
4591 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004595 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004597
Hanno Becker8b09b732019-05-08 12:03:28 +01004598 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004599
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004601 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004603 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004605 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004606 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4607 ssl->conf->cid_len != 0 )
4608 {
4609 /* Shift pointers to account for record header including CID
4610 * struct {
4611 * ContentType special_type = tls12_cid;
4612 * ProtocolVersion version;
4613 * uint16 epoch;
4614 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004615 * opaque cid[cid_length]; // Additional field compared to
4616 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004617 * uint16 length;
4618 * opaque enc_content[DTLSCiphertext.length];
4619 * } DTLSCiphertext;
4620 */
4621
4622 /* So far, we only support static CID lengths
4623 * fixed in the configuration. */
4624 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4625 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4626 }
4627 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004628#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004629 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004632
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004633#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004634 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004635 * Section 4.1.2.7, that is, send alert only with TLS */
4636 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4637 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004638 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4639 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004640 }
4641#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004644 }
4645
4646 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004647 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004653 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Hanno Becker8b09b732019-05-08 12:03:28 +01004659 /* Now that the total length of the record header is known, ensure
4660 * that the current datagram is large enough to hold it.
4661 * This would fail, for example, if we received a datagram of
4662 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4663 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4664 if( ret != 0 )
4665 {
4666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4667 return( ret );
4668 }
4669 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4670
4671 /* Parse and validate record length
4672 * This must happen after the CID parsing because
4673 * its position in the record header depends on
4674 * the presence of a CID. */
4675
4676 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004677 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004678 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4681 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004682 }
4683
Hanno Becker8b09b732019-05-08 12:03:28 +01004684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004685 "version = [%d:%d], msglen = %d",
4686 ssl->in_msgtype,
4687 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004688
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004689 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004690 * DTLS-related tests.
4691 * Check epoch before checking length constraint because
4692 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4693 * message gets duplicated before the corresponding Finished message,
4694 * the second ChangeCipherSpec should be discarded because it belongs
4695 * to an old epoch, but not because its length is shorter than
4696 * the minimum record length for packets using the new record transform.
4697 * Note that these two kinds of failures are handled differently,
4698 * as an unexpected record is silently skipped but an invalid
4699 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004700 */
4701#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004702 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004703 {
4704 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4705
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004706 /* Check epoch (and sequence number) with DTLS */
4707 if( rec_epoch != ssl->in_epoch )
4708 {
4709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4710 "expected %d, received %d",
4711 ssl->in_epoch, rec_epoch ) );
4712
4713#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4714 /*
4715 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4716 * access the first byte of record content (handshake type), as we
4717 * have an active transform (possibly iv_len != 0), so use the
4718 * fact that the record header len is 13 instead.
4719 */
4720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4721 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4722 rec_epoch == 0 &&
4723 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4724 ssl->in_left > 13 &&
4725 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4726 {
4727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4728 "from the same port" ) );
4729 return( ssl_handle_possible_reconnect( ssl ) );
4730 }
4731 else
4732#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004733 {
4734 /* Consider buffering the record. */
4735 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4736 {
4737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4738 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4739 }
4740
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004741 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004742 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004743 }
4744
4745#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4746 /* Replay detection only works for the current epoch */
4747 if( rec_epoch == ssl->in_epoch &&
4748 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4749 {
4750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4751 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4752 }
4753#endif
4754 }
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
Hanno Becker52c6dc62017-05-26 16:07:36 +01004757
4758 /* Check length against bounds of the current transform and version */
4759 if( ssl->transform_in == NULL )
4760 {
4761 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004762 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4765 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4766 }
4767 }
4768 else
4769 {
4770 if( ssl->in_msglen < ssl->transform_in->minlen )
4771 {
4772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4773 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4774 }
4775
4776#if defined(MBEDTLS_SSL_PROTO_SSL3)
4777 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004778 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4781 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4782 }
4783#endif
4784#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4785 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4786 /*
4787 * TLS encrypted messages can have up to 256 bytes of padding
4788 */
4789 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4790 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004791 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004792 {
4793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4794 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4795 }
4796#endif
4797 }
4798
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004799 return( 0 );
4800}
Paul Bakker5121ce52009-01-03 21:22:43 +00004801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802/*
4803 * If applicable, decrypt (and decompress) record content
4804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806{
4807 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004810 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4813 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 ret = mbedtls_ssl_hw_record_read( ssl );
4818 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4821 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004822 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004823
4824 if( ret == 0 )
4825 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004828 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004830 mbedtls_record rec;
4831
4832 rec.buf = ssl->in_iv;
4833 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4834 - ( ssl->in_iv - ssl->in_buf );
4835 rec.data_len = ssl->in_msglen;
4836 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004837#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004838 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004839 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004841
4842 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4843 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4844 ssl->conf->transport, rec.ver );
4845 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004846 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4847 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004850
Hanno Beckera5a2b082019-05-15 14:03:01 +01004851#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004852 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4853 ssl->conf->ignore_unexpected_cid
4854 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4855 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004857 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004858 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004859#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004860
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 return( ret );
4862 }
4863
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004864 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004865 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004866 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4867 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004870 /* The record content type may change during decryption,
4871 * so re-read it. */
4872 ssl->in_msgtype = rec.type;
4873 /* Also update the input buffer, because unfortunately
4874 * the server-side ssl_parse_client_hello() reparses the
4875 * record header when receiving a ClientHello initiating
4876 * a renegotiation. */
4877 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004878 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004879 ssl->in_msglen = rec.data_len;
4880 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4881 ssl->in_len[1] = (unsigned char)( rec.data_len );
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4884 ssl->in_msg, ssl->in_msglen );
4885
Hanno Beckera5a2b082019-05-15 14:03:01 +01004886#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004887 /* We have already checked the record content type
4888 * in ssl_parse_record_header(), failing or silently
4889 * dropping the record in the case of an unknown type.
4890 *
4891 * Since with the use of CIDs, the record content type
4892 * might change during decryption, re-check the record
4893 * content type, but treat a failure as fatal this time. */
4894 if( ssl_check_record_type( ssl->in_msgtype ) )
4895 {
4896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4897 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4898 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004900
Angus Grattond8213d02016-05-25 20:56:48 +10004901 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004906 else if( ssl->in_msglen == 0 )
4907 {
4908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4910 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4911 {
4912 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4915 }
4916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4917
4918 ssl->nb_zero++;
4919
4920 /*
4921 * Three or more empty messages may be a DoS attack
4922 * (excessive CPU consumption).
4923 */
4924 if( ssl->nb_zero > 3 )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004927 "messages, possible DoS attack" ) );
4928 /* Treat the records as if they were not properly authenticated,
4929 * thereby failing the connection if we see more than allowed
4930 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004931 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4932 }
4933 }
4934 else
4935 ssl->nb_zero = 0;
4936
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4938#if defined(MBEDTLS_SSL_PROTO_TLS)
4939 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004940 {
4941 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004942 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004943 if( ++ssl->in_ctr[i - 1] != 0 )
4944 break;
4945
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004946 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004947 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004948 {
4949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4950 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4951 }
4952 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004953#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004954
Paul Bakker5121ce52009-01-03 21:22:43 +00004955 }
4956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004958 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004959 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004960 {
4961 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004964 return( ret );
4965 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004966 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004970 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004972 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004973 }
4974#endif
4975
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004976 return( 0 );
4977}
4978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004980
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004981/*
4982 * Read a record.
4983 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004984 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4985 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4986 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004987 */
Hanno Becker1097b342018-08-15 14:09:41 +01004988
4989/* Helper functions for mbedtls_ssl_read_record(). */
4990static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004991static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4992static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004993
Hanno Becker327c93b2018-08-15 13:56:18 +01004994int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004995 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004996{
4997 int ret;
4998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005000
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005001 if( ssl->keep_current_message == 0 )
5002 {
5003 do {
Simon Butcher99000142016-10-13 17:21:01 +01005004
Hanno Becker26994592018-08-15 14:14:59 +01005005 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005006 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005007 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005008
Hanno Beckere74d5562018-08-15 14:26:08 +01005009 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005010 {
Hanno Becker40f50842018-08-15 14:48:01 +01005011#if defined(MBEDTLS_SSL_PROTO_DTLS)
5012 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005013
Hanno Becker40f50842018-08-15 14:48:01 +01005014 /* We only check for buffered messages if the
5015 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005016 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005017 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005018 {
Hanno Becker40f50842018-08-15 14:48:01 +01005019 if( ssl_load_buffered_message( ssl ) == 0 )
5020 have_buffered = 1;
5021 }
5022
5023 if( have_buffered == 0 )
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025 {
5026 ret = ssl_get_next_record( ssl );
5027 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5028 continue;
5029
5030 if( ret != 0 )
5031 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005032 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005033 return( ret );
5034 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005035 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005036 }
5037
5038 ret = mbedtls_ssl_handle_message_type( ssl );
5039
Hanno Becker40f50842018-08-15 14:48:01 +01005040#if defined(MBEDTLS_SSL_PROTO_DTLS)
5041 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5042 {
5043 /* Buffer future message */
5044 ret = ssl_buffer_message( ssl );
5045 if( ret != 0 )
5046 return( ret );
5047
5048 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5049 }
5050#endif /* MBEDTLS_SSL_PROTO_DTLS */
5051
Hanno Becker90333da2017-10-10 11:27:13 +01005052 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5053 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005054
5055 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005056 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005057 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005058 return( ret );
5059 }
5060
Hanno Becker327c93b2018-08-15 13:56:18 +01005061 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005062 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005063 {
5064 mbedtls_ssl_update_handshake_status( ssl );
5065 }
Simon Butcher99000142016-10-13 17:21:01 +01005066 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067 else
Simon Butcher99000142016-10-13 17:21:01 +01005068 {
Hanno Becker02f59072018-08-15 14:00:24 +01005069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005070 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005071 }
5072
5073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5074
5075 return( 0 );
5076}
5077
Hanno Becker40f50842018-08-15 14:48:01 +01005078#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005079static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005080{
Hanno Becker40f50842018-08-15 14:48:01 +01005081 if( ssl->in_left > ssl->next_record_offset )
5082 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005083
Hanno Becker40f50842018-08-15 14:48:01 +01005084 return( 0 );
5085}
5086
5087static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5088{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005090 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005091 int ret = 0;
5092
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 if( hs == NULL )
5094 return( -1 );
5095
Hanno Beckere00ae372018-08-20 09:39:42 +01005096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5097
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5099 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5100 {
5101 /* Check if we have seen a ChangeCipherSpec before.
5102 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005103 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 {
5105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5106 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005107 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005108 }
5109
Hanno Becker39b8bc92018-08-28 17:17:13 +01005110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5112 ssl->in_msglen = 1;
5113 ssl->in_msg[0] = 1;
5114
5115 /* As long as they are equal, the exact value doesn't matter. */
5116 ssl->in_left = 0;
5117 ssl->next_record_offset = 0;
5118
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005119 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 goto exit;
5121 }
Hanno Becker37f95322018-08-16 13:55:32 +01005122
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005124 /* Debug only */
5125 {
5126 unsigned offset;
5127 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5128 {
5129 hs_buf = &hs->buffering.hs[offset];
5130 if( hs_buf->is_valid == 1 )
5131 {
5132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5133 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005134 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005135 }
5136 }
5137 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005138#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005139
5140 /* Check if we have buffered and/or fully reassembled the
5141 * next handshake message. */
5142 hs_buf = &hs->buffering.hs[0];
5143 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5144 {
5145 /* Synthesize a record containing the buffered HS message. */
5146 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5147 ( hs_buf->data[2] << 8 ) |
5148 hs_buf->data[3];
5149
5150 /* Double-check that we haven't accidentally buffered
5151 * a message that doesn't fit into the input buffer. */
5152 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5153 {
5154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5155 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5156 }
5157
5158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5159 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5160 hs_buf->data, msg_len + 12 );
5161
5162 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5163 ssl->in_hslen = msg_len + 12;
5164 ssl->in_msglen = msg_len + 12;
5165 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5166
5167 ret = 0;
5168 goto exit;
5169 }
5170 else
5171 {
5172 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5173 hs->in_msg_seq ) );
5174 }
5175
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005176 ret = -1;
5177
5178exit:
5179
5180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5181 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005182}
5183
Hanno Beckera02b0b42018-08-21 17:20:27 +01005184static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5185 size_t desired )
5186{
5187 int offset;
5188 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5190 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191
Hanno Becker01315ea2018-08-21 17:22:17 +01005192 /* Get rid of future records epoch first, if such exist. */
5193 ssl_free_buffered_record( ssl );
5194
5195 /* Check if we have enough space available now. */
5196 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5197 hs->buffering.total_bytes_buffered ) )
5198 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005200 return( 0 );
5201 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005202
Hanno Becker4f432ad2018-08-28 10:02:32 +01005203 /* We don't have enough space to buffer the next expected handshake
5204 * message. Remove buffers used for future messages to gain space,
5205 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005206 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5207 offset >= 0; offset-- )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5210 offset ) );
5211
Hanno Beckerb309b922018-08-23 13:18:05 +01005212 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005213
5214 /* Check if we have enough space available now. */
5215 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5216 hs->buffering.total_bytes_buffered ) )
5217 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005219 return( 0 );
5220 }
5221 }
5222
5223 return( -1 );
5224}
5225
Hanno Becker40f50842018-08-15 14:48:01 +01005226static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5227{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005228 int ret = 0;
5229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5230
5231 if( hs == NULL )
5232 return( 0 );
5233
5234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5235
5236 switch( ssl->in_msgtype )
5237 {
5238 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005240
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005241 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005242 break;
5243
5244 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005245 {
5246 unsigned recv_msg_seq_offset;
5247 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5248 mbedtls_ssl_hs_buffer *hs_buf;
5249 size_t msg_len = ssl->in_hslen - 12;
5250
5251 /* We should never receive an old handshake
5252 * message - double-check nonetheless. */
5253 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5254 {
5255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5257 }
5258
5259 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5260 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5261 {
5262 /* Silently ignore -- message too far in the future */
5263 MBEDTLS_SSL_DEBUG_MSG( 2,
5264 ( "Ignore future HS message with sequence number %u, "
5265 "buffering window %u - %u",
5266 recv_msg_seq, ssl->handshake->in_msg_seq,
5267 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5268
5269 goto exit;
5270 }
5271
5272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5273 recv_msg_seq, recv_msg_seq_offset ) );
5274
5275 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5276
5277 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005278 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005279 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005280 size_t reassembly_buf_sz;
5281
Hanno Becker37f95322018-08-16 13:55:32 +01005282 hs_buf->is_fragmented =
5283 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5284
5285 /* We copy the message back into the input buffer
5286 * after reassembly, so check that it's not too large.
5287 * This is an implementation-specific limitation
5288 * and not one from the standard, hence it is not
5289 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005290 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005291 {
5292 /* Ignore message */
5293 goto exit;
5294 }
5295
Hanno Beckere0b150f2018-08-21 15:51:03 +01005296 /* Check if we have enough space to buffer the message. */
5297 if( hs->buffering.total_bytes_buffered >
5298 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5302 }
5303
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005304 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5305 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005306
5307 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5308 hs->buffering.total_bytes_buffered ) )
5309 {
5310 if( recv_msg_seq_offset > 0 )
5311 {
5312 /* If we can't buffer a future message because
5313 * of space limitations -- ignore. */
5314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5315 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5316 (unsigned) hs->buffering.total_bytes_buffered ) );
5317 goto exit;
5318 }
Hanno Beckere1801392018-08-21 16:51:05 +01005319 else
5320 {
5321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5322 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5323 (unsigned) hs->buffering.total_bytes_buffered ) );
5324 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005325
Hanno Beckera02b0b42018-08-21 17:20:27 +01005326 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005327 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5329 (unsigned) msg_len,
5330 (unsigned) reassembly_buf_sz,
5331 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005333 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5334 goto exit;
5335 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005336 }
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5339 msg_len ) );
5340
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005341 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5342 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005343 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005344 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005345 goto exit;
5346 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005347 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005348
5349 /* Prepare final header: copy msg_type, length and message_seq,
5350 * then add standardised fragment_offset and fragment_length */
5351 memcpy( hs_buf->data, ssl->in_msg, 6 );
5352 memset( hs_buf->data + 6, 0, 3 );
5353 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5354
5355 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356
5357 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005358 }
5359 else
5360 {
5361 /* Make sure msg_type and length are consistent */
5362 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5365 /* Ignore */
5366 goto exit;
5367 }
5368 }
5369
Hanno Becker4422bbb2018-08-20 09:40:19 +01005370 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005371 {
5372 size_t frag_len, frag_off;
5373 unsigned char * const msg = hs_buf->data + 12;
5374
5375 /*
5376 * Check and copy current fragment
5377 */
5378
5379 /* Validation of header fields already done in
5380 * mbedtls_ssl_prepare_handshake_record(). */
5381 frag_off = ssl_get_hs_frag_off( ssl );
5382 frag_len = ssl_get_hs_frag_len( ssl );
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5385 frag_off, frag_len ) );
5386 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5387
5388 if( hs_buf->is_fragmented )
5389 {
5390 unsigned char * const bitmask = msg + msg_len;
5391 ssl_bitmask_set( bitmask, frag_off, frag_len );
5392 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5393 msg_len ) == 0 );
5394 }
5395 else
5396 {
5397 hs_buf->is_complete = 1;
5398 }
5399
5400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5401 hs_buf->is_complete ? "" : "not yet " ) );
5402 }
5403
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005404 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005405 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005406
5407 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005408 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005409 break;
5410 }
5411
5412exit:
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5415 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005416}
5417#endif /* MBEDTLS_SSL_PROTO_DTLS */
5418
Hanno Becker1097b342018-08-15 14:09:41 +01005419static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005420{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005421 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 * Consume last content-layer message and potentially
5423 * update in_msglen which keeps track of the contents'
5424 * consumption state.
5425 *
5426 * (1) Handshake messages:
5427 * Remove last handshake message, move content
5428 * and adapt in_msglen.
5429 *
5430 * (2) Alert messages:
5431 * Consume whole record content, in_msglen = 0.
5432 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005433 * (3) Change cipher spec:
5434 * Consume whole record content, in_msglen = 0.
5435 *
5436 * (4) Application data:
5437 * Don't do anything - the record layer provides
5438 * the application data as a stream transport
5439 * and consumes through mbedtls_ssl_read only.
5440 *
5441 */
5442
5443 /* Case (1): Handshake messages */
5444 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005445 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005446 /* Hard assertion to be sure that no application data
5447 * is in flight, as corrupting ssl->in_msglen during
5448 * ssl->in_offt != NULL is fatal. */
5449 if( ssl->in_offt != NULL )
5450 {
5451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5452 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5453 }
5454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005455 /*
5456 * Get next Handshake message in the current record
5457 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005458
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005460 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005461 * current handshake content: If DTLS handshake
5462 * fragmentation is used, that's the fragment
5463 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005464 * size here is faulty and should be changed at
5465 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 * (2) While it doesn't seem to cause problems, one
5467 * has to be very careful not to assume that in_hslen
5468 * is always <= in_msglen in a sensible communication.
5469 * Again, it's wrong for DTLS handshake fragmentation.
5470 * The following check is therefore mandatory, and
5471 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005472 * Additionally, ssl->in_hslen might be arbitrarily out of
5473 * bounds after handling a DTLS message with an unexpected
5474 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 */
5476 if( ssl->in_hslen < ssl->in_msglen )
5477 {
5478 ssl->in_msglen -= ssl->in_hslen;
5479 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5480 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005481
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5483 ssl->in_msg, ssl->in_msglen );
5484 }
5485 else
5486 {
5487 ssl->in_msglen = 0;
5488 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005489
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 ssl->in_hslen = 0;
5491 }
5492 /* Case (4): Application data */
5493 else if( ssl->in_offt != NULL )
5494 {
5495 return( 0 );
5496 }
5497 /* Everything else (CCS & Alerts) */
5498 else
5499 {
5500 ssl->in_msglen = 0;
5501 }
5502
Hanno Becker1097b342018-08-15 14:09:41 +01005503 return( 0 );
5504}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505
Hanno Beckere74d5562018-08-15 14:26:08 +01005506static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5507{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005509 return( 1 );
5510
5511 return( 0 );
5512}
5513
Hanno Becker5f066e72018-08-16 14:56:31 +01005514#if defined(MBEDTLS_SSL_PROTO_DTLS)
5515
5516static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5517{
5518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5519 if( hs == NULL )
5520 return;
5521
Hanno Becker01315ea2018-08-21 17:22:17 +01005522 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005523 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005524 hs->buffering.total_bytes_buffered -=
5525 hs->buffering.future_record.len;
5526
5527 mbedtls_free( hs->buffering.future_record.data );
5528 hs->buffering.future_record.data = NULL;
5529 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005530}
5531
5532static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5533{
5534 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5535 unsigned char * rec;
5536 size_t rec_len;
5537 unsigned rec_epoch;
5538
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005539 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005540 return( 0 );
5541
5542 if( hs == NULL )
5543 return( 0 );
5544
Hanno Becker5f066e72018-08-16 14:56:31 +01005545 rec = hs->buffering.future_record.data;
5546 rec_len = hs->buffering.future_record.len;
5547 rec_epoch = hs->buffering.future_record.epoch;
5548
5549 if( rec == NULL )
5550 return( 0 );
5551
Hanno Becker4cb782d2018-08-20 11:19:05 +01005552 /* Only consider loading future records if the
5553 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005554 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005555 return( 0 );
5556
Hanno Becker5f066e72018-08-16 14:56:31 +01005557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5558
5559 if( rec_epoch != ssl->in_epoch )
5560 {
5561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5562 goto exit;
5563 }
5564
5565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5566
5567 /* Double-check that the record is not too large */
5568 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5569 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5570 {
5571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5573 }
5574
5575 memcpy( ssl->in_hdr, rec, rec_len );
5576 ssl->in_left = rec_len;
5577 ssl->next_record_offset = 0;
5578
5579 ssl_free_buffered_record( ssl );
5580
5581exit:
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5583 return( 0 );
5584}
5585
5586static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5587{
5588 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5589 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005591
5592 /* Don't buffer future records outside handshakes. */
5593 if( hs == NULL )
5594 return( 0 );
5595
5596 /* Only buffer handshake records (we are only interested
5597 * in Finished messages). */
5598 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5599 return( 0 );
5600
5601 /* Don't buffer more than one future epoch record. */
5602 if( hs->buffering.future_record.data != NULL )
5603 return( 0 );
5604
Hanno Becker01315ea2018-08-21 17:22:17 +01005605 /* Don't buffer record if there's not enough buffering space remaining. */
5606 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5607 hs->buffering.total_bytes_buffered ) )
5608 {
5609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5610 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5611 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005612 return( 0 );
5613 }
5614
Hanno Becker5f066e72018-08-16 14:56:31 +01005615 /* Buffer record */
5616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5617 ssl->in_epoch + 1 ) );
5618 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5619 rec_hdr_len + ssl->in_msglen );
5620
5621 /* ssl_parse_record_header() only considers records
5622 * of the next epoch as candidates for buffering. */
5623 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005624 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005625
5626 hs->buffering.future_record.data =
5627 mbedtls_calloc( 1, hs->buffering.future_record.len );
5628 if( hs->buffering.future_record.data == NULL )
5629 {
5630 /* If we run out of RAM trying to buffer a
5631 * record from the next epoch, just ignore. */
5632 return( 0 );
5633 }
5634
Hanno Becker01315ea2018-08-21 17:22:17 +01005635 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005636
Hanno Becker01315ea2018-08-21 17:22:17 +01005637 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005638 return( 0 );
5639}
5640
5641#endif /* MBEDTLS_SSL_PROTO_DTLS */
5642
Hanno Beckere74d5562018-08-15 14:26:08 +01005643static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005644{
5645 int ret;
5646
Hanno Becker5f066e72018-08-16 14:56:31 +01005647#if defined(MBEDTLS_SSL_PROTO_DTLS)
5648 /* We might have buffered a future record; if so,
5649 * and if the epoch matches now, load it.
5650 * On success, this call will set ssl->in_left to
5651 * the length of the buffered record, so that
5652 * the calls to ssl_fetch_input() below will
5653 * essentially be no-ops. */
5654 ret = ssl_load_buffered_record( ssl );
5655 if( ret != 0 )
5656 return( ret );
5657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005658
Hanno Becker8b09b732019-05-08 12:03:28 +01005659 /* Reset in pointers to default state for TLS/DTLS records,
5660 * assuming no CID and no offset between record content and
5661 * record plaintext. */
5662 ssl_update_in_pointers( ssl );
5663
5664 /* Ensure that we have enough space available for the default form
5665 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5666 * with no space for CIDs counted in). */
5667 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5668 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005671 return( ret );
5672 }
5673
5674 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005677 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005678 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005679 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005680 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5681 {
5682 ret = ssl_buffer_future_record( ssl );
5683 if( ret != 0 )
5684 return( ret );
5685
5686 /* Fall through to handling of unexpected records */
5687 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5688 }
5689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005690 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5691 {
5692 /* Skip unexpected record (but not whole datagram) */
5693 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005694 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5697 "(header)" ) );
5698 }
5699 else
5700 {
5701 /* Skip invalid record and the rest of the datagram */
5702 ssl->next_record_offset = 0;
5703 ssl->in_left = 0;
5704
5705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5706 "(header)" ) );
5707 }
5708
5709 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005710 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005711 }
5712#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005714 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715
5716 /*
5717 * Read and optionally decrypt the message contents
5718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005719 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005720 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723 return( ret );
5724 }
5725
5726 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005729 {
Hanno Becker43395762019-05-03 14:46:38 +01005730 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005731 if( ssl->next_record_offset < ssl->in_left )
5732 {
5733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5734 }
5735 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005736 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005737#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005738#if defined(MBEDTLS_SSL_PROTO_TLS)
5739 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005741 }
5742#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005743
5744 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005747 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 {
5749 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005750 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005752 /* Except when waiting for Finished as a bad mac here
5753 * probably means something went wrong in the handshake
5754 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5755 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5756 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5757 {
5758#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5759 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5760 {
5761 mbedtls_ssl_send_alert_message( ssl,
5762 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5763 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5764 }
5765#endif
5766 return( ret );
5767 }
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005770 if( ssl->conf->badmac_limit != 0 &&
5771 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5774 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005775 }
5776#endif
5777
Hanno Becker4a810fb2017-05-24 16:27:30 +01005778 /* As above, invalid records cause
5779 * dismissal of the whole datagram. */
5780
5781 ssl->next_record_offset = 0;
5782 ssl->in_left = 0;
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005785 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790 MBEDTLS_SSL_TRANSPORT_ELSE
5791#endif /* MBEDTLS_SSL_PROTO_DTLS */
5792#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005793 {
5794 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5796 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 mbedtls_ssl_send_alert_message( ssl,
5799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005801 }
5802#endif
5803 return( ret );
5804 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005805#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005806 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005807
Simon Butcher99000142016-10-13 17:21:01 +01005808 return( 0 );
5809}
5810
5811int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5812{
5813 int ret;
5814
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005815 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005816 * Handle particular types of records
5817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005818 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005819 {
Simon Butcher99000142016-10-13 17:21:01 +01005820 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5821 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005822 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005824 }
5825
Hanno Beckere678eaa2018-08-21 14:57:46 +01005826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005827 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005828 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005829 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5831 ssl->in_msglen ) );
5832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005833 }
5834
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msg[0] != 1 )
5836 {
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5838 ssl->in_msg[0] ) );
5839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5840 }
5841
5842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005843 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005844 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5845 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5846 {
5847 if( ssl->handshake == NULL )
5848 {
5849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5850 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5851 }
5852
5853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5854 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5855 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005856#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005859 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005861 if( ssl->in_msglen != 2 )
5862 {
5863 /* Note: Standard allows for more than one 2 byte alert
5864 to be packed in a single message, but Mbed TLS doesn't
5865 currently support this. */
5866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5867 ssl->in_msglen ) );
5868 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5869 }
5870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005872 ssl->in_msg[0], ssl->in_msg[1] ) );
5873
5874 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005875 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005880 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005881 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005882 }
5883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5885 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5888 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005890
5891#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5892 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5893 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5894 {
Hanno Becker90333da2017-10-10 11:27:13 +01005895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005896 /* Will be handled when trying to parse ServerHello */
5897 return( 0 );
5898 }
5899#endif
5900
5901#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5903 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5904 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5905 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5906 {
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5908 /* Will be handled in mbedtls_ssl_parse_certificate() */
5909 return( 0 );
5910 }
5911#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5912
5913 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005914 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 }
5916
Hanno Beckerc76c6192017-06-06 10:03:17 +01005917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005919 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005920 /* Drop unexpected ApplicationData records,
5921 * except at the beginning of renegotiations */
5922 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5923 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5924#if defined(MBEDTLS_SSL_RENEGOTIATION)
5925 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5926 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005927#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005928 )
5929 {
5930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5931 return( MBEDTLS_ERR_SSL_NON_FATAL );
5932 }
5933
5934 if( ssl->handshake != NULL &&
5935 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5936 {
5937 ssl_handshake_wrapup_free_hs_transform( ssl );
5938 }
5939 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005940#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005941
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 return( 0 );
5943}
5944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005945int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005946{
5947 int ret;
5948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5950 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005952 {
5953 return( ret );
5954 }
5955
5956 return( 0 );
5957}
5958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005959int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005960 unsigned char level,
5961 unsigned char message )
5962{
5963 int ret;
5964
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005965 if( ssl == NULL || ssl->conf == NULL )
5966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005972 ssl->out_msglen = 2;
5973 ssl->out_msg[0] = level;
5974 ssl->out_msg[1] = message;
5975
Hanno Becker67bc7c32018-08-06 11:33:50 +01005976 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005979 return( ret );
5980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005982
5983 return( 0 );
5984}
5985
Paul Bakker5121ce52009-01-03 21:22:43 +00005986/*
5987 * Handshake functions
5988 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005989#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005990/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005991int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005992{
Hanno Becker8759e162017-12-27 21:34:08 +00005993 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005996
Hanno Becker5097cba2019-02-05 13:36:46 +00005997 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006000 ssl->state++;
6001 return( 0 );
6002 }
6003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6005 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006}
6007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006009{
Hanno Becker8759e162017-12-27 21:34:08 +00006010 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013
Hanno Becker5097cba2019-02-05 13:36:46 +00006014 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006017 ssl->state++;
6018 return( 0 );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006023}
Gilles Peskinef9828522017-05-03 12:28:43 +02006024
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006025#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006026/* Some certificate support -> implement write and parse */
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006031 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006033 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006036
Hanno Becker5097cba2019-02-05 13:36:46 +00006037 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006040 ssl->state++;
6041 return( 0 );
6042 }
6043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006045 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006046 {
6047 if( ssl->client_auth == 0 )
6048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006050 ssl->state++;
6051 return( 0 );
6052 }
6053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 /*
6056 * If using SSLv3 and got no cert, send an Alert message
6057 * (otherwise an empty Certificate message will be sent).
6058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6060 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
6062 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6064 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6065 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 goto write_msg;
6069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072#endif /* MBEDTLS_SSL_CLI_C */
6073#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006074 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6079 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006080 }
6081 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 /*
6087 * 0 . 0 handshake type
6088 * 1 . 3 handshake length
6089 * 4 . 6 length of all certs
6090 * 7 . 9 length of cert. 1
6091 * 10 . n-1 peer certificate
6092 * n . n+2 length of cert. 2
6093 * n+3 . ... upper level cert, etc.
6094 */
6095 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Paul Bakker29087132010-03-21 21:03:34 +00006098 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
6100 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006101 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006104 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 }
6107
6108 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6109 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6110 ssl->out_msg[i + 2] = (unsigned char)( n );
6111
6112 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6113 i += n; crt = crt->next;
6114 }
6115
6116 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6117 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6118 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6119
6120 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6122 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006123
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006125write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006127
6128 ssl->state++;
6129
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006130 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 return( ret );
6134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006137
Paul Bakkered27a042013-04-18 22:46:23 +02006138 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139}
6140
Hanno Becker285ff0c2019-01-31 07:44:03 +00006141#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006142static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6143 unsigned char *crt_buf,
6144 size_t crt_buf_len )
6145{
6146 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6147
6148 if( peer_crt == NULL )
6149 return( -1 );
6150
6151 if( peer_crt->raw.len != crt_buf_len )
6152 return( -1 );
6153
Hanno Becker68b856d2019-02-08 14:00:04 +00006154 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006155}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006156#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006157
Hanno Becker22141592019-02-05 12:38:15 +00006158static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6159{
6160 if( session->peer_cert != NULL )
6161 {
6162 mbedtls_x509_crt_free( session->peer_cert );
6163 mbedtls_free( session->peer_cert );
6164 session->peer_cert = NULL;
6165 }
Hanno Becker9fb6e2e2019-02-05 17:00:50 +00006166
6167#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6168 if( session->peer_cert_digest != NULL )
6169 {
6170 /* Zeroization is not necessary. */
6171 mbedtls_free( session->peer_cert_digest );
6172 session->peer_cert_digest = NULL;
6173 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6174 session->peer_cert_digest_len = 0;
6175 }
6176#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker22141592019-02-05 12:38:15 +00006177}
6178
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006179/*
6180 * Once the certificate message is read, parse it into a cert chain and
6181 * perform basic checks, but leave actual verification to the caller
6182 */
Hanno Becker35e41772019-02-05 15:37:23 +00006183static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6184 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006185{
Hanno Becker35e41772019-02-05 15:37:23 +00006186 int ret;
6187#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6188 int crt_cnt=0;
6189#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006190 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006191 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006196 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6197 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 }
6200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6202 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006205 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6206 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006207 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006208 }
6209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006211
Paul Bakker5121ce52009-01-03 21:22:43 +00006212 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006214 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006215 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006216
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006217 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006221 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6222 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 }
6225
Hanno Becker33c3dc82019-01-30 14:46:46 +00006226 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6227 i += 3;
6228
Hanno Becker33c3dc82019-01-30 14:46:46 +00006229 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 while( i < ssl->in_hslen )
6231 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006232 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006233 if ( i + 3 > ssl->in_hslen ) {
6234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006235 mbedtls_ssl_send_alert_message( ssl,
6236 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6237 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006238 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6239 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006240 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6241 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 if( ssl->in_msg[i] != 0 )
6243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006245 mbedtls_ssl_send_alert_message( ssl,
6246 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6247 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006248 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006249 }
6250
Hanno Becker33c3dc82019-01-30 14:46:46 +00006251 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6253 | (unsigned int) ssl->in_msg[i + 2];
6254 i += 3;
6255
6256 if( n < 128 || i + n > ssl->in_hslen )
6257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006259 mbedtls_ssl_send_alert_message( ssl,
6260 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6261 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006263 }
6264
Hanno Becker33c3dc82019-01-30 14:46:46 +00006265 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006266#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6267 if( crt_cnt++ == 0 &&
6268 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6269 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006271 /* During client-side renegotiation, check that the server's
6272 * end-CRTs hasn't changed compared to the initial handshake,
6273 * mitigating the triple handshake attack. On success, reuse
6274 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6276 if( ssl_check_peer_crt_unchanged( ssl,
6277 &ssl->in_msg[i],
6278 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006279 {
Hanno Becker35e41772019-02-05 15:37:23 +00006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6281 mbedtls_ssl_send_alert_message( ssl,
6282 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6283 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6284 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006285 }
Hanno Becker35e41772019-02-05 15:37:23 +00006286
6287 /* Now we can safely free the original chain. */
6288 ssl_clear_peer_cert( ssl->session );
6289 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006290#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6291
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006293 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006294 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006296 case 0: /*ok*/
6297 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6298 /* Ignore certificate with an unknown algorithm: maybe a
6299 prior certificate was already trusted. */
6300 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006301
Hanno Becker33c3dc82019-01-30 14:46:46 +00006302 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6303 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6304 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006305
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6307 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6308 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006309
Hanno Becker33c3dc82019-01-30 14:46:46 +00006310 default:
6311 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6312 crt_parse_der_failed:
6313 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6314 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6315 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006316 }
6317
6318 i += n;
6319 }
6320
Hanno Becker35e41772019-02-05 15:37:23 +00006321 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006322 return( 0 );
6323}
6324
Hanno Beckerb8a08572019-02-05 12:49:06 +00006325#if defined(MBEDTLS_SSL_SRV_C)
6326static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6327{
6328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6329 return( -1 );
6330
6331#if defined(MBEDTLS_SSL_PROTO_SSL3)
6332 /*
6333 * Check if the client sent an empty certificate
6334 */
6335 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6336 {
6337 if( ssl->in_msglen == 2 &&
6338 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6339 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6340 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6341 {
6342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6343 return( 0 );
6344 }
6345
6346 return( -1 );
6347 }
6348#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6349
6350#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6351 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6352 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6353 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6354 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6355 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6356 {
6357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6358 return( 0 );
6359 }
6360
6361 return( -1 );
6362#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6363 MBEDTLS_SSL_PROTO_TLS1_2 */
6364}
6365#endif /* MBEDTLS_SSL_SRV_C */
6366
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006367/* Check if a certificate message is expected.
6368 * Return either
6369 * - SSL_CERTIFICATE_EXPECTED, or
6370 * - SSL_CERTIFICATE_SKIP
6371 * indicating whether a Certificate message is expected or not.
6372 */
6373#define SSL_CERTIFICATE_EXPECTED 0
6374#define SSL_CERTIFICATE_SKIP 1
6375static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6376 int authmode )
6377{
6378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6379 ssl->handshake->ciphersuite_info;
6380
6381 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6382 return( SSL_CERTIFICATE_SKIP );
6383
6384#if defined(MBEDTLS_SSL_SRV_C)
6385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6386 {
6387 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6388 return( SSL_CERTIFICATE_SKIP );
6389
6390 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6391 {
6392 /* NOTE: Is it intentional that we set verify_result
6393 * to SKIP_VERIFY on server-side only? */
6394 ssl->session_negotiate->verify_result =
6395 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6396 return( SSL_CERTIFICATE_SKIP );
6397 }
6398 }
6399#endif /* MBEDTLS_SSL_SRV_C */
6400
6401 return( SSL_CERTIFICATE_EXPECTED );
6402}
6403
Hanno Becker3cf50612019-02-05 14:36:34 +00006404static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6405 int authmode,
6406 mbedtls_x509_crt *chain,
6407 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006408{
Hanno Becker613d4902019-02-05 13:11:17 +00006409 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6411 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006412 mbedtls_x509_crt *ca_chain;
6413 mbedtls_x509_crl *ca_crl;
6414
6415 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6416 return( 0 );
6417
6418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6419 if( ssl->handshake->sni_ca_chain != NULL )
6420 {
6421 ca_chain = ssl->handshake->sni_ca_chain;
6422 ca_crl = ssl->handshake->sni_ca_crl;
6423 }
6424 else
6425#endif
6426 {
6427 ca_chain = ssl->conf->ca_chain;
6428 ca_crl = ssl->conf->ca_crl;
6429 }
6430
6431 /*
6432 * Main check: verify certificate
6433 */
6434 ret = mbedtls_x509_crt_verify_restartable(
6435 chain,
6436 ca_chain, ca_crl,
6437 ssl->conf->cert_profile,
6438 ssl->hostname,
6439 &ssl->session_negotiate->verify_result,
6440 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6441
6442 if( ret != 0 )
6443 {
6444 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6445 }
6446
6447#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6448 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6449 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6450#endif
6451
6452 /*
6453 * Secondary checks: always done, but change 'ret' only if it was 0
6454 */
6455
6456#if defined(MBEDTLS_ECP_C)
6457 {
6458 const mbedtls_pk_context *pk = &chain->pk;
6459
6460 /* If certificate uses an EC key, make sure the curve is OK */
6461 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6462 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6463 {
6464 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6465
6466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6467 if( ret == 0 )
6468 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6469 }
6470 }
6471#endif /* MBEDTLS_ECP_C */
6472
6473 if( mbedtls_ssl_check_cert_usage( chain,
6474 ciphersuite_info,
6475 ! ssl->conf->endpoint,
6476 &ssl->session_negotiate->verify_result ) != 0 )
6477 {
6478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6479 if( ret == 0 )
6480 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6481 }
6482
6483 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6484 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6485 * with details encoded in the verification flags. All other kinds
6486 * of error codes, including those from the user provided f_vrfy
6487 * functions, are treated as fatal and lead to a failure of
6488 * ssl_parse_certificate even if verification was optional. */
6489 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6490 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6491 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6492 {
6493 ret = 0;
6494 }
6495
6496 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6499 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6500 }
6501
6502 if( ret != 0 )
6503 {
6504 uint8_t alert;
6505
6506 /* The certificate may have been rejected for several reasons.
6507 Pick one and send the corresponding alert. Which alert to send
6508 may be a subject of debate in some cases. */
6509 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6510 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6511 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6512 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6513 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6514 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6515 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6516 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6517 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6518 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6519 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6520 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6521 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6522 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6523 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6524 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6525 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6526 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6527 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6528 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6529 else
6530 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6531 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6532 alert );
6533 }
6534
6535#if defined(MBEDTLS_DEBUG_C)
6536 if( ssl->session_negotiate->verify_result != 0 )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6539 ssl->session_negotiate->verify_result ) );
6540 }
6541 else
6542 {
6543 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6544 }
6545#endif /* MBEDTLS_DEBUG_C */
6546
6547 return( ret );
6548}
6549
6550int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6551{
6552 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006553 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006554#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6555 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6556 ? ssl->handshake->sni_authmode
6557 : ssl->conf->authmode;
6558#else
6559 const int authmode = ssl->conf->authmode;
6560#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006561 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006562
6563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6564
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006565 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6566 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006567 {
6568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006569 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006570 }
6571
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006572#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6573 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006574 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006575 {
6576 goto crt_verify;
6577 }
6578#endif
6579
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006580 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006581 {
6582 /* mbedtls_ssl_read_record may have sent an alert already. We
6583 let it decide whether to alert. */
6584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6585 return( ret );
6586 }
6587
Hanno Beckerb8a08572019-02-05 12:49:06 +00006588#if defined(MBEDTLS_SSL_SRV_C)
6589 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6590 {
6591 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006592
6593 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006594 ret = 0;
6595 else
6596 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006597
Hanno Becker613d4902019-02-05 13:11:17 +00006598 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006599 }
6600#endif /* MBEDTLS_SSL_SRV_C */
6601
Hanno Becker35e41772019-02-05 15:37:23 +00006602 /* Clear existing peer CRT structure in case we tried to
6603 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006604 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker35e41772019-02-05 15:37:23 +00006605 ssl->session_negotiate->peer_cert =
6606 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6607 if( ssl->session_negotiate->peer_cert == NULL )
6608 {
6609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6610 sizeof( mbedtls_x509_crt ) ) );
6611 mbedtls_ssl_send_alert_message( ssl,
6612 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6613 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6614 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6615 }
6616 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Beckera46c2872019-02-05 13:08:01 +00006617
Hanno Becker35e41772019-02-05 15:37:23 +00006618 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6619 if( ret != 0 )
Hanno Beckera7c1df62019-02-05 14:35:46 +00006620 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006621
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006622#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6623 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006624 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006625
6626crt_verify:
6627 if( ssl->handshake->ecrs_enabled)
6628 rs_ctx = &ssl->handshake->ecrs_ctx;
6629#endif
6630
Hanno Becker3cf50612019-02-05 14:36:34 +00006631 ret = ssl_parse_certificate_verify( ssl, authmode,
6632 ssl->session_negotiate->peer_cert,
6633 rs_ctx );
6634 if( ret != 0 )
6635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638
Hanno Becker613d4902019-02-05 13:11:17 +00006639exit:
6640
6641 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 return( ret );
6643}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006644#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006647{
6648 int ret;
6649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006653 ssl->out_msglen = 1;
6654 ssl->out_msg[0] = 1;
6655
Paul Bakker5121ce52009-01-03 21:22:43 +00006656 ssl->state++;
6657
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006658 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006659 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 return( ret );
6662 }
6663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006665
6666 return( 0 );
6667}
6668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006670{
6671 int ret;
6672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006674
Hanno Becker327c93b2018-08-15 13:56:18 +01006675 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006678 return( ret );
6679 }
6680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006681 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006684 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6685 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006687 }
6688
Hanno Beckere678eaa2018-08-21 14:57:46 +01006689 /* CCS records are only accepted if they have length 1 and content '1',
6690 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006691
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006692 /*
6693 * Switch to our negotiated transform and session parameters for inbound
6694 * data.
6695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006697 ssl->transform_in = ssl->transform_negotiate;
6698 ssl->session_in = ssl->session_negotiate;
6699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006701 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006703#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006704 ssl_dtls_replay_reset( ssl );
6705#endif
6706
6707 /* Increment epoch */
6708 if( ++ssl->in_epoch == 0 )
6709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006711 /* This is highly unlikely to happen for legitimate reasons, so
6712 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006714 }
6715 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006716 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006717#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006718#if defined(MBEDTLS_SSL_PROTO_TLS)
6719 {
6720 memset( ssl->in_ctr, 0, 8 );
6721 }
6722#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006723
Hanno Beckerf5970a02019-05-08 09:38:41 +01006724 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6727 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006732 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6733 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006734 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006735 }
6736 }
6737#endif
6738
Paul Bakker5121ce52009-01-03 21:22:43 +00006739 ssl->state++;
6740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006742
6743 return( 0 );
6744}
6745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006746void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6747 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006748{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006749 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6752 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6753 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006754 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006755 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006757#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6758#if defined(MBEDTLS_SHA512_C)
6759 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006760 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6761 else
6762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#if defined(MBEDTLS_SHA256_C)
6764 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006765 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006766 else
6767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006771 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006772 }
Paul Bakker380da532012-04-18 16:10:25 +00006773}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006775void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006776{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6778 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006779 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6780 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6783#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006784 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006785#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006786#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006787 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006788#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006789#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006790}
6791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006793 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006794{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006795#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6796 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006797 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6798 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6801#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006802 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006803#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006805 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006808}
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6811 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6812static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006813 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006814{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006815 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6816 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006817}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006818#endif
Paul Bakker380da532012-04-18 16:10:25 +00006819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006820#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6821#if defined(MBEDTLS_SHA256_C)
6822static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006823 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006824{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006825 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006826}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006827#endif
Paul Bakker380da532012-04-18 16:10:25 +00006828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829#if defined(MBEDTLS_SHA512_C)
6830static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006831 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006832{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006833 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006834}
Paul Bakker769075d2012-11-24 11:26:46 +01006835#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006838#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006839static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006841{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006842 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006843 mbedtls_md5_context md5;
6844 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006845
Paul Bakker5121ce52009-01-03 21:22:43 +00006846 unsigned char padbuf[48];
6847 unsigned char md5sum[16];
6848 unsigned char sha1sum[20];
6849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006851 if( !session )
6852 session = ssl->session;
6853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006855
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006856 mbedtls_md5_init( &md5 );
6857 mbedtls_sha1_init( &sha1 );
6858
6859 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6860 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006861
6862 /*
6863 * SSLv3:
6864 * hash =
6865 * MD5( master + pad2 +
6866 * MD5( handshake + sender + master + pad1 ) )
6867 * + SHA1( master + pad2 +
6868 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006869 */
6870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006871#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006872 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6873 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006876#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006877 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6878 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006879#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006882 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006883
Paul Bakker1ef83d62012-04-11 12:09:53 +00006884 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006885
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006886 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6887 mbedtls_md5_update_ret( &md5, session->master, 48 );
6888 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6889 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006890
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006891 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6892 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6893 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6894 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006895
Paul Bakker1ef83d62012-04-11 12:09:53 +00006896 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006897
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006898 mbedtls_md5_starts_ret( &md5 );
6899 mbedtls_md5_update_ret( &md5, session->master, 48 );
6900 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6901 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6902 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006903
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006904 mbedtls_sha1_starts_ret( &sha1 );
6905 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6906 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6907 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6908 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006911
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006912 mbedtls_md5_free( &md5 );
6913 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006914
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006915 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6916 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6917 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006919 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006921#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006924static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006926{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006927 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006928 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006929 mbedtls_md5_context md5;
6930 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006933 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006934 if( !session )
6935 session = ssl->session;
6936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006938
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006939 mbedtls_md5_init( &md5 );
6940 mbedtls_sha1_init( &sha1 );
6941
6942 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6943 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006944
Paul Bakker1ef83d62012-04-11 12:09:53 +00006945 /*
6946 * TLSv1:
6947 * hash = PRF( master, finished_label,
6948 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6949 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006952 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6953 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006954#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006957 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6958 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006959#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006962 ? "client finished"
6963 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006964
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006965 mbedtls_md5_finish_ret( &md5, padbuf );
6966 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006967
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006968 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006969 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006972
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006973 mbedtls_md5_free( &md5 );
6974 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006975
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006976 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006982#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6983#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006984static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006985 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006986{
6987 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006988 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006989 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006990 unsigned char padbuf[32];
6991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006993 if( !session )
6994 session = ssl->session;
6995
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006996 mbedtls_sha256_init( &sha256 );
6997
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006999
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007000 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007001
7002 /*
7003 * TLSv1.2:
7004 * hash = PRF( master, finished_label,
7005 * Hash( handshake ) )[0.11]
7006 */
7007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008#if !defined(MBEDTLS_SHA256_ALT)
7009 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007010 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007011#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007014 ? "client finished"
7015 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007016
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007017 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007018
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007019 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007020 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007022 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007023
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007024 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007025
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007026 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007029}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007032#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007033static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007034 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007035{
7036 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007037 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007038 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007039 unsigned char padbuf[48];
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007042 if( !session )
7043 session = ssl->session;
7044
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007045 mbedtls_sha512_init( &sha512 );
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007048
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007049 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007050
7051 /*
7052 * TLSv1.2:
7053 * hash = PRF( master, finished_label,
7054 * Hash( handshake ) )[0.11]
7055 */
7056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007058 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7059 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007060#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007063 ? "client finished"
7064 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007065
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007066 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007067
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007068 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007069 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007072
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007073 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007074
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007075 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079#endif /* MBEDTLS_SHA512_C */
7080#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007083{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007085
7086 /*
7087 * Free our handshake params
7088 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007089 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007091 ssl->handshake = NULL;
7092
7093 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007094 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007095 */
7096 if( ssl->transform )
7097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098 mbedtls_ssl_transform_free( ssl->transform );
7099 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007100 }
7101 ssl->transform = ssl->transform_negotiate;
7102 ssl->transform_negotiate = NULL;
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007105}
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007108{
7109 int resume = ssl->handshake->resume;
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113#if defined(MBEDTLS_SSL_RENEGOTIATION)
7114 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007117 ssl->renego_records_seen = 0;
7118 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007119#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007120
7121 /*
7122 * Free the previous session and switch in the current one
7123 */
Paul Bakker0a597072012-09-25 21:55:46 +00007124 if( ssl->session )
7125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007127 /* RFC 7366 3.1: keep the EtM state */
7128 ssl->session_negotiate->encrypt_then_mac =
7129 ssl->session->encrypt_then_mac;
7130#endif
7131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132 mbedtls_ssl_session_free( ssl->session );
7133 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007134 }
7135 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007136 ssl->session_negotiate = NULL;
7137
Paul Bakker0a597072012-09-25 21:55:46 +00007138 /*
7139 * Add cache entry
7140 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007141 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007142 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007143 resume == 0 )
7144 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007145 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007147 }
Paul Bakker0a597072012-09-25 21:55:46 +00007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007150 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007151 ssl->handshake->flight != NULL )
7152 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007153 /* Cancel handshake timer */
7154 ssl_set_timer( ssl, 0 );
7155
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007156 /* Keep last flight around in case we need to resend it:
7157 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007159 }
7160 else
7161#endif
7162 ssl_handshake_wrapup_free_hs_transform( ssl );
7163
Paul Bakker48916f92012-09-16 19:57:18 +00007164 ssl->state++;
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007167}
7168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007171 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007175 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007176
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007177 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007179 /*
7180 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7181 * may define some other value. Currently (early 2016), no defined
7182 * ciphersuite does this (and this is unlikely to change as activity has
7183 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007188 ssl->verify_data_len = hash_len;
7189 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007190#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007191
Paul Bakker5121ce52009-01-03 21:22:43 +00007192 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7194 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007195
7196 /*
7197 * In case of session resuming, invert the client and server
7198 * ChangeCipherSpec messages order.
7199 */
Paul Bakker0a597072012-09-25 21:55:46 +00007200 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007203 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007207 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007209#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007210 }
7211 else
7212 ssl->state++;
7213
Paul Bakker48916f92012-09-16 19:57:18 +00007214 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007215 * Switch to our negotiated transform and session parameters for outbound
7216 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007221 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007222 {
7223 unsigned char i;
7224
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007225 /* Remember current epoch settings for resending */
7226 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007227 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007228
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007229 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007230 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007231
7232 /* Increment epoch */
7233 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007234 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007235 break;
7236
7237 /* The loop goes to its end iff the counter is wrapping */
7238 if( i == 0 )
7239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7241 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007242 }
7243 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007244 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007246#if defined(MBEDTLS_SSL_PROTO_TLS)
7247 {
7248 memset( ssl->cur_out_ctr, 0, 8 );
7249 }
7250#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007251
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007252 ssl->transform_out = ssl->transform_negotiate;
7253 ssl->session_out = ssl->session_negotiate;
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7256 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007260 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7261 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007262 }
7263 }
7264#endif
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007267 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007269#endif
7270
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007271 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007272 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007274 return( ret );
7275 }
7276
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007278 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007279 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7280 {
7281 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7282 return( ret );
7283 }
7284#endif
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007287
7288 return( 0 );
7289}
7290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007292#define SSL_MAX_HASH_LEN 36
7293#else
7294#define SSL_MAX_HASH_LEN 12
7295#endif
7296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007298{
Paul Bakker23986e52011-04-24 08:57:21 +00007299 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007300 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007301 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007304
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007305 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007306
Hanno Becker327c93b2018-08-15 13:56:18 +01007307 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007310 return( ret );
7311 }
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007316 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7317 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007319 }
7320
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007321 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_SSL3)
7323 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007324 hash_len = 36;
7325 else
7326#endif
7327 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7330 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007333 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7334 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007336 }
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007339 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007342 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7343 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007345 }
7346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007348 ssl->verify_data_len = hash_len;
7349 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007350#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007351
Paul Bakker0a597072012-09-25 21:55:46 +00007352 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007357#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007358#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007359 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007362 }
7363 else
7364 ssl->state++;
7365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007367 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007369#endif
7370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
7373 return( 0 );
7374}
7375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007377{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7381 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7382 mbedtls_md5_init( &handshake->fin_md5 );
7383 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007384 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7385 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7388#if defined(MBEDTLS_SHA256_C)
7389 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007390 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007391#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if defined(MBEDTLS_SHA512_C)
7393 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007394 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007395#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007397
7398 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007399
7400#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7401 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7402 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7403#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405#if defined(MBEDTLS_DHM_C)
7406 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007407#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if defined(MBEDTLS_ECDH_C)
7409 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007410#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007411#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007412 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007413#if defined(MBEDTLS_SSL_CLI_C)
7414 handshake->ecjpake_cache = NULL;
7415 handshake->ecjpake_cache_len = 0;
7416#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007417#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007418
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007419#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007420 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007421#endif
7422
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007423#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7424 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7425#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007426}
7427
Hanno Becker611a83b2018-01-03 14:27:32 +00007428void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007429{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007430 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7433 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007434
Hanno Becker92231322018-01-03 15:32:51 +00007435#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 mbedtls_md_init( &transform->md_ctx_enc );
7437 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007438#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007439}
7440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007444}
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007447{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007448 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007449 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007451 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007453 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007454 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007455
7456 /*
7457 * Either the pointers are now NULL or cleared properly and can be freed.
7458 * Now allocate missing structures.
7459 */
7460 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007461 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007462 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007463 }
Paul Bakker48916f92012-09-16 19:57:18 +00007464
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007465 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007466 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007467 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007468 }
Paul Bakker48916f92012-09-16 19:57:18 +00007469
Paul Bakker82788fb2014-10-20 13:59:19 +02007470 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007471 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007472 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007473 }
Paul Bakker48916f92012-09-16 19:57:18 +00007474
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007475 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007476 if( ssl->handshake == NULL ||
7477 ssl->transform_negotiate == NULL ||
7478 ssl->session_negotiate == NULL )
7479 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 mbedtls_free( ssl->handshake );
7483 mbedtls_free( ssl->transform_negotiate );
7484 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007485
7486 ssl->handshake = NULL;
7487 ssl->transform_negotiate = NULL;
7488 ssl->session_negotiate = NULL;
7489
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007490 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007491 }
7492
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007493 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007495 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007496 ssl_handshake_params_init( ssl->handshake );
7497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007499 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007500 {
7501 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007502
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7504 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7505 else
7506 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007507
7508 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007509 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007510#endif
7511
Paul Bakker48916f92012-09-16 19:57:18 +00007512 return( 0 );
7513}
7514
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007515#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007516/* Dummy cookie callbacks for defaults */
7517static int ssl_cookie_write_dummy( void *ctx,
7518 unsigned char **p, unsigned char *end,
7519 const unsigned char *cli_id, size_t cli_id_len )
7520{
7521 ((void) ctx);
7522 ((void) p);
7523 ((void) end);
7524 ((void) cli_id);
7525 ((void) cli_id_len);
7526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007528}
7529
7530static int ssl_cookie_check_dummy( void *ctx,
7531 const unsigned char *cookie, size_t cookie_len,
7532 const unsigned char *cli_id, size_t cli_id_len )
7533{
7534 ((void) ctx);
7535 ((void) cookie);
7536 ((void) cookie_len);
7537 ((void) cli_id);
7538 ((void) cli_id_len);
7539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007541}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007542#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007543
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007544/* Once ssl->out_hdr as the address of the beginning of the
7545 * next outgoing record is set, deduce the other pointers.
7546 *
7547 * Note: For TLS, we save the implicit record sequence number
7548 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7549 * and the caller has to make sure there's space for this.
7550 */
7551
7552static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7553 mbedtls_ssl_transform *transform )
7554{
7555#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007556 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007557 {
7558 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007560 ssl->out_cid = ssl->out_ctr + 8;
7561 ssl->out_len = ssl->out_cid;
7562 if( transform != NULL )
7563 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007564#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007565 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007566#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007567 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007568 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007569 MBEDTLS_SSL_TRANSPORT_ELSE
7570#endif /* MBEDTLS_SSL_PROTO_DTLS */
7571#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007572 {
7573 ssl->out_ctr = ssl->out_hdr - 8;
7574 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007575#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007576 ssl->out_cid = ssl->out_len;
7577#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007578 ssl->out_iv = ssl->out_hdr + 5;
7579 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007580#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007581
7582 /* Adjust out_msg to make space for explicit IV, if used. */
7583 if( transform != NULL &&
7584 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7585 {
7586 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7587 }
7588 else
7589 ssl->out_msg = ssl->out_iv;
7590}
7591
7592/* Once ssl->in_hdr as the address of the beginning of the
7593 * next incoming record is set, deduce the other pointers.
7594 *
7595 * Note: For TLS, we save the implicit record sequence number
7596 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7597 * and the caller has to make sure there's space for this.
7598 */
7599
Hanno Beckerf5970a02019-05-08 09:38:41 +01007600static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007601{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007602 /* This function sets the pointers to match the case
7603 * of unprotected TLS/DTLS records, with both ssl->in_iv
7604 * and ssl->in_msg pointing to the beginning of the record
7605 * content.
7606 *
7607 * When decrypting a protected record, ssl->in_msg
7608 * will be shifted to point to the beginning of the
7609 * record plaintext.
7610 */
7611
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007612#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007613 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007614 {
Hanno Becker70e79282019-05-03 14:34:53 +01007615 /* This sets the header pointers to match records
7616 * without CID. When we receive a record containing
7617 * a CID, the fields are shifted accordingly in
7618 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007619 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007620#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007621 ssl->in_cid = ssl->in_ctr + 8;
7622 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007623#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007624 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007625#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007626 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007627 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007628 MBEDTLS_SSL_TRANSPORT_ELSE
7629#endif /* MBEDTLS_SSL_PROTO_DTLS */
7630#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007631 {
7632 ssl->in_ctr = ssl->in_hdr - 8;
7633 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007635 ssl->in_cid = ssl->in_len;
7636#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007637 ssl->in_iv = ssl->in_hdr + 5;
7638 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007639#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007640
Hanno Beckerf5970a02019-05-08 09:38:41 +01007641 /* This will be adjusted at record decryption time. */
7642 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007643}
7644
Paul Bakker5121ce52009-01-03 21:22:43 +00007645/*
7646 * Initialize an SSL context
7647 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007648void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7649{
7650 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7651}
7652
7653/*
7654 * Setup an SSL context
7655 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007656
7657static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7658{
7659 /* Set the incoming and outgoing record pointers. */
7660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007661 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007662 {
7663 ssl->out_hdr = ssl->out_buf;
7664 ssl->in_hdr = ssl->in_buf;
7665 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007666 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007667#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007668#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007669 {
7670 ssl->out_hdr = ssl->out_buf + 8;
7671 ssl->in_hdr = ssl->in_buf + 8;
7672 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007673#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007674
7675 /* Derive other internal pointers. */
7676 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007677 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007678}
7679
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007680int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007681 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007682{
Paul Bakker48916f92012-09-16 19:57:18 +00007683 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007685 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007686
7687 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007688 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007689 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007690
7691 /* Set to NULL in case of an error condition */
7692 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007693
Angus Grattond8213d02016-05-25 20:56:48 +10007694 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7695 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007696 {
Angus Grattond8213d02016-05-25 20:56:48 +10007697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007698 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007699 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007700 }
7701
7702 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7703 if( ssl->out_buf == NULL )
7704 {
7705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007706 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007707 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007708 }
7709
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007710 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007711
Paul Bakker48916f92012-09-16 19:57:18 +00007712 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007713 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007714
7715 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007716
7717error:
7718 mbedtls_free( ssl->in_buf );
7719 mbedtls_free( ssl->out_buf );
7720
7721 ssl->conf = NULL;
7722
7723 ssl->in_buf = NULL;
7724 ssl->out_buf = NULL;
7725
7726 ssl->in_hdr = NULL;
7727 ssl->in_ctr = NULL;
7728 ssl->in_len = NULL;
7729 ssl->in_iv = NULL;
7730 ssl->in_msg = NULL;
7731
7732 ssl->out_hdr = NULL;
7733 ssl->out_ctr = NULL;
7734 ssl->out_len = NULL;
7735 ssl->out_iv = NULL;
7736 ssl->out_msg = NULL;
7737
k-stachowiak9f7798e2018-07-31 16:52:32 +02007738 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007739}
7740
7741/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007742 * Reset an initialized and used SSL context for re-use while retaining
7743 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007744 *
7745 * If partial is non-zero, keep data in the input buffer and client ID.
7746 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007747 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007748static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007749{
Paul Bakker48916f92012-09-16 19:57:18 +00007750 int ret;
7751
Hanno Becker7e772132018-08-10 12:38:21 +01007752#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7753 !defined(MBEDTLS_SSL_SRV_C)
7754 ((void) partial);
7755#endif
7756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007758
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007759 /* Cancel any possibly running timer */
7760 ssl_set_timer( ssl, 0 );
7761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_RENEGOTIATION)
7763 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007764 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007765
7766 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7768 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007769#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007771
Paul Bakker7eb013f2011-10-06 12:37:39 +00007772 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007773 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007774
7775 ssl->in_msgtype = 0;
7776 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007778 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007779 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007782 ssl_dtls_replay_reset( ssl );
7783#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007784
7785 ssl->in_hslen = 0;
7786 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007787
7788 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007789
7790 ssl->out_msgtype = 0;
7791 ssl->out_msglen = 0;
7792 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7794 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007795 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007796#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007797
Hanno Becker19859472018-08-06 09:40:20 +01007798 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7799
Paul Bakker48916f92012-09-16 19:57:18 +00007800 ssl->transform_in = NULL;
7801 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007802
Hanno Becker78640902018-08-13 16:35:15 +01007803 ssl->session_in = NULL;
7804 ssl->session_out = NULL;
7805
Angus Grattond8213d02016-05-25 20:56:48 +10007806 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007807
7808#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007809 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007810#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7811 {
7812 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007813 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007814 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7817 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7820 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7823 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007824 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007825 }
7826#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007827
Paul Bakker48916f92012-09-16 19:57:18 +00007828 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 mbedtls_ssl_transform_free( ssl->transform );
7831 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007832 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007833 }
Paul Bakker48916f92012-09-16 19:57:18 +00007834
Paul Bakkerc0463502013-02-14 11:19:38 +01007835 if( ssl->session )
7836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837 mbedtls_ssl_session_free( ssl->session );
7838 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007839 ssl->session = NULL;
7840 }
7841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007843 ssl->alpn_chosen = NULL;
7844#endif
7845
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007846#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007847#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007848 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007849#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007850 {
7851 mbedtls_free( ssl->cli_id );
7852 ssl->cli_id = NULL;
7853 ssl->cli_id_len = 0;
7854 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007855#endif
7856
Paul Bakker48916f92012-09-16 19:57:18 +00007857 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7858 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007859
7860 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007861}
7862
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007863/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007864 * Reset an initialized and used SSL context for re-use while retaining
7865 * all application-set variables, function pointers and data.
7866 */
7867int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7868{
7869 return( ssl_session_reset_int( ssl, 0 ) );
7870}
7871
7872/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007873 * SSL set accessors
7874 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007875void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007876{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007877 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007878}
7879
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007880void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007881{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007882 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007883}
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007886void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007887{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007888 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007889}
7890#endif
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007893void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007894{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007895 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007896}
7897#endif
7898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007900
Hanno Becker1841b0a2018-08-24 11:13:57 +01007901void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7902 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007903{
7904 ssl->disable_datagram_packing = !allow_packing;
7905}
7906
7907void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7908 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007909{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007910 conf->hs_timeout_min = min;
7911 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007912}
7913#endif
7914
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007915void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007916{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007917 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007918}
7919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007921void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007922 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007923 void *p_vrfy )
7924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007925 conf->f_vrfy = f_vrfy;
7926 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007929
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007930void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007931 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007932 void *p_rng )
7933{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007934 conf->f_rng = f_rng;
7935 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007936}
7937
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007939 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 void *p_dbg )
7941{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007942 conf->f_dbg = f_dbg;
7943 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007944}
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007947 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007948 mbedtls_ssl_send_t *f_send,
7949 mbedtls_ssl_recv_t *f_recv,
7950 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007951{
7952 ssl->p_bio = p_bio;
7953 ssl->f_send = f_send;
7954 ssl->f_recv = f_recv;
7955 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007956}
7957
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007958#if defined(MBEDTLS_SSL_PROTO_DTLS)
7959void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7960{
7961 ssl->mtu = mtu;
7962}
7963#endif
7964
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007965void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007966{
7967 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007968}
7969
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007970void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7971 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007972 mbedtls_ssl_set_timer_t *f_set_timer,
7973 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007974{
7975 ssl->p_timer = p_timer;
7976 ssl->f_set_timer = f_set_timer;
7977 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007978
7979 /* Make sure we start with no timer running */
7980 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007981}
7982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007984void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007985 void *p_cache,
7986 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7987 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007988{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007989 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007990 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007991 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007993#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_SSL_CLI_C)
7996int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007997{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007998 int ret;
7999
8000 if( ssl == NULL ||
8001 session == NULL ||
8002 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008003 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008004 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008006 }
8007
Hanno Becker58fccf22019-02-06 14:30:46 +00008008 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8009 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008010 return( ret );
8011
Paul Bakker0a597072012-09-25 21:55:46 +00008012 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008013
8014 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008016#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008017
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008018void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008019 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008020{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008021 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8022 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8023 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8024 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008025}
8026
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008027void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008028 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008029 int major, int minor )
8030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008032 return;
8033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008035 return;
8036
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008037 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008038}
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008041void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008042 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008043{
8044 conf->cert_profile = profile;
8045}
8046
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008047/* Append a new keycert entry to a (possibly empty) list */
8048static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8049 mbedtls_x509_crt *cert,
8050 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008051{
niisato8ee24222018-06-25 19:05:48 +09008052 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008053
niisato8ee24222018-06-25 19:05:48 +09008054 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8055 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008056 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008057
niisato8ee24222018-06-25 19:05:48 +09008058 new_cert->cert = cert;
8059 new_cert->key = key;
8060 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008061
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008062 /* Update head is the list was null, else add to the end */
8063 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008064 {
niisato8ee24222018-06-25 19:05:48 +09008065 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008066 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008067 else
8068 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008069 mbedtls_ssl_key_cert *cur = *head;
8070 while( cur->next != NULL )
8071 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008072 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008073 }
8074
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008075 return( 0 );
8076}
8077
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008078int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008079 mbedtls_x509_crt *own_cert,
8080 mbedtls_pk_context *pk_key )
8081{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008082 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008083}
8084
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008085void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008086 mbedtls_x509_crt *ca_chain,
8087 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008088{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008089 conf->ca_chain = ca_chain;
8090 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008093
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008094#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8095int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8096 mbedtls_x509_crt *own_cert,
8097 mbedtls_pk_context *pk_key )
8098{
8099 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8100 own_cert, pk_key ) );
8101}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008102
8103void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8104 mbedtls_x509_crt *ca_chain,
8105 mbedtls_x509_crl *ca_crl )
8106{
8107 ssl->handshake->sni_ca_chain = ca_chain;
8108 ssl->handshake->sni_ca_crl = ca_crl;
8109}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008110
8111void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8112 int authmode )
8113{
8114 ssl->handshake->sni_authmode = authmode;
8115}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008116#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8117
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008118#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008119/*
8120 * Set EC J-PAKE password for current handshake
8121 */
8122int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8123 const unsigned char *pw,
8124 size_t pw_len )
8125{
8126 mbedtls_ecjpake_role role;
8127
Janos Follath8eb64132016-06-03 15:40:57 +01008128 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8130
8131 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8132 role = MBEDTLS_ECJPAKE_SERVER;
8133 else
8134 role = MBEDTLS_ECJPAKE_CLIENT;
8135
8136 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8137 role,
8138 MBEDTLS_MD_SHA256,
8139 MBEDTLS_ECP_DP_SECP256R1,
8140 pw, pw_len ) );
8141}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008142#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008145int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008146 const unsigned char *psk, size_t psk_len,
8147 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008148{
Paul Bakker6db455e2013-09-18 17:29:31 +02008149 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008154
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008155 /* Identity len will be encoded on two bytes */
8156 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008157 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008158 {
8159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8160 }
8161
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008162 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008163 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008164 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008165
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008166 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008167 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008168 conf->psk_len = 0;
8169 }
8170 if( conf->psk_identity != NULL )
8171 {
8172 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008173 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008174 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008175 }
8176
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008177 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8178 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008179 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008180 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008181 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008182 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008183 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008184 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008185 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008186
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008187 conf->psk_len = psk_len;
8188 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008189
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008190 memcpy( conf->psk, psk, conf->psk_len );
8191 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008192
8193 return( 0 );
8194}
8195
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008196int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8197 const unsigned char *psk, size_t psk_len )
8198{
8199 if( psk == NULL || ssl->handshake == NULL )
8200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8201
8202 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8203 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8204
8205 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008206 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008207 mbedtls_platform_zeroize( ssl->handshake->psk,
8208 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008209 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008210 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008211 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008212
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008213 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008214 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008215
8216 ssl->handshake->psk_len = psk_len;
8217 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8218
8219 return( 0 );
8220}
8221
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008222void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008224 size_t),
8225 void *p_psk )
8226{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008227 conf->f_psk = f_psk;
8228 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008229}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008230#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008231
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008232#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008233
8234#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008235int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008236{
8237 int ret;
8238
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008239 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8240 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8241 {
8242 mbedtls_mpi_free( &conf->dhm_P );
8243 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008244 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008246
8247 return( 0 );
8248}
Hanno Becker470a8c42017-10-04 15:28:46 +01008249#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008250
Hanno Beckera90658f2017-10-04 15:29:08 +01008251int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8252 const unsigned char *dhm_P, size_t P_len,
8253 const unsigned char *dhm_G, size_t G_len )
8254{
8255 int ret;
8256
8257 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8258 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8259 {
8260 mbedtls_mpi_free( &conf->dhm_P );
8261 mbedtls_mpi_free( &conf->dhm_G );
8262 return( ret );
8263 }
8264
8265 return( 0 );
8266}
Paul Bakker5121ce52009-01-03 21:22:43 +00008267
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008268int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008269{
8270 int ret;
8271
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008272 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8273 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8274 {
8275 mbedtls_mpi_free( &conf->dhm_P );
8276 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008277 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008278 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008279
8280 return( 0 );
8281}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008282#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008283
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008284#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8285/*
8286 * Set the minimum length for Diffie-Hellman parameters
8287 */
8288void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8289 unsigned int bitlen )
8290{
8291 conf->dhm_min_bitlen = bitlen;
8292}
8293#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8294
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008295#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008296/*
8297 * Set allowed/preferred hashes for handshake signatures
8298 */
8299void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8300 const int *hashes )
8301{
8302 conf->sig_hashes = hashes;
8303}
Hanno Becker947194e2017-04-07 13:25:49 +01008304#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008305
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008306#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008307/*
8308 * Set the allowed elliptic curves
8309 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008310void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008311 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008312{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008313 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008314}
Hanno Becker947194e2017-04-07 13:25:49 +01008315#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008316
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008317#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008319{
Hanno Becker947194e2017-04-07 13:25:49 +01008320 /* Initialize to suppress unnecessary compiler warning */
8321 size_t hostname_len = 0;
8322
8323 /* Check if new hostname is valid before
8324 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008325 if( hostname != NULL )
8326 {
8327 hostname_len = strlen( hostname );
8328
8329 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8331 }
8332
8333 /* Now it's clear that we will overwrite the old hostname,
8334 * so we can free it safely */
8335
8336 if( ssl->hostname != NULL )
8337 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008338 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008339 mbedtls_free( ssl->hostname );
8340 }
8341
8342 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008343
Paul Bakker5121ce52009-01-03 21:22:43 +00008344 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008345 {
8346 ssl->hostname = NULL;
8347 }
8348 else
8349 {
8350 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008351 if( ssl->hostname == NULL )
8352 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008353
Hanno Becker947194e2017-04-07 13:25:49 +01008354 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008355
Hanno Becker947194e2017-04-07 13:25:49 +01008356 ssl->hostname[hostname_len] = '\0';
8357 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008358
8359 return( 0 );
8360}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008361#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008362
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008363#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008364void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008366 const unsigned char *, size_t),
8367 void *p_sni )
8368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008369 conf->f_sni = f_sni;
8370 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008374#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008375int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008376{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008377 size_t cur_len, tot_len;
8378 const char **p;
8379
8380 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008381 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8382 * MUST NOT be truncated."
8383 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008384 */
8385 tot_len = 0;
8386 for( p = protos; *p != NULL; p++ )
8387 {
8388 cur_len = strlen( *p );
8389 tot_len += cur_len;
8390
8391 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008392 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008393 }
8394
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008396
8397 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008398}
8399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008400const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008401{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008402 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008404#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008405
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008406void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008407{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008408 conf->max_major_ver = major;
8409 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008410}
8411
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008412void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008413{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008414 conf->min_major_ver = major;
8415 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008416}
8417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008419void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008420{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008421 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008422}
8423#endif
8424
Janos Follath088ce432017-04-10 12:42:31 +01008425#if defined(MBEDTLS_SSL_SRV_C)
8426void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8427 char cert_req_ca_list )
8428{
8429 conf->cert_req_ca_list = cert_req_ca_list;
8430}
8431#endif
8432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008434void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008436 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008437}
8438#endif
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008441void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008442{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008443 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008444}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008445
8446void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008447 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008448{
8449 conf->enforce_extended_master_secret = ems_enf;
8450}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008451#endif
8452
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008453#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008454void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008455{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008456 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008457}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008458#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008462{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008464 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008466 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008467 }
8468
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008469 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008470
8471 return( 0 );
8472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008473#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008475#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008476void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008477{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008478 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008479}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008480#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008482#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008483void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008484{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008485 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008486}
8487#endif
8488
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008489void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008490{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008491 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008492}
8493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008496{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008497 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008498}
8499
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008500void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008501{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008502 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008503}
8504
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008505void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008506 const unsigned char period[8] )
8507{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008508 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008512#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008513#if defined(MBEDTLS_SSL_CLI_C)
8514void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008515{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008516 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008517}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008518#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008519
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008520#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008521void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8522 mbedtls_ssl_ticket_write_t *f_ticket_write,
8523 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8524 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008525{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008526 conf->f_ticket_write = f_ticket_write;
8527 conf->f_ticket_parse = f_ticket_parse;
8528 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008529}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008531#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008532
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008533#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8534void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8535 mbedtls_ssl_export_keys_t *f_export_keys,
8536 void *p_export_keys )
8537{
8538 conf->f_export_keys = f_export_keys;
8539 conf->p_export_keys = p_export_keys;
8540}
8541#endif
8542
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008543#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008544void mbedtls_ssl_conf_async_private_cb(
8545 mbedtls_ssl_config *conf,
8546 mbedtls_ssl_async_sign_t *f_async_sign,
8547 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8548 mbedtls_ssl_async_resume_t *f_async_resume,
8549 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008550 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008551{
8552 conf->f_async_sign_start = f_async_sign;
8553 conf->f_async_decrypt_start = f_async_decrypt;
8554 conf->f_async_resume = f_async_resume;
8555 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008556 conf->p_async_config_data = async_config_data;
8557}
8558
Gilles Peskine8f97af72018-04-26 11:46:10 +02008559void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8560{
8561 return( conf->p_async_config_data );
8562}
8563
Gilles Peskine1febfef2018-04-30 11:54:39 +02008564void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008565{
8566 if( ssl->handshake == NULL )
8567 return( NULL );
8568 else
8569 return( ssl->handshake->user_async_ctx );
8570}
8571
Gilles Peskine1febfef2018-04-30 11:54:39 +02008572void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008573 void *ctx )
8574{
8575 if( ssl->handshake != NULL )
8576 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008577}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008578#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008579
Paul Bakker5121ce52009-01-03 21:22:43 +00008580/*
8581 * SSL get accessors
8582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008584{
8585 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8586}
8587
Hanno Becker8b170a02017-10-10 11:51:19 +01008588int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8589{
8590 /*
8591 * Case A: We're currently holding back
8592 * a message for further processing.
8593 */
8594
8595 if( ssl->keep_current_message == 1 )
8596 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008598 return( 1 );
8599 }
8600
8601 /*
8602 * Case B: Further records are pending in the current datagram.
8603 */
8604
8605#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008606 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008607 ssl->in_left > ssl->next_record_offset )
8608 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008610 return( 1 );
8611 }
8612#endif /* MBEDTLS_SSL_PROTO_DTLS */
8613
8614 /*
8615 * Case C: A handshake message is being processed.
8616 */
8617
Hanno Becker8b170a02017-10-10 11:51:19 +01008618 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8619 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008621 return( 1 );
8622 }
8623
8624 /*
8625 * Case D: An application data message is being processed
8626 */
8627 if( ssl->in_offt != NULL )
8628 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008630 return( 1 );
8631 }
8632
8633 /*
8634 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008635 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008636 * we implement support for multiple alerts in single records.
8637 */
8638
8639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8640 return( 0 );
8641}
8642
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008643uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008644{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008645 if( ssl->session != NULL )
8646 return( ssl->session->verify_result );
8647
8648 if( ssl->session_negotiate != NULL )
8649 return( ssl->session_negotiate->verify_result );
8650
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008651 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008652}
8653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008655{
Paul Bakker926c8e42013-03-06 10:23:34 +01008656 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008657 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008660}
8661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008665 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008666 {
8667 switch( ssl->minor_ver )
8668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008670 return( "DTLSv1.0" );
8671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008673 return( "DTLSv1.2" );
8674
8675 default:
8676 return( "unknown (DTLS)" );
8677 }
8678 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008679 MBEDTLS_SSL_TRANSPORT_ELSE
8680#endif /* MBEDTLS_SSL_PROTO_DTLS */
8681#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008682 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008683 switch( ssl->minor_ver )
8684 {
8685 case MBEDTLS_SSL_MINOR_VERSION_0:
8686 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008687
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008688 case MBEDTLS_SSL_MINOR_VERSION_1:
8689 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008690
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008691 case MBEDTLS_SSL_MINOR_VERSION_2:
8692 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008693
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008694 case MBEDTLS_SSL_MINOR_VERSION_3:
8695 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008696
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008697 default:
8698 return( "unknown" );
8699 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008700 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008701#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008702}
8703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008705{
Hanno Becker3136ede2018-08-17 15:28:19 +01008706 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008707 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008708 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008709
Hanno Becker43395762019-05-03 14:46:38 +01008710 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8711
Hanno Becker78640902018-08-13 16:35:15 +01008712 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008713 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715#if defined(MBEDTLS_ZLIB_SUPPORT)
8716 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8717 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008718#endif
8719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008720 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008722 case MBEDTLS_MODE_GCM:
8723 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008724 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008726 transform_expansion = transform->minlen;
8727 break;
8728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008729 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008730
8731 block_size = mbedtls_cipher_get_block_size(
8732 &transform->cipher_ctx_enc );
8733
Hanno Becker3136ede2018-08-17 15:28:19 +01008734 /* Expansion due to the addition of the MAC. */
8735 transform_expansion += transform->maclen;
8736
8737 /* Expansion due to the addition of CBC padding;
8738 * Theoretically up to 256 bytes, but we never use
8739 * more than the block size of the underlying cipher. */
8740 transform_expansion += block_size;
8741
8742 /* For TLS 1.1 or higher, an explicit IV is added
8743 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008744#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8745 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008746 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008747#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008748
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008749 break;
8750
8751 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008753 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008754 }
8755
Hanno Beckera5a2b082019-05-15 14:03:01 +01008756#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008757 if( transform->out_cid_len != 0 )
8758 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008759#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008760
Hanno Becker43395762019-05-03 14:46:38 +01008761 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008762}
8763
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008764#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8765size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8766{
8767 size_t max_len;
8768
8769 /*
8770 * Assume mfl_code is correct since it was checked when set
8771 */
Angus Grattond8213d02016-05-25 20:56:48 +10008772 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008773
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008774 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008775 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008776 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008777 {
Angus Grattond8213d02016-05-25 20:56:48 +10008778 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008779 }
8780
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008781 /* During a handshake, use the value being negotiated */
8782 if( ssl->session_negotiate != NULL &&
8783 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8784 {
8785 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8786 }
8787
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008788 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008789}
8790#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8791
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008792#if defined(MBEDTLS_SSL_PROTO_DTLS)
8793static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8794{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008795 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8796 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8797 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8798 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8799 return ( 0 );
8800
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008801 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8802 return( ssl->mtu );
8803
8804 if( ssl->mtu == 0 )
8805 return( ssl->handshake->mtu );
8806
8807 return( ssl->mtu < ssl->handshake->mtu ?
8808 ssl->mtu : ssl->handshake->mtu );
8809}
8810#endif /* MBEDTLS_SSL_PROTO_DTLS */
8811
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008812int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8813{
8814 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8815
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008816#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8817 !defined(MBEDTLS_SSL_PROTO_DTLS)
8818 (void) ssl;
8819#endif
8820
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008821#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8822 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8823
8824 if( max_len > mfl )
8825 max_len = mfl;
8826#endif
8827
8828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008829 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008830 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008831 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008832 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8833 const size_t overhead = (size_t) ret;
8834
8835 if( ret < 0 )
8836 return( ret );
8837
8838 if( mtu <= overhead )
8839 {
8840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8841 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8842 }
8843
8844 if( max_len > mtu - overhead )
8845 max_len = mtu - overhead;
8846 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008847#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008848
Hanno Becker0defedb2018-08-10 12:35:02 +01008849#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8850 !defined(MBEDTLS_SSL_PROTO_DTLS)
8851 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008852#endif
8853
8854 return( (int) max_len );
8855}
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857#if defined(MBEDTLS_X509_CRT_PARSE_C)
8858const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008859{
8860 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008861 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008862
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008863 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008868int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8869 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008870{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008871 if( ssl == NULL ||
8872 dst == NULL ||
8873 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008874 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008877 }
8878
Hanno Becker58fccf22019-02-06 14:30:46 +00008879 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008882
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008883const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8884{
8885 if( ssl == NULL )
8886 return( NULL );
8887
8888 return( ssl->session );
8889}
8890
Paul Bakker5121ce52009-01-03 21:22:43 +00008891/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008892 * Define ticket header determining Mbed TLS version
8893 * and structure of the ticket.
8894 */
8895
Hanno Becker41527622019-05-16 12:50:45 +01008896/*
Hanno Becker26829e92019-05-28 14:30:45 +01008897 * Define bitflag determining compile-time settings influencing
8898 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008899 */
8900
Hanno Becker26829e92019-05-28 14:30:45 +01008901#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008902#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008903#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008904#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008905#endif /* MBEDTLS_HAVE_TIME */
8906
8907#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008908#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008909#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008910#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008911#endif /* MBEDTLS_X509_CRT_PARSE_C */
8912
8913#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008914#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008915#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008916#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008917#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8918
8919#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008920#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008921#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008922#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008923#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8924
8925#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008926#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008927#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008928#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008929#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8930
8931#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008932#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008933#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008934#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008935#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8936
Hanno Becker41527622019-05-16 12:50:45 +01008937#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8938#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8939#else
8940#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8941#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8942
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008943#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8944#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8945#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8946#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8947#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8948#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8949#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008950
Hanno Becker26829e92019-05-28 14:30:45 +01008951#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008952 ( (uint16_t) ( \
8953 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8954 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8955 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8956 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8957 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8958 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008959 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008960
Hanno Becker557fe9f2019-05-16 12:41:07 +01008961static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008962 MBEDTLS_VERSION_MAJOR,
8963 MBEDTLS_VERSION_MINOR,
8964 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008965 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8966 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008967};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008968
8969/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008970 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008971 * (in the presentation language of TLS, RFC 8446 section 3)
8972 *
Hanno Becker26829e92019-05-28 14:30:45 +01008973 * opaque mbedtls_version[3]; // major, minor, patch
8974 * opaque session_format[2]; // version-specific 16-bit field determining
8975 * // the format of the remaining
8976 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008977 *
8978 * Note: When updating the format, remember to keep
8979 * these version+format bytes.
8980 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008981 * // In this version, `session_format` determines
8982 * // the setting of those compile-time
8983 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008984 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008985 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008986 * uint8 ciphersuite[2]; // defined by the standard
8987 * uint8 compression; // 0 or 1
8988 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008989 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008990 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008991 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008992 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8993 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008994 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008995 * uint8 mfl_code; // up to 255 according to standard
8996 * uint8 trunc_hmac; // 0 or 1
8997 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008998 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008999 * The order is the same as in the definition of the structure, except
9000 * verify_result is put before peer_cert so that all mandatory fields come
9001 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009002 */
9003int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9004 unsigned char *buf,
9005 size_t buf_len,
9006 size_t *olen )
9007{
9008 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009009 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009010#if defined(MBEDTLS_HAVE_TIME)
9011 uint64_t start;
9012#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009013#if defined(MBEDTLS_X509_CRT_PARSE_C)
9014 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009015#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009016
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009017 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009018 * Add version identifier
9019 */
9020
9021 used += sizeof( ssl_serialized_session_header );
9022
9023 if( used <= buf_len )
9024 {
9025 memcpy( p, ssl_serialized_session_header,
9026 sizeof( ssl_serialized_session_header ) );
9027 p += sizeof( ssl_serialized_session_header );
9028 }
9029
9030 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009031 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009032 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009033#if defined(MBEDTLS_HAVE_TIME)
9034 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009035
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009036 if( used <= buf_len )
9037 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009038 start = (uint64_t) session->start;
9039
9040 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9041 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9042 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9043 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9044 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9045 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9046 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9047 *p++ = (unsigned char)( ( start ) & 0xFF );
9048 }
9049#endif /* MBEDTLS_HAVE_TIME */
9050
9051 /*
9052 * Basic mandatory fields
9053 */
9054 used += 2 /* ciphersuite */
9055 + 1 /* compression */
9056 + 1 /* id_len */
9057 + sizeof( session->id )
9058 + sizeof( session->master )
9059 + 4; /* verify_result */
9060
9061 if( used <= buf_len )
9062 {
9063 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9064 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9065
9066 *p++ = (unsigned char)( session->compression & 0xFF );
9067
9068 *p++ = (unsigned char)( session->id_len & 0xFF );
9069 memcpy( p, session->id, 32 );
9070 p += 32;
9071
9072 memcpy( p, session->master, 48 );
9073 p += 48;
9074
9075 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9076 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9077 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9078 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009079 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009080
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009081 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009082 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009083 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009084#if defined(MBEDTLS_X509_CRT_PARSE_C)
9085 if( session->peer_cert == NULL )
9086 cert_len = 0;
9087 else
9088 cert_len = session->peer_cert->raw.len;
9089
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009090 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009091
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009092 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009093 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009094 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9095 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9096 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9097
9098 if( session->peer_cert != NULL )
9099 {
9100 memcpy( p, session->peer_cert->raw.p, cert_len );
9101 p += cert_len;
9102 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009103 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009104#endif /* MBEDTLS_X509_CRT_PARSE_C */
9105
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009106 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009107 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009108 */
9109#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009110 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009111
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009112 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009113 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009114 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9115 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9116 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9117
9118 if( session->ticket != NULL )
9119 {
9120 memcpy( p, session->ticket, session->ticket_len );
9121 p += session->ticket_len;
9122 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009123
9124 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9125 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9126 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9127 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009128 }
9129#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9130
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009131 /*
9132 * Misc extension-related info
9133 */
9134#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9135 used += 1;
9136
9137 if( used <= buf_len )
9138 *p++ = session->mfl_code;
9139#endif
9140
9141#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9142 used += 1;
9143
9144 if( used <= buf_len )
9145 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9146#endif
9147
9148#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9149 used += 1;
9150
9151 if( used <= buf_len )
9152 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9153#endif
9154
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009155 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009156 *olen = used;
9157
9158 if( used > buf_len )
9159 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009160
9161 return( 0 );
9162}
9163
9164/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009165 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009166 *
9167 * This internal version is wrapped by a public function that cleans up in
9168 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009169 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009170static int ssl_session_load( mbedtls_ssl_session *session,
9171 const unsigned char *buf,
9172 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009173{
9174 const unsigned char *p = buf;
9175 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009176#if defined(MBEDTLS_HAVE_TIME)
9177 uint64_t start;
9178#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009179#if defined(MBEDTLS_X509_CRT_PARSE_C)
9180 size_t cert_len;
9181#endif /* MBEDTLS_X509_CRT_PARSE_C */
9182
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009183 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009184 * Check version identifier
9185 */
9186
9187 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009189
9190 if( memcmp( p, ssl_serialized_session_header,
9191 sizeof( ssl_serialized_session_header ) ) != 0 )
9192 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009193 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009194 }
9195 p += sizeof( ssl_serialized_session_header );
9196
9197 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009198 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009199 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009200#if defined(MBEDTLS_HAVE_TIME)
9201 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9203
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009204 start = ( (uint64_t) p[0] << 56 ) |
9205 ( (uint64_t) p[1] << 48 ) |
9206 ( (uint64_t) p[2] << 40 ) |
9207 ( (uint64_t) p[3] << 32 ) |
9208 ( (uint64_t) p[4] << 24 ) |
9209 ( (uint64_t) p[5] << 16 ) |
9210 ( (uint64_t) p[6] << 8 ) |
9211 ( (uint64_t) p[7] );
9212 p += 8;
9213
9214 session->start = (time_t) start;
9215#endif /* MBEDTLS_HAVE_TIME */
9216
9217 /*
9218 * Basic mandatory fields
9219 */
9220 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9221 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9222
9223 session->ciphersuite = ( p[0] << 8 ) | p[1];
9224 p += 2;
9225
9226 session->compression = *p++;
9227
9228 session->id_len = *p++;
9229 memcpy( session->id, p, 32 );
9230 p += 32;
9231
9232 memcpy( session->master, p, 48 );
9233 p += 48;
9234
9235 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9236 ( (uint32_t) p[1] << 16 ) |
9237 ( (uint32_t) p[2] << 8 ) |
9238 ( (uint32_t) p[3] );
9239 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009240
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009241 /* Immediately clear invalid pointer values that have been read, in case
9242 * we exit early before we replaced them with valid ones. */
9243#if defined(MBEDTLS_X509_CRT_PARSE_C)
9244 session->peer_cert = NULL;
9245#endif
9246#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9247 session->ticket = NULL;
9248#endif
9249
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009250 /*
9251 * Peer certificate
9252 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009253#if defined(MBEDTLS_X509_CRT_PARSE_C)
9254 if( 3 > (size_t)( end - p ) )
9255 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9256
9257 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9258 p += 3;
9259
9260 if( cert_len == 0 )
9261 {
9262 session->peer_cert = NULL;
9263 }
9264 else
9265 {
9266 int ret;
9267
9268 if( cert_len > (size_t)( end - p ) )
9269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9270
9271 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9272
9273 if( session->peer_cert == NULL )
9274 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9275
9276 mbedtls_x509_crt_init( session->peer_cert );
9277
9278 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9279 p, cert_len ) ) != 0 )
9280 {
9281 mbedtls_x509_crt_free( session->peer_cert );
9282 mbedtls_free( session->peer_cert );
9283 session->peer_cert = NULL;
9284 return( ret );
9285 }
9286
9287 p += cert_len;
9288 }
9289#endif /* MBEDTLS_X509_CRT_PARSE_C */
9290
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009291 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009292 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009293 */
9294#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9295 if( 3 > (size_t)( end - p ) )
9296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9297
9298 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9299 p += 3;
9300
9301 if( session->ticket_len != 0 )
9302 {
9303 if( session->ticket_len > (size_t)( end - p ) )
9304 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9305
9306 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9307 if( session->ticket == NULL )
9308 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9309
9310 memcpy( session->ticket, p, session->ticket_len );
9311 p += session->ticket_len;
9312 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009313
9314 if( 4 > (size_t)( end - p ) )
9315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9316
9317 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9318 ( (uint32_t) p[1] << 16 ) |
9319 ( (uint32_t) p[2] << 8 ) |
9320 ( (uint32_t) p[3] );
9321 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009322#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9323
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009324 /*
9325 * Misc extension-related info
9326 */
9327#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9328 if( 1 > (size_t)( end - p ) )
9329 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9330
9331 session->mfl_code = *p++;
9332#endif
9333
9334#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9335 if( 1 > (size_t)( end - p ) )
9336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9337
9338 session->trunc_hmac = *p++;
9339#endif
9340
9341#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9342 if( 1 > (size_t)( end - p ) )
9343 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9344
9345 session->encrypt_then_mac = *p++;
9346#endif
9347
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009348 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009349 if( p != end )
9350 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9351
9352 return( 0 );
9353}
9354
9355/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009356 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009357 */
9358int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9359 const unsigned char *buf,
9360 size_t len )
9361{
9362 int ret = ssl_session_load( session, buf, len );
9363
9364 if( ret != 0 )
9365 mbedtls_ssl_session_free( session );
9366
9367 return( ret );
9368}
9369
9370/*
Paul Bakker1961b702013-01-25 14:49:24 +01009371 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009373int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009374{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009376
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009377 if( ssl == NULL || ssl->conf == NULL )
9378 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009380#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009381 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009385 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009387#endif
9388
Paul Bakker1961b702013-01-25 14:49:24 +01009389 return( ret );
9390}
9391
9392/*
9393 * Perform the SSL handshake
9394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009396{
9397 int ret = 0;
9398
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009399 if( ssl == NULL || ssl->conf == NULL )
9400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009407
9408 if( ret != 0 )
9409 break;
9410 }
9411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009413
9414 return( ret );
9415}
9416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009417#if defined(MBEDTLS_SSL_RENEGOTIATION)
9418#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009419/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009420 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009423{
9424 int ret;
9425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009427
9428 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9430 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009431
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009432 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009433 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009434 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009435 return( ret );
9436 }
9437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009439
9440 return( 0 );
9441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009442#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009443
9444/*
9445 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 * - any side: calling mbedtls_ssl_renegotiate(),
9447 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9448 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009449 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009450 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009454{
9455 int ret;
9456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009458
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009459 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9460 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009461
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009462 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9463 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009465 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009467 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009468 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009469 ssl->handshake->out_msg_seq = 1;
9470 else
9471 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009472 }
9473#endif
9474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9476 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009481 return( ret );
9482 }
9483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009485
9486 return( 0 );
9487}
9488
9489/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009490 * Renegotiate current connection on client,
9491 * or request renegotiation on server
9492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009496
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009497 if( ssl == NULL || ssl->conf == NULL )
9498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009500#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009501 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009502 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9505 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009508
9509 /* Did we already try/start sending HelloRequest? */
9510 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009512
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009513 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009518 /*
9519 * On client, either start the renegotiation process or,
9520 * if already in progress, continue the handshake
9521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009526
9527 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009530 return( ret );
9531 }
9532 }
9533 else
9534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009538 return( ret );
9539 }
9540 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009542
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009543 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009544}
9545
9546/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009547 * Check record counters and renegotiate if they're above the limit.
9548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009550{
Andres AG2196c7f2016-12-15 17:01:16 +00009551 size_t ep_len = ssl_ep_len( ssl );
9552 int in_ctr_cmp;
9553 int out_ctr_cmp;
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9556 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009557 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009558 {
9559 return( 0 );
9560 }
9561
Andres AG2196c7f2016-12-15 17:01:16 +00009562 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9563 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009564 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009565 ssl->conf->renego_period + ep_len, 8 - ep_len );
9566
9567 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009568 {
9569 return( 0 );
9570 }
9571
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009576
9577/*
9578 * Receive application data decrypted from the SSL layer
9579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009581{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009582 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009583 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009584
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009585 if( ssl == NULL || ssl->conf == NULL )
9586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009591 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009594 return( ret );
9595
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009596 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009598 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009599 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009600 return( ret );
9601 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009602 }
9603#endif
9604
Hanno Becker4a810fb2017-05-24 16:27:30 +01009605 /*
9606 * Check if renegotiation is necessary and/or handshake is
9607 * in process. If yes, perform/continue, and fall through
9608 * if an unexpected packet is received while the client
9609 * is waiting for the ServerHello.
9610 *
9611 * (There is no equivalent to the last condition on
9612 * the server-side as it is not treated as within
9613 * a handshake while waiting for the ClientHello
9614 * after a renegotiation request.)
9615 */
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009618 ret = ssl_check_ctr_renegotiate( ssl );
9619 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9620 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009623 return( ret );
9624 }
9625#endif
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009630 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9631 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 return( ret );
9635 }
9636 }
9637
Hanno Beckere41158b2017-10-23 13:30:32 +01009638 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009639 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009640 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009641 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009642 if( ssl->f_get_timer != NULL &&
9643 ssl->f_get_timer( ssl->p_timer ) == -1 )
9644 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009645 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009646 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009647
Hanno Becker327c93b2018-08-15 13:56:18 +01009648 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009649 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009650 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9651 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009652
Hanno Becker4a810fb2017-05-24 16:27:30 +01009653 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9654 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009655 }
9656
9657 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009659 {
9660 /*
9661 * OpenSSL sends empty messages to randomize the IV
9662 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009663 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009666 return( 0 );
9667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009669 return( ret );
9670 }
9671 }
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009676
Hanno Becker4a810fb2017-05-24 16:27:30 +01009677 /*
9678 * - For client-side, expect SERVER_HELLO_REQUEST.
9679 * - For server-side, expect CLIENT_HELLO.
9680 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9681 */
9682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009686 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009689
9690 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009692 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009693 {
9694 continue;
9695 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009696 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009697#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009698#if defined(MBEDTLS_SSL_PROTO_TLS)
9699 {
9700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9701 }
9702#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009703 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009704#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009705
Hanno Becker4a810fb2017-05-24 16:27:30 +01009706#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009711
9712 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009715 {
9716 continue;
9717 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009718 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009719#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009720#if defined(MBEDTLS_SSL_PROTO_TLS)
9721 {
9722 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9723 }
9724#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009725 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009726#endif /* MBEDTLS_SSL_SRV_C */
9727
Hanno Becker21df7f92017-10-17 11:03:26 +01009728#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009729 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009730 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9731 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9732 ssl->conf->allow_legacy_renegotiation ==
9733 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9734 {
9735 /*
9736 * Accept renegotiation request
9737 */
Paul Bakker48916f92012-09-16 19:57:18 +00009738
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009739 /* DTLS clients need to know renego is server-initiated */
9740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009742 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9743 {
9744 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9745 }
9746#endif
9747 ret = ssl_start_renegotiation( ssl );
9748 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9749 ret != 0 )
9750 {
9751 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9752 return( ret );
9753 }
9754 }
9755 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009756#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009757 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009758 /*
9759 * Refuse renegotiation
9760 */
9761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_PROTO_SSL3)
9765 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009766 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009767 /* SSLv3 does not have a "no_renegotiation" warning, so
9768 we send a fatal alert and abort the connection. */
9769 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9770 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9771 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009772 }
9773 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9775#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9776 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9777 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9780 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9781 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009782 {
9783 return( ret );
9784 }
Paul Bakker48916f92012-09-16 19:57:18 +00009785 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009786 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9788 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9791 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009792 }
Paul Bakker48916f92012-09-16 19:57:18 +00009793 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009794
Hanno Becker90333da2017-10-10 11:27:13 +01009795 /* At this point, we don't know whether the renegotiation has been
9796 * completed or not. The cases to consider are the following:
9797 * 1) The renegotiation is complete. In this case, no new record
9798 * has been read yet.
9799 * 2) The renegotiation is incomplete because the client received
9800 * an application data record while awaiting the ServerHello.
9801 * 3) The renegotiation is incomplete because the client received
9802 * a non-handshake, non-application data message while awaiting
9803 * the ServerHello.
9804 * In each of these case, looping will be the proper action:
9805 * - For 1), the next iteration will read a new record and check
9806 * if it's application data.
9807 * - For 2), the loop condition isn't satisfied as application data
9808 * is present, hence continue is the same as break
9809 * - For 3), the loop condition is satisfied and read_record
9810 * will re-deliver the message that was held back by the client
9811 * when expecting the ServerHello.
9812 */
9813 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009814 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009815#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009817 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009818 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009819 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009820 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009823 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009825 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009826 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9831 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009834 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009835 }
9836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9840 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009841 }
9842
9843 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009844
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009845 /* We're going to return something now, cancel timer,
9846 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009848 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009849
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009851 /* If we requested renego but received AppData, resend HelloRequest.
9852 * Do it now, after setting in_offt, to avoid taking this branch
9853 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009857 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009858 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009861 return( ret );
9862 }
9863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009865#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009866 }
9867
9868 n = ( len < ssl->in_msglen )
9869 ? len : ssl->in_msglen;
9870
9871 memcpy( buf, ssl->in_offt, n );
9872 ssl->in_msglen -= n;
9873
9874 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009875 {
9876 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009877 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009878 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009879 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009880 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009881 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009882 /* more data available */
9883 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009887
Paul Bakker23986e52011-04-24 08:57:21 +00009888 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009889}
9890
9891/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009892 * Send application data to be encrypted by the SSL layer, taking care of max
9893 * fragment length and buffer size.
9894 *
9895 * According to RFC 5246 Section 6.2.1:
9896 *
9897 * Zero-length fragments of Application data MAY be sent as they are
9898 * potentially useful as a traffic analysis countermeasure.
9899 *
9900 * Therefore, it is possible that the input message length is 0 and the
9901 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009902 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009903static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009904 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009905{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009906 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9907 const size_t max_len = (size_t) ret;
9908
9909 if( ret < 0 )
9910 {
9911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9912 return( ret );
9913 }
9914
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009915 if( len > max_len )
9916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009918 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009921 "maximum fragment length: %d > %d",
9922 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009924 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009925 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009926#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009927#if defined(MBEDTLS_SSL_PROTO_TLS)
9928 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009929 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009930 }
9931#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009932 }
Paul Bakker887bd502011-06-08 13:10:54 +00009933
Paul Bakker5121ce52009-01-03 21:22:43 +00009934 if( ssl->out_left != 0 )
9935 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009936 /*
9937 * The user has previously tried to send the data and
9938 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9939 * written. In this case, we expect the high-level write function
9940 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009945 return( ret );
9946 }
9947 }
Paul Bakker887bd502011-06-08 13:10:54 +00009948 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009949 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009950 /*
9951 * The user is trying to send a message the first time, so we need to
9952 * copy the data into the internal buffers and setup the data structure
9953 * to keep track of partial writes
9954 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009955 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009957 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009958
Hanno Becker67bc7c32018-08-06 11:33:50 +01009959 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009962 return( ret );
9963 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009964 }
9965
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009966 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009967}
9968
9969/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009970 * Write application data, doing 1/n-1 splitting if necessary.
9971 *
9972 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009973 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009974 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009977static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009978 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009979{
9980 int ret;
9981
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009982 if( ssl->conf->cbc_record_splitting ==
9983 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009984 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009985 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9986 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9987 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009988 {
9989 return( ssl_write_real( ssl, buf, len ) );
9990 }
9991
9992 if( ssl->split_done == 0 )
9993 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009994 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009995 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009996 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009997 }
9998
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009999 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10000 return( ret );
10001 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010002
10003 return( ret + 1 );
10004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010006
10007/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010008 * Write application data (public-facing wrapper)
10009 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010010int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010011{
10012 int ret;
10013
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010015
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010016 if( ssl == NULL || ssl->conf == NULL )
10017 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10018
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010019#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010020 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10021 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010022 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010023 return( ret );
10024 }
10025#endif
10026
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010027 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010028 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010029 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010030 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010032 return( ret );
10033 }
10034 }
10035
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010036#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010037 ret = ssl_write_split( ssl, buf, len );
10038#else
10039 ret = ssl_write_real( ssl, buf, len );
10040#endif
10041
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010042 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010043
10044 return( ret );
10045}
10046
10047/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010048 * Notify the peer that the connection is being closed
10049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010051{
10052 int ret;
10053
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010054 if( ssl == NULL || ssl->conf == NULL )
10055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010058
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010059 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10065 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10066 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010069 return( ret );
10070 }
10071 }
10072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010074
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010075 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010076}
10077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010079{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010080 if( transform == NULL )
10081 return;
10082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010084 deflateEnd( &transform->ctx_deflate );
10085 inflateEnd( &transform->ctx_inflate );
10086#endif
10087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10089 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010090
Hanno Becker92231322018-01-03 15:32:51 +000010091#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 mbedtls_md_free( &transform->md_ctx_enc );
10093 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010094#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010095
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010096 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010097}
10098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#if defined(MBEDTLS_X509_CRT_PARSE_C)
10100static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010102 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010103
10104 while( cur != NULL )
10105 {
10106 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010108 cur = next;
10109 }
10110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010111#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010112
Hanno Becker0271f962018-08-16 13:23:47 +010010113#if defined(MBEDTLS_SSL_PROTO_DTLS)
10114
10115static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10116{
10117 unsigned offset;
10118 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10119
10120 if( hs == NULL )
10121 return;
10122
Hanno Becker283f5ef2018-08-24 09:34:47 +010010123 ssl_free_buffered_record( ssl );
10124
Hanno Becker0271f962018-08-16 13:23:47 +010010125 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010126 ssl_buffering_free_slot( ssl, offset );
10127}
10128
10129static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10130 uint8_t slot )
10131{
10132 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10133 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010134
10135 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10136 return;
10137
Hanno Beckere605b192018-08-21 15:59:07 +010010138 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010139 {
Hanno Beckere605b192018-08-21 15:59:07 +010010140 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010141 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010142 mbedtls_free( hs_buf->data );
10143 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010144 }
10145}
10146
10147#endif /* MBEDTLS_SSL_PROTO_DTLS */
10148
Gilles Peskine9b562d52018-04-25 20:32:43 +020010149void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010150{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010151 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10152
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010153 if( handshake == NULL )
10154 return;
10155
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010156#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10157 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10158 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010159 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010160 handshake->async_in_progress = 0;
10161 }
10162#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10163
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010164#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10165 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10166 mbedtls_md5_free( &handshake->fin_md5 );
10167 mbedtls_sha1_free( &handshake->fin_sha1 );
10168#endif
10169#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10170#if defined(MBEDTLS_SHA256_C)
10171 mbedtls_sha256_free( &handshake->fin_sha256 );
10172#endif
10173#if defined(MBEDTLS_SHA512_C)
10174 mbedtls_sha512_free( &handshake->fin_sha512 );
10175#endif
10176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010178#if defined(MBEDTLS_DHM_C)
10179 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181#if defined(MBEDTLS_ECDH_C)
10182 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010183#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010184#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010185 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010186#if defined(MBEDTLS_SSL_CLI_C)
10187 mbedtls_free( handshake->ecjpake_cache );
10188 handshake->ecjpake_cache = NULL;
10189 handshake->ecjpake_cache_len = 0;
10190#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010191#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010192
Janos Follath4ae5c292016-02-10 11:27:43 +000010193#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10194 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010195 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010197#endif
10198
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010199#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10200 if( handshake->psk != NULL )
10201 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010202 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010203 mbedtls_free( handshake->psk );
10204 }
10205#endif
10206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10208 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010209 /*
10210 * Free only the linked list wrapper, not the keys themselves
10211 * since the belong to the SNI callback
10212 */
10213 if( handshake->sni_key_cert != NULL )
10214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010216
10217 while( cur != NULL )
10218 {
10219 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010221 cur = next;
10222 }
10223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010225
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010226#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010227 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010228#endif
10229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#if defined(MBEDTLS_SSL_PROTO_DTLS)
10231 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010232 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010233 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010234#endif
10235
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010236 mbedtls_platform_zeroize( handshake,
10237 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010238}
10239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010241{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010242 if( session == NULL )
10243 return;
10244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010246 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010247#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010248
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010249#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010251#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010252
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010253 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010254}
10255
Paul Bakker5121ce52009-01-03 21:22:43 +000010256/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010257 * Serialize a full SSL context
10258 */
10259int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10260 unsigned char *buf,
10261 size_t buf_len,
10262 size_t *olen )
10263{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010264 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010265 (void) ssl;
10266
10267 if( buf != NULL )
10268 memset( buf, 0, buf_len );
10269
10270 *olen = 0;
10271
10272 return( 0 );
10273}
10274
10275/*
10276 * Deserialize a full SSL context
10277 */
10278int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10279 const unsigned char *buf,
10280 size_t len )
10281{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010282 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010283 (void) ssl;
10284 (void) buf;
10285 (void) len;
10286
10287 return( 0 );
10288}
10289
10290/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010291 * Free an SSL context
10292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010294{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010295 if( ssl == NULL )
10296 return;
10297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010299
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010300 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010301 {
Angus Grattond8213d02016-05-25 20:56:48 +100010302 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010303 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010304 }
10305
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010306 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010307 {
Angus Grattond8213d02016-05-25 20:56:48 +100010308 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010310 }
10311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010313 if( ssl->compress_buf != NULL )
10314 {
Angus Grattond8213d02016-05-25 20:56:48 +100010315 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010317 }
10318#endif
10319
Paul Bakker48916f92012-09-16 19:57:18 +000010320 if( ssl->transform )
10321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322 mbedtls_ssl_transform_free( ssl->transform );
10323 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010324 }
10325
10326 if( ssl->handshake )
10327 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010328 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10330 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 mbedtls_free( ssl->handshake );
10333 mbedtls_free( ssl->transform_negotiate );
10334 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010335 }
10336
Paul Bakkerc0463502013-02-14 11:19:38 +010010337 if( ssl->session )
10338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 mbedtls_ssl_session_free( ssl->session );
10340 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010341 }
10342
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010344 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010345 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010346 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010348 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010349#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10352 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10355 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010356 }
10357#endif
10358
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010359#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010361#endif
10362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010364
Paul Bakker86f04f42013-02-14 11:20:09 +010010365 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010366 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010367}
10368
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010369/*
10370 * Initialze mbedtls_ssl_config
10371 */
10372void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10373{
10374 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010375
10376#if !defined(MBEDTLS_SSL_PROTO_TLS)
10377 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10378#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010379}
10380
Simon Butcherc97b6972015-12-27 23:48:17 +000010381#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010382static int ssl_preset_default_hashes[] = {
10383#if defined(MBEDTLS_SHA512_C)
10384 MBEDTLS_MD_SHA512,
10385 MBEDTLS_MD_SHA384,
10386#endif
10387#if defined(MBEDTLS_SHA256_C)
10388 MBEDTLS_MD_SHA256,
10389 MBEDTLS_MD_SHA224,
10390#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010391#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010392 MBEDTLS_MD_SHA1,
10393#endif
10394 MBEDTLS_MD_NONE
10395};
Simon Butcherc97b6972015-12-27 23:48:17 +000010396#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010397
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010398static int ssl_preset_suiteb_ciphersuites[] = {
10399 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10400 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10401 0
10402};
10403
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010404#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010405static int ssl_preset_suiteb_hashes[] = {
10406 MBEDTLS_MD_SHA256,
10407 MBEDTLS_MD_SHA384,
10408 MBEDTLS_MD_NONE
10409};
10410#endif
10411
10412#if defined(MBEDTLS_ECP_C)
10413static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10414 MBEDTLS_ECP_DP_SECP256R1,
10415 MBEDTLS_ECP_DP_SECP384R1,
10416 MBEDTLS_ECP_DP_NONE
10417};
10418#endif
10419
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010420/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010421 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010423int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010424 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010425{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010426#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010427 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010428#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010429
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010430 /* Use the functions here so that they are covered in tests,
10431 * but otherwise access member directly for efficiency */
10432 mbedtls_ssl_conf_endpoint( conf, endpoint );
10433 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010434
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010435 /*
10436 * Things that are common to all presets
10437 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010438#if defined(MBEDTLS_SSL_CLI_C)
10439 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10440 {
10441 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10442#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10443 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10444#endif
10445 }
10446#endif
10447
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010448#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010449 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010450#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010451
10452#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10453 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10454#endif
10455
10456#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10457 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010458 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010459 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010460#endif
10461
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010462#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10463 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10464#endif
10465
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010466#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010467 conf->f_cookie_write = ssl_cookie_write_dummy;
10468 conf->f_cookie_check = ssl_cookie_check_dummy;
10469#endif
10470
10471#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10472 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10473#endif
10474
Janos Follath088ce432017-04-10 12:42:31 +010010475#if defined(MBEDTLS_SSL_SRV_C)
10476 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10477#endif
10478
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010479#if defined(MBEDTLS_SSL_PROTO_DTLS)
10480 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10481 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10482#endif
10483
10484#if defined(MBEDTLS_SSL_RENEGOTIATION)
10485 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010486 memset( conf->renego_period, 0x00, 2 );
10487 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010488#endif
10489
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010490#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10491 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10492 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010493 const unsigned char dhm_p[] =
10494 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10495 const unsigned char dhm_g[] =
10496 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10497
Hanno Beckera90658f2017-10-04 15:29:08 +010010498 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10499 dhm_p, sizeof( dhm_p ),
10500 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010501 {
10502 return( ret );
10503 }
10504 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010505#endif
10506
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507 /*
10508 * Preset-specific defaults
10509 */
10510 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010511 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512 /*
10513 * NSA Suite B
10514 */
10515 case MBEDTLS_SSL_PRESET_SUITEB:
10516 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10517 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10518 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10519 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10520
10521 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10522 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10523 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10524 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10525 ssl_preset_suiteb_ciphersuites;
10526
10527#if defined(MBEDTLS_X509_CRT_PARSE_C)
10528 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010529#endif
10530
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010531#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010532 conf->sig_hashes = ssl_preset_suiteb_hashes;
10533#endif
10534
10535#if defined(MBEDTLS_ECP_C)
10536 conf->curve_list = ssl_preset_suiteb_curves;
10537#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010538 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010539
10540 /*
10541 * Default
10542 */
10543 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010544 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10545 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10546 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10547 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10548 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10549 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10550 MBEDTLS_SSL_MIN_MINOR_VERSION :
10551 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010552 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10553 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10554
10555#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010556 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010557 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10558#endif
10559
10560 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10561 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10562 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10563 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10564 mbedtls_ssl_list_ciphersuites();
10565
10566#if defined(MBEDTLS_X509_CRT_PARSE_C)
10567 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10568#endif
10569
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010570#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010571 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010572#endif
10573
10574#if defined(MBEDTLS_ECP_C)
10575 conf->curve_list = mbedtls_ecp_grp_id_list();
10576#endif
10577
10578#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10579 conf->dhm_min_bitlen = 1024;
10580#endif
10581 }
10582
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010583 return( 0 );
10584}
10585
10586/*
10587 * Free mbedtls_ssl_config
10588 */
10589void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10590{
10591#if defined(MBEDTLS_DHM_C)
10592 mbedtls_mpi_free( &conf->dhm_P );
10593 mbedtls_mpi_free( &conf->dhm_G );
10594#endif
10595
10596#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10597 if( conf->psk != NULL )
10598 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010599 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010600 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010601 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010602 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010603 }
10604
10605 if( conf->psk_identity != NULL )
10606 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010607 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010608 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010609 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010610 conf->psk_identity_len = 0;
10611 }
10612#endif
10613
10614#if defined(MBEDTLS_X509_CRT_PARSE_C)
10615 ssl_key_cert_free( conf->key_cert );
10616#endif
10617
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010618 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010619}
10620
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010621#if defined(MBEDTLS_PK_C) && \
10622 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010623/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010628#if defined(MBEDTLS_RSA_C)
10629 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10630 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632#if defined(MBEDTLS_ECDSA_C)
10633 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10634 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010637}
10638
Hanno Becker7e5437a2017-04-28 17:15:26 +010010639unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10640{
10641 switch( type ) {
10642 case MBEDTLS_PK_RSA:
10643 return( MBEDTLS_SSL_SIG_RSA );
10644 case MBEDTLS_PK_ECDSA:
10645 case MBEDTLS_PK_ECKEY:
10646 return( MBEDTLS_SSL_SIG_ECDSA );
10647 default:
10648 return( MBEDTLS_SSL_SIG_ANON );
10649 }
10650}
10651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010653{
10654 switch( sig )
10655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010656#if defined(MBEDTLS_RSA_C)
10657 case MBEDTLS_SSL_SIG_RSA:
10658 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010659#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660#if defined(MBEDTLS_ECDSA_C)
10661 case MBEDTLS_SSL_SIG_ECDSA:
10662 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010663#endif
10664 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010665 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010666 }
10667}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010668#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010669
Hanno Becker7e5437a2017-04-28 17:15:26 +010010670#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10671 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10672
10673/* Find an entry in a signature-hash set matching a given hash algorithm. */
10674mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10675 mbedtls_pk_type_t sig_alg )
10676{
10677 switch( sig_alg )
10678 {
10679 case MBEDTLS_PK_RSA:
10680 return( set->rsa );
10681 case MBEDTLS_PK_ECDSA:
10682 return( set->ecdsa );
10683 default:
10684 return( MBEDTLS_MD_NONE );
10685 }
10686}
10687
10688/* Add a signature-hash-pair to a signature-hash set */
10689void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10690 mbedtls_pk_type_t sig_alg,
10691 mbedtls_md_type_t md_alg )
10692{
10693 switch( sig_alg )
10694 {
10695 case MBEDTLS_PK_RSA:
10696 if( set->rsa == MBEDTLS_MD_NONE )
10697 set->rsa = md_alg;
10698 break;
10699
10700 case MBEDTLS_PK_ECDSA:
10701 if( set->ecdsa == MBEDTLS_MD_NONE )
10702 set->ecdsa = md_alg;
10703 break;
10704
10705 default:
10706 break;
10707 }
10708}
10709
10710/* Allow exactly one hash algorithm for each signature. */
10711void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10712 mbedtls_md_type_t md_alg )
10713{
10714 set->rsa = md_alg;
10715 set->ecdsa = md_alg;
10716}
10717
10718#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10719 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10720
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010721/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010722 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010725{
10726 switch( hash )
10727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728#if defined(MBEDTLS_MD5_C)
10729 case MBEDTLS_SSL_HASH_MD5:
10730 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010731#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732#if defined(MBEDTLS_SHA1_C)
10733 case MBEDTLS_SSL_HASH_SHA1:
10734 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736#if defined(MBEDTLS_SHA256_C)
10737 case MBEDTLS_SSL_HASH_SHA224:
10738 return( MBEDTLS_MD_SHA224 );
10739 case MBEDTLS_SSL_HASH_SHA256:
10740 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742#if defined(MBEDTLS_SHA512_C)
10743 case MBEDTLS_SSL_HASH_SHA384:
10744 return( MBEDTLS_MD_SHA384 );
10745 case MBEDTLS_SSL_HASH_SHA512:
10746 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010747#endif
10748 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010750 }
10751}
10752
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010753/*
10754 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10755 */
10756unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10757{
10758 switch( md )
10759 {
10760#if defined(MBEDTLS_MD5_C)
10761 case MBEDTLS_MD_MD5:
10762 return( MBEDTLS_SSL_HASH_MD5 );
10763#endif
10764#if defined(MBEDTLS_SHA1_C)
10765 case MBEDTLS_MD_SHA1:
10766 return( MBEDTLS_SSL_HASH_SHA1 );
10767#endif
10768#if defined(MBEDTLS_SHA256_C)
10769 case MBEDTLS_MD_SHA224:
10770 return( MBEDTLS_SSL_HASH_SHA224 );
10771 case MBEDTLS_MD_SHA256:
10772 return( MBEDTLS_SSL_HASH_SHA256 );
10773#endif
10774#if defined(MBEDTLS_SHA512_C)
10775 case MBEDTLS_MD_SHA384:
10776 return( MBEDTLS_SSL_HASH_SHA384 );
10777 case MBEDTLS_MD_SHA512:
10778 return( MBEDTLS_SSL_HASH_SHA512 );
10779#endif
10780 default:
10781 return( MBEDTLS_SSL_HASH_NONE );
10782 }
10783}
10784
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010785#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010786/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010787 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010788 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010789 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010790int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010791{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010793
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010794 if( ssl->conf->curve_list == NULL )
10795 return( -1 );
10796
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010797 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010798 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010799 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010800
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010801 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010802}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010803#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010804
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010805#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010806/*
10807 * Check if a hash proposed by the peer is in our list.
10808 * Return 0 if we're willing to use it, -1 otherwise.
10809 */
10810int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10811 mbedtls_md_type_t md )
10812{
10813 const int *cur;
10814
10815 if( ssl->conf->sig_hashes == NULL )
10816 return( -1 );
10817
10818 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10819 if( *cur == (int) md )
10820 return( 0 );
10821
10822 return( -1 );
10823}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010824#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826#if defined(MBEDTLS_X509_CRT_PARSE_C)
10827int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10828 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010829 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010830 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010831{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010832 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010833#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010834 int usage = 0;
10835#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010837 const char *ext_oid;
10838 size_t ext_len;
10839#endif
10840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010841#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10842 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010843 ((void) cert);
10844 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010845 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010846#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10849 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010850 {
10851 /* Server part of the key exchange */
10852 switch( ciphersuite->key_exchange )
10853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854 case MBEDTLS_KEY_EXCHANGE_RSA:
10855 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010856 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010857 break;
10858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10860 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10861 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10862 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010863 break;
10864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10866 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010867 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010868 break;
10869
10870 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 case MBEDTLS_KEY_EXCHANGE_NONE:
10872 case MBEDTLS_KEY_EXCHANGE_PSK:
10873 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10874 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010875 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010876 usage = 0;
10877 }
10878 }
10879 else
10880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10882 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010883 }
10884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010885 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010886 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010887 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010888 ret = -1;
10889 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010890#else
10891 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010892#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10895 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10898 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010899 }
10900 else
10901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10903 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010904 }
10905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010907 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010908 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010909 ret = -1;
10910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010911#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010912
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010913 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010916
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010917/*
10918 * Convert version numbers to/from wire format
10919 * and, for DTLS, to/from TLS equivalent.
10920 *
10921 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010922 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010923 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10924 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010927 unsigned char ver[2] )
10928{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010929#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10930 ((void) transport);
10931#endif
10932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010934 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010936 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010937 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10938
10939 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10940 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10941 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010942 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010943#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010944#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010945 {
10946 ver[0] = (unsigned char) major;
10947 ver[1] = (unsigned char) minor;
10948 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010949#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010950}
10951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010953 const unsigned char ver[2] )
10954{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010955#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10956 ((void) transport);
10957#endif
10958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010960 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010961 {
10962 *major = 255 - ver[0] + 2;
10963 *minor = 255 - ver[1] + 1;
10964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010966 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10967 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010968 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010969#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010970#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010971 {
10972 *major = ver[0];
10973 *minor = ver[1];
10974 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020010975#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010976}
10977
Simon Butcher99000142016-10-13 17:21:01 +010010978int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10979{
10980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10981 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10982 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10983
10984 switch( md )
10985 {
10986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10987#if defined(MBEDTLS_MD5_C)
10988 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010989 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010990#endif
10991#if defined(MBEDTLS_SHA1_C)
10992 case MBEDTLS_SSL_HASH_SHA1:
10993 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10994 break;
10995#endif
10996#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10997#if defined(MBEDTLS_SHA512_C)
10998 case MBEDTLS_SSL_HASH_SHA384:
10999 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11000 break;
11001#endif
11002#if defined(MBEDTLS_SHA256_C)
11003 case MBEDTLS_SSL_HASH_SHA256:
11004 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11005 break;
11006#endif
11007 default:
11008 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11009 }
11010
11011 return 0;
11012#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11013 (void) ssl;
11014 (void) md;
11015
11016 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11017#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11018}
11019
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11021 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11022int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11023 unsigned char *output,
11024 unsigned char *data, size_t data_len )
11025{
11026 int ret = 0;
11027 mbedtls_md5_context mbedtls_md5;
11028 mbedtls_sha1_context mbedtls_sha1;
11029
11030 mbedtls_md5_init( &mbedtls_md5 );
11031 mbedtls_sha1_init( &mbedtls_sha1 );
11032
11033 /*
11034 * digitally-signed struct {
11035 * opaque md5_hash[16];
11036 * opaque sha_hash[20];
11037 * };
11038 *
11039 * md5_hash
11040 * MD5(ClientHello.random + ServerHello.random
11041 * + ServerParams);
11042 * sha_hash
11043 * SHA(ClientHello.random + ServerHello.random
11044 * + ServerParams);
11045 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011046 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011047 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011049 goto exit;
11050 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011051 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011052 ssl->handshake->randbytes, 64 ) ) != 0 )
11053 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011055 goto exit;
11056 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011057 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011058 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011059 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011060 goto exit;
11061 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011062 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011063 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011065 goto exit;
11066 }
11067
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011068 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011069 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011071 goto exit;
11072 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011073 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011074 ssl->handshake->randbytes, 64 ) ) != 0 )
11075 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011077 goto exit;
11078 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011079 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011080 data_len ) ) != 0 )
11081 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011083 goto exit;
11084 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011085 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011086 output + 16 ) ) != 0 )
11087 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011089 goto exit;
11090 }
11091
11092exit:
11093 mbedtls_md5_free( &mbedtls_md5 );
11094 mbedtls_sha1_free( &mbedtls_sha1 );
11095
11096 if( ret != 0 )
11097 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11098 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11099
11100 return( ret );
11101
11102}
11103#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11104 MBEDTLS_SSL_PROTO_TLS1_1 */
11105
11106#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11107 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11108int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011109 unsigned char *hash, size_t *hashlen,
11110 unsigned char *data, size_t data_len,
11111 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011112{
11113 int ret = 0;
11114 mbedtls_md_context_t ctx;
11115 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011116 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011117
11118 mbedtls_md_init( &ctx );
11119
11120 /*
11121 * digitally-signed struct {
11122 * opaque client_random[32];
11123 * opaque server_random[32];
11124 * ServerDHParams params;
11125 * };
11126 */
11127 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11128 {
11129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11130 goto exit;
11131 }
11132 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11133 {
11134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11135 goto exit;
11136 }
11137 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11138 {
11139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11140 goto exit;
11141 }
11142 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11143 {
11144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11145 goto exit;
11146 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011147 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011148 {
11149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11150 goto exit;
11151 }
11152
11153exit:
11154 mbedtls_md_free( &ctx );
11155
11156 if( ret != 0 )
11157 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11158 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11159
11160 return( ret );
11161}
11162#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11163 MBEDTLS_SSL_PROTO_TLS1_2 */
11164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011165#endif /* MBEDTLS_SSL_TLS_C */