blob: 4d7c624e36c3751d20bc30aa903381836ef819d2 [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 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399 if( src->ticket != NULL )
400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200401 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200402 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 memcpy( dst->ticket, src->ticket, src->ticket_len );
406 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408
409 return( 0 );
410}
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
413int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200414 const unsigned char *key_enc, const unsigned char *key_dec,
415 size_t keylen,
416 const unsigned char *iv_enc, const unsigned char *iv_dec,
417 size_t ivlen,
418 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200419 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
421int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
422int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
425#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000426
Paul Bakker5121ce52009-01-03 21:22:43 +0000427/*
428 * Key material generation
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200431static int ssl3_prf( const unsigned char *secret, size_t slen,
432 const char *label,
433 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000434 unsigned char *dstbuf, size_t dlen )
435{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100436 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000437 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200438 mbedtls_md5_context md5;
439 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000440 unsigned char padding[16];
441 unsigned char sha1sum[20];
442 ((void)label);
443
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200444 mbedtls_md5_init( &md5 );
445 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200446
Paul Bakker5f70b252012-09-13 14:23:06 +0000447 /*
448 * SSLv3:
449 * block =
450 * MD5( secret + SHA1( 'A' + secret + random ) ) +
451 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
452 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
453 * ...
454 */
455 for( i = 0; i < dlen / 16; i++ )
456 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200457 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100459 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100460 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100461 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100462 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100463 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100464 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000469
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100470 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100471 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100472 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100473 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100474 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000478 }
479
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200481 mbedtls_md5_free( &md5 );
482 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500484 mbedtls_platform_zeroize( padding, sizeof( padding ) );
485 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200492static int tls1_prf( const unsigned char *secret, size_t slen,
493 const char *label,
494 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000495 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496{
Paul Bakker23986e52011-04-24 08:57:21 +0000497 size_t nb, hs;
498 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200499 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 unsigned char tmp[128];
501 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 const mbedtls_md_info_t *md_info;
503 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100504 int ret;
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511 hs = ( slen + 1 ) / 2;
512 S1 = secret;
513 S2 = secret + slen - hs;
514
515 nb = strlen( label );
516 memcpy( tmp + 20, label, nb );
517 memcpy( tmp + 20 + nb, random, rlen );
518 nb += rlen;
519
520 /*
521 * First compute P_md5(secret,label+random)[0..dlen]
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100527 return( ret );
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
530 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
531 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533 for( i = 0; i < dlen; i += 16 )
534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_reset ( &md_ctx );
536 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
537 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_hmac_reset ( &md_ctx );
540 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
541 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
544
545 for( j = 0; j < k; j++ )
546 dstbuf[i + j] = h_i[j];
547 }
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100550
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 /*
552 * XOR out with P_sha1(secret,label+random)[0..dlen]
553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100558 return( ret );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
561 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
562 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 for( i = 0; i < dlen; i += 20 )
565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
568 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_hmac_reset ( &md_ctx );
571 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
572 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
575
576 for( j = 0; j < k; j++ )
577 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
578 }
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500582 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
583 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 return( 0 );
586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
590static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100591 const unsigned char *secret, size_t slen,
592 const char *label,
593 const unsigned char *random, size_t rlen,
594 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000595{
596 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100597 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000598 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
600 const mbedtls_md_info_t *md_info;
601 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100602 int ret;
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100610
611 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000613
614 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100615 memcpy( tmp + md_len, label, nb );
616 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000617 nb += rlen;
618
619 /*
620 * Compute P_<hash>(secret, label + random)[0..dlen]
621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100623 return( ret );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
626 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
627 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100628
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100629 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_md_hmac_reset ( &md_ctx );
632 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
633 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_md_hmac_reset ( &md_ctx );
636 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
637 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000638
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100639 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000640
641 for( j = 0; j < k; j++ )
642 dstbuf[i + j] = h_i[j];
643 }
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100646
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500647 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
648 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000649
650 return( 0 );
651}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100654static int tls_prf_sha256( const unsigned char *secret, size_t slen,
655 const char *label,
656 const unsigned char *random, size_t rlen,
657 unsigned char *dstbuf, size_t dlen )
658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100660 label, random, rlen, dstbuf, dlen ) );
661}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200665static int tls_prf_sha384( const unsigned char *secret, size_t slen,
666 const char *label,
667 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000668 unsigned char *dstbuf, size_t dlen )
669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_SHA512_C */
674#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
679 defined(MBEDTLS_SSL_PROTO_TLS1_1)
680static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200681#endif
Paul Bakker380da532012-04-18 16:10:25 +0000682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200684static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200686#endif
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200689static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200691#endif
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
694#if defined(MBEDTLS_SHA256_C)
695static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200696static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SHA512_C)
701static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200702static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200707/* Type for the TLS PRF */
708typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
709 const unsigned char *, size_t,
710 unsigned char *, size_t);
711
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200712/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200713 * Populate a transform structure with session keys and all the other
714 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200715 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200716 * Parameters:
717 * - [in/out]: transform: structure to populate
718 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200719 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200720 * - [in] ciphersuite
721 * - [in] master
722 * - [in] encrypt_then_mac
723 * - [in] trunc_hmac
724 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200725 * - [in] tls_prf: pointer to PRF to use for key derivation
726 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200727 * - [in] minor_ver: SSL/TLS minor version
728 * - [in] endpoint: client or server
729 * - [in] ssl: optionally used for:
730 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
731 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
732 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200733 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200734static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200735 int ciphersuite,
736 const unsigned char master[48],
737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
738 int encrypt_then_mac,
739#endif
740#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
741 int trunc_hmac,
742#endif
743#if defined(MBEDTLS_ZLIB_SUPPORT)
744 int compression,
745#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200746 ssl_tls_prf_t tls_prf,
747 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200748 int minor_ver,
749 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200750 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000751{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200752 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 unsigned char keyblk[256];
754 unsigned char *key1;
755 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100756 unsigned char *mac_enc;
757 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000758 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200759 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000760 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000761 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 const mbedtls_cipher_info_t *cipher_info;
763 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100764
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
766 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
767 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200768 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200769 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000770#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000771
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200772 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200774 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000775#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200776 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200778 /*
779 * Get various info structures
780 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200781 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200782 if( ciphersuite_info == NULL )
783 {
784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200785 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
787 }
788
Hanno Becker8759e162017-12-27 21:34:08 +0000789 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100790 if( cipher_info == NULL )
791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000793 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100795 }
796
Hanno Becker8759e162017-12-27 21:34:08 +0000797 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100798 if( md_info == NULL )
799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000801 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100803 }
804
Hanno Beckera5a2b082019-05-15 14:03:01 +0100805#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100806 /* Copy own and peer's CID if the use of the CID
807 * extension has been negotiated. */
808 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
809 {
810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100811
Hanno Becker4932f9f2019-05-03 15:23:51 +0100812 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100813 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100814 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100815 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100816
817 transform->out_cid_len = ssl->handshake->peer_cid_len;
818 memcpy( transform->out_cid, ssl->handshake->peer_cid,
819 ssl->handshake->peer_cid_len );
820 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
821 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100822 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100824
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200826 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000827 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200828 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100829 if( ret != 0 )
830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100832 return( ret );
833 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200836 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200837 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200838 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 /*
842 * Determine the appropriate key, IV and MAC length.
843 */
Paul Bakker68884e32013-01-07 18:20:04 +0100844
Hanno Beckere7f2df02017-12-27 08:17:40 +0000845 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200846
Hanno Beckerf1229442018-01-03 15:32:31 +0000847#if defined(MBEDTLS_GCM_C) || \
848 defined(MBEDTLS_CCM_C) || \
849 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200851 cipher_info->mode == MBEDTLS_MODE_CCM ||
852 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 {
Hanno Becker8759e162017-12-27 21:34:08 +0000854 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200855
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200856 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000857 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000858 transform->taglen =
859 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200860
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200861 /* All modes haves 96-bit IVs;
862 * GCM and CCM has 4 implicit and 8 explicit bytes
863 * ChachaPoly has all 12 bytes implicit
864 */
Paul Bakker68884e32013-01-07 18:20:04 +0100865 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200866 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
867 transform->fixed_ivlen = 12;
868 else
869 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200870
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 /* Minimum length of encrypted record */
872 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000873 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100874 }
875 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000876#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
877#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
878 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
879 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100880 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200881 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
883 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200886 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100887 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200889 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000890 mac_key_len = mbedtls_md_get_size( md_info );
891 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200894 /*
895 * If HMAC is to be truncated, we shall keep the leftmost bytes,
896 * (rfc 6066 page 13 or rfc 2104 section 4),
897 * so we only need to adjust the length here.
898 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200899 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000902
903#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
904 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000905 * HMAC implementation which also truncates the key
906 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000907 mac_key_len = transform->maclen;
908#endif
909 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200911
912 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100913 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200915 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200917 transform->minlen = transform->maclen;
918 else
Paul Bakker68884e32013-01-07 18:20:04 +0100919 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200920 /*
921 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100922 * 1. if EtM is in use: one block plus MAC
923 * otherwise: * first multiple of blocklen greater than maclen
924 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200927 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100928 {
929 transform->minlen = transform->maclen
930 + cipher_info->block_size;
931 }
932 else
933#endif
934 {
935 transform->minlen = transform->maclen
936 + cipher_info->block_size
937 - transform->maclen % cipher_info->block_size;
938 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200941 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
942 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200943 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100944 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200947 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
948 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200949 {
950 transform->minlen += transform->ivlen;
951 }
952 else
953#endif
954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
956 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200957 }
Paul Bakker68884e32013-01-07 18:20:04 +0100958 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000960 else
961#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
962 {
963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
965 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Hanno Beckere7f2df02017-12-27 08:17:40 +0000967 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
968 (unsigned) keylen,
969 (unsigned) transform->minlen,
970 (unsigned) transform->ivlen,
971 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
973 /*
974 * Finally setup the cipher contexts, IVs and MAC secrets.
975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200977 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000978 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000979 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000980 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker68884e32013-01-07 18:20:04 +0100982 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000983 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000985 /*
986 * This is not used in TLS v1.1.
987 */
Paul Bakker48916f92012-09-16 19:57:18 +0000988 iv_copy_len = ( transform->fixed_ivlen ) ?
989 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000990 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
991 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000992 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993 }
994 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#endif /* MBEDTLS_SSL_CLI_C */
996#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Beckere7f2df02017-12-27 08:17:40 +0000999 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Hanno Becker81c7b182017-11-09 18:39:33 +00001002 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001003 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1011 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001019 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001020
Hanno Becker92231322018-01-03 15:32:51 +00001021#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001023 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001024 {
Hanno Becker92231322018-01-03 15:32:51 +00001025 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1028 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001029 }
1030
Hanno Becker81c7b182017-11-09 18:39:33 +00001031 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1032 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001033 }
1034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1036#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1037 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001038 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001039 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001040 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1041 For AEAD-based ciphersuites, there is nothing to do here. */
1042 if( mac_key_len != 0 )
1043 {
1044 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1045 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1046 }
Paul Bakker68884e32013-01-07 18:20:04 +01001047 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001048 else
1049#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1052 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001053 }
Hanno Becker92231322018-01-03 15:32:51 +00001054#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1057 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001058 {
1059 int ret = 0;
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001062
Hanno Beckere7f2df02017-12-27 08:17:40 +00001063 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001064 transform->iv_enc, transform->iv_dec,
1065 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001066 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001067 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1070 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001071 }
1072 }
Hanno Becker92231322018-01-03 15:32:51 +00001073#else
1074 ((void) mac_dec);
1075 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001077
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001078#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1079 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001080 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001081 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001082 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001084 iv_copy_len );
1085 }
1086#endif
1087
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001088 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001089 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001092 return( ret );
1093 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001094
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001095 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001096 cipher_info ) ) != 0 )
1097 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001099 return( ret );
1100 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001103 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001107 return( ret );
1108 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001111 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", 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 defined(MBEDTLS_CIPHER_MODE_CBC)
1119 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1122 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001126 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1129 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 return( ret );
1133 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001136
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001137 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001139 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001141 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001144
Paul Bakker48916f92012-09-16 19:57:18 +00001145 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1146 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001148 if( deflateInit( &transform->ctx_deflate,
1149 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001150 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1153 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001154 }
1155 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001157
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 return( 0 );
1159}
1160
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001161/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001162 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001163 *
1164 * Inputs:
1165 * - SSL/TLS minor version
1166 * - hash associated with the ciphersuite (only used by TLS 1.2)
1167 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001168 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001169 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1170 */
1171static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1172 int minor_ver,
1173 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001174{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001175#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1176 (void) hash;
1177#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001178
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001179#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001180 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001181 {
1182 handshake->tls_prf = ssl3_prf;
1183 handshake->calc_verify = ssl_calc_verify_ssl;
1184 handshake->calc_finished = ssl_calc_finished_ssl;
1185 }
1186 else
1187#endif
1188#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001190 {
1191 handshake->tls_prf = tls1_prf;
1192 handshake->calc_verify = ssl_calc_verify_tls;
1193 handshake->calc_finished = ssl_calc_finished_tls;
1194 }
1195 else
1196#endif
1197#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1198#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001199 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1200 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = tls_prf_sha384;
1203 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1204 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls_prf_sha256;
1212 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1213 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1214 }
1215 else
1216#endif
1217#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1218 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1220 }
1221
1222 return( 0 );
1223}
1224
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001225/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001226 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001227 *
1228 * Parameters:
1229 * [in/out] handshake
1230 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1231 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001232 * [out] master
1233 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001234 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001235static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001237 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001238{
1239 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001240
1241#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001242 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001243 (void) ssl;
1244#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001247 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1249 return( 0 );
1250 }
1251
1252 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1253 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254
1255#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001257 {
1258 unsigned char session_hash[48];
1259 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001260
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001261 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001262
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001263 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1264 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001265
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001266 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001267 "extended master secret",
1268 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001269 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001270 }
1271 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001272#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001273 {
1274 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1275 "master secret",
1276 handshake->randbytes, 64,
1277 master, 48 );
1278 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001279 if( ret != 0 )
1280 {
1281 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1282 return( ret );
1283 }
1284
1285 mbedtls_platform_zeroize( handshake->premaster,
1286 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287
1288 return( 0 );
1289}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001290
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001291int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1292{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001293 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001294 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1295 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001296
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1298
1299 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001300 ret = ssl_set_handshake_prfs( ssl->handshake,
1301 ssl->minor_ver,
1302 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001303 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001304 {
1305 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001306 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001307 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001308
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001309 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001310 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001311 ssl->session_negotiate->master,
1312 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001313 if( ret != 0 )
1314 {
1315 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1316 return( ret );
1317 }
1318
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001319 /* Swap the client and server random values:
1320 * - MS derivation wanted client+server (RFC 5246 8.1)
1321 * - key derivation wants server+client (RFC 5246 6.3) */
1322 {
1323 unsigned char tmp[64];
1324 memcpy( tmp, ssl->handshake->randbytes, 64 );
1325 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1326 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1327 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1328 }
1329
1330 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001331 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001332 ssl->session_negotiate->ciphersuite,
1333 ssl->session_negotiate->master,
1334#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1335 ssl->session_negotiate->encrypt_then_mac,
1336#endif
1337#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1338 ssl->session_negotiate->trunc_hmac,
1339#endif
1340#if defined(MBEDTLS_ZLIB_SUPPORT)
1341 ssl->session_negotiate->compression,
1342#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001343 ssl->handshake->tls_prf,
1344 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001345 ssl->minor_ver,
1346 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001347 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001348 if( ret != 0 )
1349 {
1350 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1351 return( ret );
1352 }
1353
1354 /* We no longer need Server/ClientHello.random values */
1355 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1356 sizeof( ssl->handshake->randbytes ) );
1357
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001358 /* Allocate compression buffer */
1359#if defined(MBEDTLS_ZLIB_SUPPORT)
1360 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1361 ssl->compress_buf == NULL )
1362 {
1363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1364 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1365 if( ssl->compress_buf == NULL )
1366 {
1367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001368 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001369 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1370 }
1371 }
1372#endif
1373
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1375
1376 return( 0 );
1377}
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001380void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1381 unsigned char hash[36],
1382 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001384 mbedtls_md5_context md5;
1385 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 unsigned char pad_1[48];
1387 unsigned char pad_2[48];
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001391 mbedtls_md5_init( &md5 );
1392 mbedtls_sha1_init( &sha1 );
1393
1394 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1395 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001396
Paul Bakker380da532012-04-18 16:10:25 +00001397 memset( pad_1, 0x36, 48 );
1398 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001400 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1401 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1402 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001404 mbedtls_md5_starts_ret( &md5 );
1405 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1406 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1407 mbedtls_md5_update_ret( &md5, hash, 16 );
1408 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001409
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001410 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1411 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1412 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001413
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001414 mbedtls_sha1_starts_ret( &sha1 );
1415 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1416 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1417 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1418 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001419
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001420 *hlen = 36;
1421
1422 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001424
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001425 mbedtls_md5_free( &md5 );
1426 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001427
Paul Bakker380da532012-04-18 16:10:25 +00001428 return;
1429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001433void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1434 unsigned char hash[36],
1435 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001436{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001437 mbedtls_md5_context md5;
1438 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001442 mbedtls_md5_init( &md5 );
1443 mbedtls_sha1_init( &sha1 );
1444
1445 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1446 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001447
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001448 mbedtls_md5_finish_ret( &md5, hash );
1449 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001450
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001451 *hlen = 36;
1452
1453 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001455
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001456 mbedtls_md5_free( &md5 );
1457 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001458
Paul Bakker380da532012-04-18 16:10:25 +00001459 return;
1460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1464#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001465void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1466 unsigned char hash[32],
1467 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001468{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001469 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001470
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001471 mbedtls_sha256_init( &sha256 );
1472
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001474
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001475 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001476 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001477
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001478 *hlen = 32;
1479
1480 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001482
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001483 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001484
Paul Bakker380da532012-04-18 16:10:25 +00001485 return;
1486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001490void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1491 unsigned char hash[48],
1492 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001493{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001494 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001496 mbedtls_sha512_init( &sha512 );
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001500 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001501 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001503 *hlen = 48;
1504
1505 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001508 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001509
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 return;
1511}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#endif /* MBEDTLS_SHA512_C */
1513#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1516int 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 +02001517{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001518 unsigned char *p = ssl->handshake->premaster;
1519 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001520 const unsigned char *psk = ssl->conf->psk;
1521 size_t psk_len = ssl->conf->psk_len;
1522
1523 /* If the psk callback was called, use its result */
1524 if( ssl->handshake->psk != NULL )
1525 {
1526 psk = ssl->handshake->psk;
1527 psk_len = ssl->handshake->psk_len;
1528 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001529
1530 /*
1531 * PMS = struct {
1532 * opaque other_secret<0..2^16-1>;
1533 * opaque psk<0..2^16-1>;
1534 * };
1535 * with "other_secret" depending on the particular key exchange
1536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1538 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001539 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001540 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001542
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001543 *(p++) = (unsigned char)( psk_len >> 8 );
1544 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001545
1546 if( end < p || (size_t)( end - p ) < psk_len )
1547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1548
1549 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001550 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001551 }
1552 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1554#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1555 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001556 {
1557 /*
1558 * other_secret already set by the ClientKeyExchange message,
1559 * and is 48 bytes long
1560 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001561 if( end - p < 2 )
1562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1563
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001564 *p++ = 0;
1565 *p++ = 48;
1566 p += 48;
1567 }
1568 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1570#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1571 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001572 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001573 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001574 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001575
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001576 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001578 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001579 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001582 return( ret );
1583 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001584 *(p++) = (unsigned char)( len >> 8 );
1585 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001586 p += len;
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589 }
1590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1592#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1593 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001594 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001595 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 size_t zlen;
1597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001599 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001600 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 return( ret );
1604 }
1605
1606 *(p++) = (unsigned char)( zlen >> 8 );
1607 *(p++) = (unsigned char)( zlen );
1608 p += zlen;
1609
Janos Follath3fbdada2018-08-15 10:26:53 +01001610 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1611 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 }
1613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1617 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 }
1619
1620 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001621 if( end - p < 2 )
1622 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001623
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001624 *(p++) = (unsigned char)( psk_len >> 8 );
1625 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001626
1627 if( end < p || (size_t)( end - p ) < psk_len )
1628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1629
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001630 memcpy( p, psk, psk_len );
1631 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632
1633 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1634
1635 return( 0 );
1636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001640/*
1641 * SSLv3.0 MAC functions
1642 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001643#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001644static void ssl_mac( mbedtls_md_context_t *md_ctx,
1645 const unsigned char *secret,
1646 const unsigned char *buf, size_t len,
1647 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001648 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001649{
1650 unsigned char header[11];
1651 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001652 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1654 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001655
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001656 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001658 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001659 else
Paul Bakker68884e32013-01-07 18:20:04 +01001660 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
1662 memcpy( header, ctr, 8 );
1663 header[ 8] = (unsigned char) type;
1664 header[ 9] = (unsigned char)( len >> 8 );
1665 header[10] = (unsigned char)( len );
1666
Paul Bakker68884e32013-01-07 18:20:04 +01001667 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_md_starts( md_ctx );
1669 mbedtls_md_update( md_ctx, secret, md_size );
1670 mbedtls_md_update( md_ctx, padding, padlen );
1671 mbedtls_md_update( md_ctx, header, 11 );
1672 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001673 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Paul Bakker68884e32013-01-07 18:20:04 +01001675 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 mbedtls_md_starts( md_ctx );
1677 mbedtls_md_update( md_ctx, secret, md_size );
1678 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001679 mbedtls_md_update( md_ctx, out, md_size );
1680 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001683
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001684/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001685 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001686#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001687 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1688 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1689 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1690/* This function makes sure every byte in the memory region is accessed
1691 * (in ascending addresses order) */
1692static void ssl_read_memory( unsigned char *p, size_t len )
1693{
1694 unsigned char acc = 0;
1695 volatile unsigned char force;
1696
1697 for( ; len != 0; p++, len-- )
1698 acc ^= *p;
1699
1700 force = acc;
1701 (void) force;
1702}
1703#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1704
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001705/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001707 */
Hanno Becker3307b532017-12-27 21:37:21 +00001708
Hanno Beckera5a2b082019-05-15 14:03:01 +01001709#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001710/* This functions transforms a DTLS plaintext fragment and a record content
1711 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001712 *
1713 * struct {
1714 * opaque content[DTLSPlaintext.length];
1715 * ContentType real_type;
1716 * uint8 zeros[length_of_padding];
1717 * } DTLSInnerPlaintext;
1718 *
1719 * Input:
1720 * - `content`: The beginning of the buffer holding the
1721 * plaintext to be wrapped.
1722 * - `*content_size`: The length of the plaintext in Bytes.
1723 * - `max_len`: The number of Bytes available starting from
1724 * `content`. This must be `>= *content_size`.
1725 * - `rec_type`: The desired record content type.
1726 *
1727 * Output:
1728 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1729 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1730 *
1731 * Returns:
1732 * - `0` on success.
1733 * - A negative error code if `max_len` didn't offer enough space
1734 * for the expansion.
1735 */
1736static int ssl_cid_build_inner_plaintext( unsigned char *content,
1737 size_t *content_size,
1738 size_t remaining,
1739 uint8_t rec_type )
1740{
1741 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001742 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1743 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1744 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001745
1746 /* Write real content type */
1747 if( remaining == 0 )
1748 return( -1 );
1749 content[ len ] = rec_type;
1750 len++;
1751 remaining--;
1752
1753 if( remaining < pad )
1754 return( -1 );
1755 memset( content + len, 0, pad );
1756 len += pad;
1757 remaining -= pad;
1758
1759 *content_size = len;
1760 return( 0 );
1761}
1762
Hanno Becker7dc25772019-05-20 15:08:01 +01001763/* This function parses a DTLSInnerPlaintext structure.
1764 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001765static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1766 size_t *content_size,
1767 uint8_t *rec_type )
1768{
1769 size_t remaining = *content_size;
1770
1771 /* Determine length of padding by skipping zeroes from the back. */
1772 do
1773 {
1774 if( remaining == 0 )
1775 return( -1 );
1776 remaining--;
1777 } while( content[ remaining ] == 0 );
1778
1779 *content_size = remaining;
1780 *rec_type = content[ remaining ];
1781
1782 return( 0 );
1783}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001784#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001785
Hanno Becker99abf512019-05-20 14:50:53 +01001786/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001787 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001788static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001789 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001790 mbedtls_record *rec )
1791{
Hanno Becker99abf512019-05-20 14:50:53 +01001792 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001793 *
1794 * additional_data = seq_num + TLSCompressed.type +
1795 * TLSCompressed.version + TLSCompressed.length;
1796 *
Hanno Becker99abf512019-05-20 14:50:53 +01001797 * For the CID extension, this is extended as follows
1798 * (quoting draft-ietf-tls-dtls-connection-id-05,
1799 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001800 *
1801 * additional_data = seq_num + DTLSPlaintext.type +
1802 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001803 * cid +
1804 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001805 * length_of_DTLSInnerPlaintext;
1806 */
1807
Hanno Becker3307b532017-12-27 21:37:21 +00001808 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1809 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001810 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001811
Hanno Beckera5a2b082019-05-15 14:03:01 +01001812#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001813 if( rec->cid_len != 0 )
1814 {
1815 memcpy( add_data + 11, rec->cid, rec->cid_len );
1816 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1817 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1818 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1819 *add_data_len = 13 + 1 + rec->cid_len;
1820 }
1821 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001822#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001823 {
1824 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1825 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1826 *add_data_len = 13;
1827 }
Hanno Becker3307b532017-12-27 21:37:21 +00001828}
1829
Hanno Becker611a83b2018-01-03 14:27:32 +00001830int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1831 mbedtls_ssl_transform *transform,
1832 mbedtls_record *rec,
1833 int (*f_rng)(void *, unsigned char *, size_t),
1834 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001835{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001837 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001838 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001839 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001840 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001841 size_t post_avail;
1842
1843 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001844#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001845 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001846 ((void) ssl);
1847#endif
1848
1849 /* The PRNG is used for dynamic IV generation that's used
1850 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1851#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1852 ( defined(MBEDTLS_AES_C) || \
1853 defined(MBEDTLS_ARIA_C) || \
1854 defined(MBEDTLS_CAMELLIA_C) ) && \
1855 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1856 ((void) f_rng);
1857 ((void) p_rng);
1858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Hanno Becker3307b532017-12-27 21:37:21 +00001862 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001863 {
Hanno Becker3307b532017-12-27 21:37:21 +00001864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1865 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1866 }
Hanno Becker505089d2019-05-01 09:45:57 +01001867 if( rec == NULL
1868 || rec->buf == NULL
1869 || rec->buf_len < rec->data_offset
1870 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001871#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001872 || rec->cid_len != 0
1873#endif
1874 )
Hanno Becker3307b532017-12-27 21:37:21 +00001875 {
1876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001878 }
1879
Hanno Becker3307b532017-12-27 21:37:21 +00001880 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001881 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001883 data, rec->data_len );
1884
1885 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1886
1887 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1888 {
1889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1890 (unsigned) rec->data_len,
1891 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1893 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001894
Hanno Beckera5a2b082019-05-15 14:03:01 +01001895#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001896 /*
1897 * Add CID information
1898 */
1899 rec->cid_len = transform->out_cid_len;
1900 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1901 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001902
1903 if( rec->cid_len != 0 )
1904 {
1905 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001906 * Wrap plaintext into DTLSInnerPlaintext structure.
1907 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001908 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001909 * Note that this changes `rec->data_len`, and hence
1910 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001911 */
1912 if( ssl_cid_build_inner_plaintext( data,
1913 &rec->data_len,
1914 post_avail,
1915 rec->type ) != 0 )
1916 {
1917 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1918 }
1919
1920 rec->type = MBEDTLS_SSL_MSG_CID;
1921 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001922#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001923
Hanno Becker92c930f2019-04-29 17:31:37 +01001924 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001927 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001929#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 if( mode == MBEDTLS_MODE_STREAM ||
1931 ( mode == MBEDTLS_MODE_CBC
1932#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001933 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001934#endif
1935 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 {
Hanno Becker3307b532017-12-27 21:37:21 +00001937 if( post_avail < transform->maclen )
1938 {
1939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1940 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1941 }
1942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001944 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001945 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001946 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001947 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1948 data, rec->data_len, rec->ctr, rec->type, mac );
1949 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001950 }
1951 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1954 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001955 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001956 {
Hanno Becker992b6872017-11-09 18:57:39 +00001957 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1958
Hanno Beckere83efe62019-04-29 13:52:53 +01001959 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001960
Hanno Becker3307b532017-12-27 21:37:21 +00001961 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001962 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001963 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1964 data, rec->data_len );
1965 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1966 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1967
1968 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001969 }
1970 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001971#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001975 }
1976
Hanno Becker3307b532017-12-27 21:37:21 +00001977 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1978 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001979
Hanno Becker3307b532017-12-27 21:37:21 +00001980 rec->data_len += transform->maclen;
1981 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001982 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001983 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001984#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001985
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 /*
1987 * Encrypt
1988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1990 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001992 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001993 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001995 "including %d bytes of padding",
1996 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Hanno Becker3307b532017-12-27 21:37:21 +00001998 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1999 transform->iv_enc, transform->ivlen,
2000 data, rec->data_len,
2001 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002004 return( ret );
2005 }
2006
Hanno Becker3307b532017-12-27 21:37:21 +00002007 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
Paul Bakker68884e32013-01-07 18:20:04 +01002013 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002015
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002016#if defined(MBEDTLS_GCM_C) || \
2017 defined(MBEDTLS_CCM_C) || \
2018 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002020 mode == MBEDTLS_MODE_CCM ||
2021 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002022 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002023 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002024 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002025 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002026
Hanno Becker3307b532017-12-27 21:37:21 +00002027 /* Check that there's space for both the authentication tag
2028 * and the explicit IV before and after the record content. */
2029 if( post_avail < transform->taglen ||
2030 rec->data_offset < explicit_iv_len )
2031 {
2032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2033 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2034 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002035
Paul Bakker68884e32013-01-07 18:20:04 +01002036 /*
2037 * Generate IV
2038 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2040 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002041 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002042 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002043 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2044 explicit_iv_len );
2045 /* Prefix record content with explicit IV. */
2046 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 }
2048 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2049 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002050 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002051 unsigned char i;
2052
2053 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2054
2055 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002056 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002057 }
2058 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002059 {
2060 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2062 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002063 }
2064
Hanno Beckere83efe62019-04-29 13:52:53 +01002065 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002066
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002067 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2068 iv, transform->ivlen );
2069 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002070 data - explicit_iv_len, explicit_iv_len );
2071 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002072 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002075 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002076
Paul Bakker68884e32013-01-07 18:20:04 +01002077 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002078 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002079 */
Hanno Becker3307b532017-12-27 21:37:21 +00002080
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002081 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002082 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002083 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002084 data, rec->data_len, /* source */
2085 data, &rec->data_len, /* destination */
2086 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002089 return( ret );
2090 }
2091
Hanno Becker3307b532017-12-27 21:37:21 +00002092 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2093 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002094
Hanno Becker3307b532017-12-27 21:37:21 +00002095 rec->data_len += transform->taglen + explicit_iv_len;
2096 rec->data_offset -= explicit_iv_len;
2097 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002098 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2102#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002103 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002106 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002107 size_t padlen, i;
2108 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002109
Hanno Becker3307b532017-12-27 21:37:21 +00002110 /* Currently we're always using minimal padding
2111 * (up to 255 bytes would be allowed). */
2112 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2113 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 padlen = 0;
2115
Hanno Becker3307b532017-12-27 21:37:21 +00002116 /* Check there's enough space in the buffer for the padding. */
2117 if( post_avail < padlen + 1 )
2118 {
2119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2120 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2121 }
2122
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002124 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Hanno Becker3307b532017-12-27 21:37:21 +00002126 rec->data_len += padlen + 1;
2127 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002130 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002131 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2132 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002133 */
Hanno Becker3307b532017-12-27 21:37:21 +00002134 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002135 {
Hanno Becker3307b532017-12-27 21:37:21 +00002136 if( f_rng == NULL )
2137 {
2138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2140 }
2141
2142 if( rec->data_offset < transform->ivlen )
2143 {
2144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2145 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2146 }
2147
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002148 /*
2149 * Generate IV
2150 */
Hanno Becker3307b532017-12-27 21:37:21 +00002151 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002152 if( ret != 0 )
2153 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154
Hanno Becker3307b532017-12-27 21:37:21 +00002155 memcpy( data - transform->ivlen, transform->iv_enc,
2156 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002157
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002162 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002163 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002164 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165
Hanno Becker3307b532017-12-27 21:37:21 +00002166 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2167 transform->iv_enc,
2168 transform->ivlen,
2169 data, rec->data_len,
2170 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002173 return( ret );
2174 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002175
Hanno Becker3307b532017-12-27 21:37:21 +00002176 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002180 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002183 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002184 {
2185 /*
2186 * Save IV in SSL3 and TLS1
2187 */
Hanno Becker3307b532017-12-27 21:37:21 +00002188 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2189 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
Hanno Becker3307b532017-12-27 21:37:21 +00002191 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002192#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002193 {
2194 data -= transform->ivlen;
2195 rec->data_offset -= transform->ivlen;
2196 rec->data_len += transform->ivlen;
2197 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002200 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002201 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002202 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2203
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002204 /*
2205 * MAC(MAC_write_key, seq_num +
2206 * TLSCipherText.type +
2207 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002208 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002209 * IV + // except for TLS 1.0
2210 * ENC(content + padding + padding_length));
2211 */
Hanno Becker3307b532017-12-27 21:37:21 +00002212
2213 if( post_avail < transform->maclen)
2214 {
2215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2216 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2217 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218
Hanno Beckere83efe62019-04-29 13:52:53 +01002219 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002222 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002223 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002224
Hanno Becker3307b532017-12-27 21:37:21 +00002225 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002226 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002227 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2228 data, rec->data_len );
2229 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2230 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002231
Hanno Becker3307b532017-12-27 21:37:21 +00002232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002233
Hanno Becker3307b532017-12-27 21:37:21 +00002234 rec->data_len += transform->maclen;
2235 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002236 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002237 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002240 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002242 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002246 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002247
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002248 /* Make extra sure authentication was performed, exactly once */
2249 if( auth_done != 1 )
2250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 }
2254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
2257 return( 0 );
2258}
2259
Hanno Becker611a83b2018-01-03 14:27:32 +00002260int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2261 mbedtls_ssl_transform *transform,
2262 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002264 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002266 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002267#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002268 size_t padlen = 0, correct = 1;
2269#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002270 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002271 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002272 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002273
Hanno Becker611a83b2018-01-03 14:27:32 +00002274#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002275 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002276 ((void) ssl);
2277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002280 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2284 }
2285 if( rec == NULL ||
2286 rec->buf == NULL ||
2287 rec->buf_len < rec->data_offset ||
2288 rec->buf_len - rec->data_offset < rec->data_len )
2289 {
2290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002292 }
2293
Hanno Becker4c6876b2017-12-27 21:28:58 +00002294 data = rec->buf + rec->data_offset;
2295 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296
Hanno Beckera5a2b082019-05-15 14:03:01 +01002297#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002298 /*
2299 * Match record's CID with incoming CID.
2300 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002301 if( rec->cid_len != transform->in_cid_len ||
2302 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2303 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002304 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002305 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002306#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2309 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002310 {
2311 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002312 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2313 transform->iv_dec,
2314 transform->ivlen,
2315 data, rec->data_len,
2316 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002319 return( ret );
2320 }
2321
Hanno Becker4c6876b2017-12-27 21:28:58 +00002322 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 }
Paul Bakker68884e32013-01-07 18:20:04 +01002328 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330#if defined(MBEDTLS_GCM_C) || \
2331 defined(MBEDTLS_CCM_C) || \
2332 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 mode == MBEDTLS_MODE_CCM ||
2335 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002336 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002337 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002339
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 /*
2341 * Compute and update sizes
2342 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002343 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002346 "+ taglen (%d)", rec->data_len,
2347 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002349 }
Paul Bakker68884e32013-01-07 18:20:04 +01002350
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 /*
2352 * Prepare IV
2353 */
2354 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2355 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002356 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002358 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 }
2361 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2362 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002363 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002364 unsigned char i;
2365
2366 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2367
2368 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002369 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 }
2371 else
2372 {
2373 /* Reminder if we ever add an AEAD mode with a different size */
2374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2375 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2376 }
2377
Hanno Becker4c6876b2017-12-27 21:28:58 +00002378 data += explicit_iv_len;
2379 rec->data_offset += explicit_iv_len;
2380 rec->data_len -= explicit_iv_len + transform->taglen;
2381
Hanno Beckere83efe62019-04-29 13:52:53 +01002382 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002383 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002384 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002385
2386 memcpy( transform->iv_dec + transform->fixed_ivlen,
2387 data - explicit_iv_len, explicit_iv_len );
2388
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002389 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002390 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002391 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002392
Paul Bakker68884e32013-01-07 18:20:04 +01002393
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002394 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002395 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002396 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002397 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2398 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002399 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 data, rec->data_len,
2401 data, &olen,
2402 data + rec->data_len,
2403 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002404 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2408 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002409
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002410 return( ret );
2411 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002412 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002413
Hanno Becker4c6876b2017-12-27 21:28:58 +00002414 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002418 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002421#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2422#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002423 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002426 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002427
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 /*
Paul Bakker45829992013-01-03 14:52:21 +01002429 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002432 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2433 {
2434 /* The ciphertext is prefixed with the CBC IV. */
2435 minlen += transform->ivlen;
2436 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002437#endif
Paul Bakker45829992013-01-03 14:52:21 +01002438
Hanno Becker4c6876b2017-12-27 21:28:58 +00002439 /* Size considerations:
2440 *
2441 * - The CBC cipher text must not be empty and hence
2442 * at least of size transform->ivlen.
2443 *
2444 * Together with the potential IV-prefix, this explains
2445 * the first of the two checks below.
2446 *
2447 * - The record must contain a MAC, either in plain or
2448 * encrypted, depending on whether Encrypt-then-MAC
2449 * is used or not.
2450 * - If it is, the message contains the IV-prefix,
2451 * the CBC ciphertext, and the MAC.
2452 * - If it is not, the padded plaintext, and hence
2453 * the CBC ciphertext, has at least length maclen + 1
2454 * because there is at least the padding length byte.
2455 *
2456 * As the CBC ciphertext is not empty, both cases give the
2457 * lower bound minlen + maclen + 1 on the record size, which
2458 * we test for in the second check below.
2459 */
2460 if( rec->data_len < minlen + transform->ivlen ||
2461 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002464 "+ 1 ) ( + expl IV )", rec->data_len,
2465 transform->ivlen,
2466 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002468 }
2469
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002470 /*
2471 * Authenticate before decrypt if enabled
2472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002474 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002475 {
Hanno Becker992b6872017-11-09 18:57:39 +00002476 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002479
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2481 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002482
Hanno Beckere83efe62019-04-29 13:52:53 +01002483 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002484
Hanno Beckere83efe62019-04-29 13:52:53 +01002485 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2486 add_data_len );
2487 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2488 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002489 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2490 data, rec->data_len );
2491 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2492 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002493
Hanno Becker4c6876b2017-12-27 21:28:58 +00002494 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2495 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002496 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Becker4c6876b2017-12-27 21:28:58 +00002499 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2500 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002504 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002505 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508
2509 /*
2510 * Check length sanity
2511 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002512 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002516 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 }
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002520 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002521 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002522 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002523 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002524 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002525 /* This is safe because data_len >= minlen + maclen + 1 initially,
2526 * and at this point we have at most subtracted maclen (note that
2527 * minlen == transform->ivlen here). */
2528 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002529
Hanno Becker4c6876b2017-12-27 21:28:58 +00002530 data += transform->ivlen;
2531 rec->data_offset += transform->ivlen;
2532 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002535
Hanno Becker4c6876b2017-12-27 21:28:58 +00002536 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2537 transform->iv_dec, transform->ivlen,
2538 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002541 return( ret );
2542 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002543
Hanno Becker4c6876b2017-12-27 21:28:58 +00002544 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2547 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002548 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002550#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002551 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002552 {
2553 /*
2554 * Save IV in SSL3 and TLS1
2555 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002556 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2557 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002559#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 /* Safe since data_len >= minlen + maclen + 1, so after having
2562 * subtracted at most minlen and maclen up to this point,
2563 * data_len > 0. */
2564 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002565
Hanno Becker4c6876b2017-12-27 21:28:58 +00002566 if( auth_done == 1 )
2567 {
2568 correct *= ( rec->data_len >= padlen + 1 );
2569 padlen *= ( rec->data_len >= padlen + 1 );
2570 }
2571 else
Paul Bakker45829992013-01-03 14:52:21 +01002572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002574 if( rec->data_len < transform->maclen + padlen + 1 )
2575 {
2576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2577 rec->data_len,
2578 transform->maclen,
2579 padlen + 1 ) );
2580 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002581#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002582
2583 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2584 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Becker4c6876b2017-12-27 21:28:58 +00002587 padlen++;
2588
2589 /* Regardless of the validity of the padding,
2590 * we have data_len >= padlen here. */
2591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002592#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002593 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002595 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597#if defined(MBEDTLS_SSL_DEBUG_ALL)
2598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599 "should be no more than %d",
2600 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002601#endif
Paul Bakker45829992013-01-03 14:52:21 +01002602 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
2604 }
2605 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2607#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2608 defined(MBEDTLS_SSL_PROTO_TLS1_2)
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 /* The padding check involves a series of up to 256
2612 * consecutive memory reads at the end of the record
2613 * plaintext buffer. In order to hide the length and
2614 * validity of the padding, always perform exactly
2615 * `min(256,plaintext_len)` reads (but take into account
2616 * only the last `padlen` bytes for the padding check). */
2617 size_t pad_count = 0;
2618 size_t real_count = 0;
2619 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002620
Hanno Becker4c6876b2017-12-27 21:28:58 +00002621 /* Index of first padding byte; it has been ensured above
2622 * that the subtraction is safe. */
2623 size_t const padding_idx = rec->data_len - padlen;
2624 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2625 size_t const start_idx = rec->data_len - num_checks;
2626 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002627
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002629 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002630 real_count |= ( idx >= padding_idx );
2631 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002632 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002633 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002636 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002638#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002639 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002641 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2643 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002647 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002648
Hanno Becker4c6876b2017-12-27 21:28:58 +00002649 /* If the padding was found to be invalid, padlen == 0
2650 * and the subtraction is safe. If the padding was found valid,
2651 * padlen hasn't been changed and the previous assertion
2652 * data_len >= padlen still holds. */
2653 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002655 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002657 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2660 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002662
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002663#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002665 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002669 * Authenticate if not done yet.
2670 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002672#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002673 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002674 {
Hanno Becker992b6872017-11-09 18:57:39 +00002675 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002676
Hanno Becker4c6876b2017-12-27 21:28:58 +00002677 /* If the initial value of padlen was such that
2678 * data_len < maclen + padlen + 1, then padlen
2679 * got reset to 1, and the initial check
2680 * data_len >= minlen + maclen + 1
2681 * guarantees that at this point we still
2682 * have at least data_len >= maclen.
2683 *
2684 * If the initial value of padlen was such that
2685 * data_len >= maclen + padlen + 1, then we have
2686 * subtracted either padlen + 1 (if the padding was correct)
2687 * or 0 (if the padding was incorrect) since then,
2688 * hence data_len >= maclen in any case.
2689 */
2690 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Hanno Beckere83efe62019-04-29 13:52:53 +01002692 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002695 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002696 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002697 ssl_mac( &transform->md_ctx_dec,
2698 transform->mac_dec,
2699 data, rec->data_len,
2700 rec->ctr, rec->type,
2701 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002702 }
2703 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2706 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002707 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002708 {
2709 /*
2710 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002711 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002712 *
2713 * Known timing attacks:
2714 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2715 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002716 * To compensate for different timings for the MAC calculation
2717 * depending on how much padding was removed (which is determined
2718 * by padlen), process extra_run more blocks through the hash
2719 * function.
2720 *
2721 * The formula in the paper is
2722 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2723 * where L1 is the size of the header plus the decrypted message
2724 * plus CBC padding and L2 is the size of the header plus the
2725 * decrypted message. This is for an underlying hash function
2726 * with 64-byte blocks.
2727 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2728 * correctly. We round down instead of up, so -56 is the correct
2729 * value for our calculations instead of -55.
2730 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002731 * Repeat the formula rather than defining a block_size variable.
2732 * This avoids requiring division by a variable at runtime
2733 * (which would be marginally less efficient and would require
2734 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002735 */
2736 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002737 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002738
2739 /*
2740 * The next two sizes are the minimum and maximum values of
2741 * in_msglen over all padlen values.
2742 *
2743 * They're independent of padlen, since we previously did
2744 * in_msglen -= padlen.
2745 *
2746 * Note that max_len + maclen is never more than the buffer
2747 * length, as we previously did in_msglen -= maclen too.
2748 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002749 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002750 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2751
Hanno Becker4c6876b2017-12-27 21:28:58 +00002752 memset( tmp, 0, sizeof( tmp ) );
2753
2754 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002755 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002756#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2757 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002758 case MBEDTLS_MD_MD5:
2759 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002760 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002762 extra_run =
2763 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2764 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002765 break;
2766#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002767#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002768 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002770 extra_run =
2771 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2772 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002773 break;
2774#endif
2775 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2778 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002779
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002780 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002781
Hanno Beckere83efe62019-04-29 13:52:53 +01002782 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2783 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002784 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2785 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002786 /* Make sure we access everything even when padlen > 0. This
2787 * makes the synchronisation requirements for just-in-time
2788 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002789 ssl_read_memory( data + rec->data_len, padlen );
2790 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002791
2792 /* Call mbedtls_md_process at least once due to cache attacks
2793 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002794 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002795 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002796
Hanno Becker4c6876b2017-12-27 21:28:58 +00002797 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002798
2799 /* Make sure we access all the memory that could contain the MAC,
2800 * before we check it in the next code block. This makes the
2801 * synchronisation requirements for just-in-time Prime+Probe
2802 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002803 ssl_read_memory( data + min_len,
2804 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002805 }
2806 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2808 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2811 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002812 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002813
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002815 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2816 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002818
Hanno Becker4c6876b2017-12-27 21:28:58 +00002819 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2820 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#if defined(MBEDTLS_SSL_DEBUG_ALL)
2823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002824#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 correct = 0;
2826 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002827 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002829
2830 /*
2831 * Finally check the correct flag
2832 */
2833 if( correct == 0 )
2834 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002835#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002836
2837 /* Make extra sure authentication was performed, exactly once */
2838 if( auth_done != 1 )
2839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2841 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002842 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Hanno Beckera5a2b082019-05-15 14:03:01 +01002844#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002845 if( rec->cid_len != 0 )
2846 {
2847 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2848 &rec->type );
2849 if( ret != 0 )
2850 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2851 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002852#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
2856 return( 0 );
2857}
2858
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002859#undef MAC_NONE
2860#undef MAC_PLAINTEXT
2861#undef MAC_CIPHERTEXT
2862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002864/*
2865 * Compression/decompression functions
2866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002868{
2869 int ret;
2870 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002871 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002872 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002873 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002876
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002877 if( len_pre == 0 )
2878 return( 0 );
2879
Paul Bakker2770fbd2012-07-03 13:30:23 +00002880 memcpy( msg_pre, ssl->out_msg, len_pre );
2881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002883 ssl->out_msglen ) );
2884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002886 ssl->out_msg, ssl->out_msglen );
2887
Paul Bakker48916f92012-09-16 19:57:18 +00002888 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2889 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2890 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002891 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002892
Paul Bakker48916f92012-09-16 19:57:18 +00002893 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002894 if( ret != Z_OK )
2895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2897 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002898 }
2899
Angus Grattond8213d02016-05-25 20:56:48 +10002900 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002901 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002904 ssl->out_msglen ) );
2905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002907 ssl->out_msg, ssl->out_msglen );
2908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002910
2911 return( 0 );
2912}
2913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915{
2916 int ret;
2917 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002918 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002920 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002923
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002924 if( len_pre == 0 )
2925 return( 0 );
2926
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927 memcpy( msg_pre, ssl->in_msg, len_pre );
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002930 ssl->in_msglen ) );
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933 ssl->in_msg, ssl->in_msglen );
2934
Paul Bakker48916f92012-09-16 19:57:18 +00002935 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2936 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2937 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002938 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002939 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940
Paul Bakker48916f92012-09-16 19:57:18 +00002941 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 if( ret != Z_OK )
2943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2945 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946 }
2947
Angus Grattond8213d02016-05-25 20:56:48 +10002948 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002949 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 ssl->in_msglen ) );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955 ssl->in_msg, ssl->in_msglen );
2956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002958
2959 return( 0 );
2960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2964static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966#if defined(MBEDTLS_SSL_PROTO_DTLS)
2967static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002968{
2969 /* If renegotiation is not enforced, retransmit until we would reach max
2970 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002971 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002972 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002973 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002974 unsigned char doublings = 1;
2975
2976 while( ratio != 0 )
2977 {
2978 ++doublings;
2979 ratio >>= 1;
2980 }
2981
2982 if( ++ssl->renego_records_seen > doublings )
2983 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002985 return( 0 );
2986 }
2987 }
2988
2989 return( ssl_write_hello_request( ssl ) );
2990}
2991#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002993
Paul Bakker5121ce52009-01-03 21:22:43 +00002994/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002995 * Fill the input message buffer by appending data to it.
2996 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002997 *
2998 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2999 * available (from this read and/or a previous one). Otherwise, an error code
3000 * is returned (possibly EOF or WANT_READ).
3001 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003002 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3003 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3004 * since we always read a whole datagram at once.
3005 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003006 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003007 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010{
Paul Bakker23986e52011-04-24 08:57:21 +00003011 int ret;
3012 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003016 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003019 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003021 }
3022
Angus Grattond8213d02016-05-25 20:56:48 +10003023 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003027 }
3028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003030 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003032 uint32_t timeout;
3033
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003034 /* Just to be sure */
3035 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3036 {
3037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3038 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3039 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3040 }
3041
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003042 /*
3043 * The point is, we need to always read a full datagram at once, so we
3044 * sometimes read more then requested, and handle the additional data.
3045 * It could be the rest of the current record (while fetching the
3046 * header) and/or some other records in the same datagram.
3047 */
3048
3049 /*
3050 * Move to the next record in the already read datagram if applicable
3051 */
3052 if( ssl->next_record_offset != 0 )
3053 {
3054 if( ssl->in_left < ssl->next_record_offset )
3055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003058 }
3059
3060 ssl->in_left -= ssl->next_record_offset;
3061
3062 if( ssl->in_left != 0 )
3063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003065 ssl->next_record_offset ) );
3066 memmove( ssl->in_hdr,
3067 ssl->in_hdr + ssl->next_record_offset,
3068 ssl->in_left );
3069 }
3070
3071 ssl->next_record_offset = 0;
3072 }
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003076
3077 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003078 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003079 */
3080 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003083 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003084 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003085
3086 /*
3087 * A record can't be split accross datagrams. If we need to read but
3088 * are not at the beginning of a new record, the caller did something
3089 * wrong.
3090 */
3091 if( ssl->in_left != 0 )
3092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003095 }
3096
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003097 /*
3098 * Don't even try to read if time's out already.
3099 * This avoids by-passing the timer when repeatedly receiving messages
3100 * that will end up being dropped.
3101 */
3102 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003103 {
3104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003105 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003106 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003107 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003108 {
Angus Grattond8213d02016-05-25 20:56:48 +10003109 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003112 timeout = ssl->handshake->retransmit_timeout;
3113 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003114 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003117
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003118 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003119 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3120 timeout );
3121 else
3122 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003125
3126 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003128 }
3129
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003130 if( ret == MBEDTLS_ERR_SSL_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( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003136 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003137 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003140 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003141 }
3142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003146 return( ret );
3147 }
3148
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003149 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003150 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003152 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003153 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003154 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003155 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003158 return( ret );
3159 }
3160
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003161 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003162 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003164 }
3165
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 if( ret < 0 )
3167 return( ret );
3168
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003169 ssl->in_left = ret;
3170 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003171 MBEDTLS_SSL_TRANSPORT_ELSE
3172#endif /* MBEDTLS_SSL_PROTO_DTLS */
3173#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003176 ssl->in_left, nb_want ) );
3177
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003178 while( ssl->in_left < nb_want )
3179 {
3180 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003181
3182 if( ssl_check_timer( ssl ) != 0 )
3183 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3184 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003185 {
3186 if( ssl->f_recv_timeout != NULL )
3187 {
3188 ret = ssl->f_recv_timeout( ssl->p_bio,
3189 ssl->in_hdr + ssl->in_left, len,
3190 ssl->conf->read_timeout );
3191 }
3192 else
3193 {
3194 ret = ssl->f_recv( ssl->p_bio,
3195 ssl->in_hdr + ssl->in_left, len );
3196 }
3197 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003200 ssl->in_left, nb_want ) );
3201 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003202
3203 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003205
3206 if( ret < 0 )
3207 return( ret );
3208
mohammad160352aecb92018-03-28 23:41:40 -07003209 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003210 {
Darryl Green11999bb2018-03-13 15:22:58 +00003211 MBEDTLS_SSL_DEBUG_MSG( 1,
3212 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003213 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3215 }
3216
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003217 ssl->in_left += ret;
3218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003220#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003223
3224 return( 0 );
3225}
3226
3227/*
3228 * Flush any data not yet written
3229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003231{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003232 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003233 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003236
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003237 if( ssl->f_send == NULL )
3238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003240 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003242 }
3243
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003244 /* Avoid incrementing counter if data is flushed */
3245 if( ssl->out_left == 0 )
3246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003248 return( 0 );
3249 }
3250
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 while( ssl->out_left > 0 )
3252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003254 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003255
Hanno Becker2b1e3542018-08-06 11:19:13 +01003256 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003257 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260
3261 if( ret <= 0 )
3262 return( ret );
3263
mohammad160352aecb92018-03-28 23:41:40 -07003264 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003265 {
Darryl Green11999bb2018-03-13 15:22:58 +00003266 MBEDTLS_SSL_DEBUG_MSG( 1,
3267 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003268 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3270 }
3271
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 ssl->out_left -= ret;
3273 }
3274
Hanno Becker2b1e3542018-08-06 11:19:13 +01003275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003276 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003277 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003278 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003279 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003280 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003281#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003282#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003283 {
3284 ssl->out_hdr = ssl->out_buf + 8;
3285 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003286#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003287 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
3291 return( 0 );
3292}
3293
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003294/*
3295 * Functions to handle the DTLS retransmission state machine
3296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003298/*
3299 * Append current handshake message to current outgoing flight
3300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3305 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3306 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003307
3308 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003309 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003310 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003311 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003313 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003314 }
3315
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003316 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003317 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003320 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321 }
3322
3323 /* Copy current handshake message with headers */
3324 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3325 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003326 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 msg->next = NULL;
3328
3329 /* Append to the current flight */
3330 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003331 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003332 else
3333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003334 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003335 while( cur->next != NULL )
3336 cur = cur->next;
3337 cur->next = msg;
3338 }
3339
Hanno Becker3b235902018-08-06 09:54:53 +01003340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003341 return( 0 );
3342}
3343
3344/*
3345 * Free the current flight of handshake messages
3346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_ssl_flight_item *cur = flight;
3350 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351
3352 while( cur != NULL )
3353 {
3354 next = cur->next;
3355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 mbedtls_free( cur->p );
3357 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358
3359 cur = next;
3360 }
3361}
3362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3364static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003365#endif
3366
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003367/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003368 * Swap transform_out and out_ctr with the alternative ones
3369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373 unsigned char tmp_out_ctr[8];
3374
3375 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003378 return;
3379 }
3380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003382
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003383 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003384 tmp_transform = ssl->transform_out;
3385 ssl->transform_out = ssl->handshake->alt_transform_out;
3386 ssl->handshake->alt_transform_out = tmp_transform;
3387
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003388 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003389 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3390 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003391 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003392
3393 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003394 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3397 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3402 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003403 }
3404 }
3405#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003406}
3407
3408/*
3409 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003410 */
3411int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3412{
3413 int ret = 0;
3414
3415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3416
3417 ret = mbedtls_ssl_flight_transmit( ssl );
3418
3419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3420
3421 return( ret );
3422}
3423
3424/*
3425 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003426 *
3427 * Need to remember the current message in case flush_output returns
3428 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003430 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003431int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003432{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003433 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003437 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003439
3440 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003441 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003442 ssl_swap_epochs( ssl );
3443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003445 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003446
3447 while( ssl->handshake->cur_msg != NULL )
3448 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003449 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003450 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003451
Hanno Beckere1dcb032018-08-17 16:47:58 +01003452 int const is_finished =
3453 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3454 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3455
Hanno Becker04da1892018-08-14 13:22:10 +01003456 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3457 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3458
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003459 /* Swap epochs before sending Finished: we can't do it after
3460 * sending ChangeCipherSpec, in case write returns WANT_READ.
3461 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003462 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003463 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003465 ssl_swap_epochs( ssl );
3466 }
3467
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468 ret = ssl_get_remaining_payload_in_datagram( ssl );
3469 if( ret < 0 )
3470 return( ret );
3471 max_frag_len = (size_t) ret;
3472
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003473 /* CCS is copied as is, while HS messages may need fragmentation */
3474 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3475 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003476 if( max_frag_len == 0 )
3477 {
3478 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3479 return( ret );
3480
3481 continue;
3482 }
3483
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003484 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003485 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003486 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003488 /* Update position inside current message */
3489 ssl->handshake->cur_msg_p += cur->len;
3490 }
3491 else
3492 {
3493 const unsigned char * const p = ssl->handshake->cur_msg_p;
3494 const size_t hs_len = cur->len - 12;
3495 const size_t frag_off = p - ( cur->p + 12 );
3496 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003497 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003498
Hanno Beckere1dcb032018-08-17 16:47:58 +01003499 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003500 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003501 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003503
Hanno Becker67bc7c32018-08-06 11:33:50 +01003504 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3505 return( ret );
3506
3507 continue;
3508 }
3509 max_hs_frag_len = max_frag_len - 12;
3510
3511 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3512 max_hs_frag_len : rem_len;
3513
3514 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003515 {
3516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003517 (unsigned) cur_hs_frag_len,
3518 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003519 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003520
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003521 /* Messages are stored with handshake headers as if not fragmented,
3522 * copy beginning of headers then fill fragmentation fields.
3523 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3524 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003525
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003526 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3527 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3528 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3529
Hanno Becker67bc7c32018-08-06 11:33:50 +01003530 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3531 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3532 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003533
3534 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3535
Hanno Becker3f7b9732018-08-28 09:53:25 +01003536 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003537 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3538 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003539 ssl->out_msgtype = cur->type;
3540
3541 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003542 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 }
3544
3545 /* If done with the current message move to the next one if any */
3546 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3547 {
3548 if( cur->next != NULL )
3549 {
3550 ssl->handshake->cur_msg = cur->next;
3551 ssl->handshake->cur_msg_p = cur->next->p + 12;
3552 }
3553 else
3554 {
3555 ssl->handshake->cur_msg = NULL;
3556 ssl->handshake->cur_msg_p = NULL;
3557 }
3558 }
3559
3560 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003561 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003564 return( ret );
3565 }
3566 }
3567
Hanno Becker67bc7c32018-08-06 11:33:50 +01003568 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3569 return( ret );
3570
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003571 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3573 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003574 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003577 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3578 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003579
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581
3582 return( 0 );
3583}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003584
3585/*
3586 * To be called when the last message of an incoming flight is received.
3587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003589{
3590 /* We won't need to resend that one any more */
3591 ssl_flight_free( ssl->handshake->flight );
3592 ssl->handshake->flight = NULL;
3593 ssl->handshake->cur_msg = NULL;
3594
3595 /* The next incoming flight will start with this msg_seq */
3596 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3597
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003598 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003599 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003600
Hanno Becker0271f962018-08-16 13:23:47 +01003601 /* Clear future message buffering structure. */
3602 ssl_buffering_free( ssl );
3603
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003604 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003605 ssl_set_timer( ssl, 0 );
3606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3608 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003611 }
3612 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003615
3616/*
3617 * To be called when the last message of an outgoing flight is send.
3618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003621 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003622 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3625 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003628 }
3629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633
Paul Bakker5121ce52009-01-03 21:22:43 +00003634/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003635 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637
3638/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003639 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003640 *
3641 * - fill in handshake headers
3642 * - update handshake checksum
3643 * - DTLS: save message for resending
3644 * - then pass to the record layer
3645 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003646 * DTLS: except for HelloRequest, messages are only queued, and will only be
3647 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003648 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003649 * Inputs:
3650 * - ssl->out_msglen: 4 + actual handshake message len
3651 * (4 is the size of handshake headers for TLS)
3652 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3653 * - ssl->out_msg + 4: the handshake message body
3654 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003655 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003656 * - ssl->out_msglen: the length of the record contents
3657 * (including handshake headers but excluding record headers)
3658 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003660int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003661{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003662 int ret;
3663 const size_t hs_len = ssl->out_msglen - 4;
3664 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3667
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003668 /*
3669 * Sanity checks
3670 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003671 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3673 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003674 /* In SSLv3, the client might send a NoCertificate alert. */
3675#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3676 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3677 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3678 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3679#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3680 {
3681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3683 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003686 /* Whenever we send anything different from a
3687 * HelloRequest we should be in a handshake - double check. */
3688 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3689 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003690 ssl->handshake == NULL )
3691 {
3692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003697 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003705
Hanno Beckerb50a2532018-08-06 11:52:54 +01003706 /* Double-check that we did not exceed the bounds
3707 * of the outgoing record buffer.
3708 * This should never fail as the various message
3709 * writing functions must obey the bounds of the
3710 * outgoing record buffer, but better be safe.
3711 *
3712 * Note: We deliberately do not check for the MTU or MFL here.
3713 */
3714 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3715 {
3716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3717 "size %u, maximum %u",
3718 (unsigned) ssl->out_msglen,
3719 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3721 }
3722
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003723 /*
3724 * Fill handshake headers
3725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003728 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3729 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3730 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003732 /*
3733 * DTLS has additional fields in the Handshake layer,
3734 * between the length field and the actual payload:
3735 * uint16 message_seq;
3736 * uint24 fragment_offset;
3737 * uint24 fragment_length;
3738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003740 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003741 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003742 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003743 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003744 {
3745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3746 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003747 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003748 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3750 }
3751
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003752 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003753 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003755 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003756 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003757 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003758 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3759 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3760 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003761 }
3762 else
3763 {
3764 ssl->out_msg[4] = 0;
3765 ssl->out_msg[5] = 0;
3766 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003768 /* Handshake hashes are computed without fragmentation,
3769 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003770 memset( ssl->out_msg + 6, 0x00, 3 );
3771 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003774
Hanno Becker0207e532018-08-28 10:28:28 +01003775 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003776 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3777 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 }
3779
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003780 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003782 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003783 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3784 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003785 {
3786 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003789 return( ret );
3790 }
3791 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003792 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003793#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003794 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003796 {
3797 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3798 return( ret );
3799 }
3800 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003801
3802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3803
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003804 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003805}
3806
3807/*
3808 * Record layer functions
3809 */
3810
3811/*
3812 * Write current record.
3813 *
3814 * Uses:
3815 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3816 * - ssl->out_msglen: length of the record content (excl headers)
3817 * - ssl->out_msg: record content
3818 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003820{
3821 int ret, done = 0;
3822 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003823 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003824
3825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003827#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003828 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003830 {
3831 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003834 return( ret );
3835 }
3836
3837 len = ssl->out_msglen;
3838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3842 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 ret = mbedtls_ssl_hw_record_write( ssl );
3847 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3850 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003851 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003852
3853 if( ret == 0 )
3854 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003857 if( !done )
3858 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003859 unsigned i;
3860 size_t protected_record_size;
3861
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003862 /* Skip writing the record content type to after the encryption,
3863 * as it may change when using the CID extension. */
3864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003866 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003867
Hanno Becker19859472018-08-06 09:40:20 +01003868 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003869 ssl->out_len[0] = (unsigned char)( len >> 8 );
3870 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003871
Paul Bakker48916f92012-09-16 19:57:18 +00003872 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003873 {
Hanno Becker3307b532017-12-27 21:37:21 +00003874 mbedtls_record rec;
3875
3876 rec.buf = ssl->out_iv;
3877 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3878 ( ssl->out_iv - ssl->out_buf );
3879 rec.data_len = ssl->out_msglen;
3880 rec.data_offset = ssl->out_msg - rec.buf;
3881
3882 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3883 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3884 ssl->conf->transport, rec.ver );
3885 rec.type = ssl->out_msgtype;
3886
Hanno Beckera5a2b082019-05-15 14:03:01 +01003887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003888 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003889 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003891
Hanno Becker611a83b2018-01-03 14:27:32 +00003892 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003893 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003896 return( ret );
3897 }
3898
Hanno Becker3307b532017-12-27 21:37:21 +00003899 if( rec.data_offset != 0 )
3900 {
3901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3903 }
3904
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003905 /* Update the record content type and CID. */
3906 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003907#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003908 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003909#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003910 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003911 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3912 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003913 }
3914
Hanno Becker43395762019-05-03 14:46:38 +01003915 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003916
3917#if defined(MBEDTLS_SSL_PROTO_DTLS)
3918 /* In case of DTLS, double-check that we don't exceed
3919 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003920 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003921 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003922 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003923 if( ret < 0 )
3924 return( ret );
3925
3926 if( protected_record_size > (size_t) ret )
3927 {
3928 /* Should never happen */
3929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3930 }
3931 }
3932#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003933
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003934 /* Now write the potentially updated record content type. */
3935 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003938 "version = [%d:%d], msglen = %d",
3939 ssl->out_hdr[0], ssl->out_hdr[1],
3940 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003943 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003944
3945 ssl->out_left += protected_record_size;
3946 ssl->out_hdr += protected_record_size;
3947 ssl_update_out_pointers( ssl, ssl->transform_out );
3948
Hanno Becker04484622018-08-06 09:49:38 +01003949 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3950 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3951 break;
3952
3953 /* The loop goes to its end iff the counter is wrapping */
3954 if( i == ssl_ep_len( ssl ) )
3955 {
3956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3957 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3958 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 }
3960
Hanno Becker67bc7c32018-08-06 11:33:50 +01003961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003962 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003963 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003964 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003965 size_t remaining;
3966 ret = ssl_get_remaining_payload_in_datagram( ssl );
3967 if( ret < 0 )
3968 {
3969 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3970 ret );
3971 return( ret );
3972 }
3973
3974 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003975 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003976 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003977 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003978 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003979 else
3980 {
Hanno Becker513815a2018-08-20 11:56:09 +01003981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003982 }
3983 }
3984#endif /* MBEDTLS_SSL_PROTO_DTLS */
3985
3986 if( ( flush == SSL_FORCE_FLUSH ) &&
3987 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 return( ret );
3991 }
3992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003994
3995 return( 0 );
3996}
3997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003999
4000static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4001{
4002 if( ssl->in_msglen < ssl->in_hslen ||
4003 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4004 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4005 {
4006 return( 1 );
4007 }
4008 return( 0 );
4009}
Hanno Becker44650b72018-08-16 12:51:11 +01004010
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004011static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004012{
4013 return( ( ssl->in_msg[9] << 16 ) |
4014 ( ssl->in_msg[10] << 8 ) |
4015 ssl->in_msg[11] );
4016}
4017
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004018static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004019{
4020 return( ( ssl->in_msg[6] << 16 ) |
4021 ( ssl->in_msg[7] << 8 ) |
4022 ssl->in_msg[8] );
4023}
4024
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004025static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004026{
4027 uint32_t msg_len, frag_off, frag_len;
4028
4029 msg_len = ssl_get_hs_total_len( ssl );
4030 frag_off = ssl_get_hs_frag_off( ssl );
4031 frag_len = ssl_get_hs_frag_len( ssl );
4032
4033 if( frag_off > msg_len )
4034 return( -1 );
4035
4036 if( frag_len > msg_len - frag_off )
4037 return( -1 );
4038
4039 if( frag_len + 12 > ssl->in_msglen )
4040 return( -1 );
4041
4042 return( 0 );
4043}
4044
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004045/*
4046 * Mark bits in bitmask (used for DTLS HS reassembly)
4047 */
4048static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4049{
4050 unsigned int start_bits, end_bits;
4051
4052 start_bits = 8 - ( offset % 8 );
4053 if( start_bits != 8 )
4054 {
4055 size_t first_byte_idx = offset / 8;
4056
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004057 /* Special case */
4058 if( len <= start_bits )
4059 {
4060 for( ; len != 0; len-- )
4061 mask[first_byte_idx] |= 1 << ( start_bits - len );
4062
4063 /* Avoid potential issues with offset or len becoming invalid */
4064 return;
4065 }
4066
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004067 offset += start_bits; /* Now offset % 8 == 0 */
4068 len -= start_bits;
4069
4070 for( ; start_bits != 0; start_bits-- )
4071 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4072 }
4073
4074 end_bits = len % 8;
4075 if( end_bits != 0 )
4076 {
4077 size_t last_byte_idx = ( offset + len ) / 8;
4078
4079 len -= end_bits; /* Now len % 8 == 0 */
4080
4081 for( ; end_bits != 0; end_bits-- )
4082 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4083 }
4084
4085 memset( mask + offset / 8, 0xFF, len / 8 );
4086}
4087
4088/*
4089 * Check that bitmask is full
4090 */
4091static int ssl_bitmask_check( unsigned char *mask, size_t len )
4092{
4093 size_t i;
4094
4095 for( i = 0; i < len / 8; i++ )
4096 if( mask[i] != 0xFF )
4097 return( -1 );
4098
4099 for( i = 0; i < len % 8; i++ )
4100 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4101 return( -1 );
4102
4103 return( 0 );
4104}
4105
Hanno Becker56e205e2018-08-16 09:06:12 +01004106/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004107static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004108 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004109{
Hanno Becker56e205e2018-08-16 09:06:12 +01004110 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004111
Hanno Becker56e205e2018-08-16 09:06:12 +01004112 alloc_len = 12; /* Handshake header */
4113 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004114
Hanno Beckerd07df862018-08-16 09:14:58 +01004115 if( add_bitmap )
4116 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004117
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004118 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004119}
Hanno Becker56e205e2018-08-16 09:06:12 +01004120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004122
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004123static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004124{
4125 return( ( ssl->in_msg[1] << 16 ) |
4126 ( ssl->in_msg[2] << 8 ) |
4127 ssl->in_msg[3] );
4128}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004129
Simon Butcher99000142016-10-13 17:21:01 +01004130int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004135 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004137 }
4138
Hanno Becker12555c62018-08-16 12:47:53 +01004139 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004142 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004143 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004146 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004147 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004148 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004149 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004150
Hanno Becker44650b72018-08-16 12:51:11 +01004151 if( ssl_check_hs_header( ssl ) != 0 )
4152 {
4153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4154 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4155 }
4156
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004157 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004158 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4159 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4160 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4161 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004162 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004163 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4164 {
4165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4166 recv_msg_seq,
4167 ssl->handshake->in_msg_seq ) );
4168 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4169 }
4170
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004171 /* Retransmit only on last message from previous flight, to avoid
4172 * too many retransmissions.
4173 * Besides, No sane server ever retransmits HelloVerifyRequest */
4174 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004178 "message_seq = %d, start_of_flight = %d",
4179 recv_msg_seq,
4180 ssl->handshake->in_flight_start_seq ) );
4181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004185 return( ret );
4186 }
4187 }
4188 else
4189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004191 "message_seq = %d, expected = %d",
4192 recv_msg_seq,
4193 ssl->handshake->in_msg_seq ) );
4194 }
4195
Hanno Becker90333da2017-10-10 11:27:13 +01004196 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004197 }
4198 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004199
Hanno Becker6d97ef52018-08-16 13:09:04 +01004200 /* Message reassembly is handled alongside buffering of future
4201 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004202 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004203 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004204 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004207 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004208 }
4209 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004210 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004211#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004212#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004213 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004214 /* With TLS we don't handle fragmentation (for now) */
4215 if( ssl->in_msglen < ssl->in_hslen )
4216 {
4217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4218 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4219 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004220 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004221#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004222
Simon Butcher99000142016-10-13 17:21:01 +01004223 return( 0 );
4224}
4225
4226void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4227{
Hanno Becker0271f962018-08-16 13:23:47 +01004228 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004229
Hanno Becker0271f962018-08-16 13:23:47 +01004230 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004231 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004232 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004233 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004234
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004235 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004237 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004238 ssl->handshake != NULL )
4239 {
Hanno Becker0271f962018-08-16 13:23:47 +01004240 unsigned offset;
4241 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004242
Hanno Becker0271f962018-08-16 13:23:47 +01004243 /* Increment handshake sequence number */
4244 hs->in_msg_seq++;
4245
4246 /*
4247 * Clear up handshake buffering and reassembly structure.
4248 */
4249
4250 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004251 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004252
4253 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004254 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4255 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004256 offset++, hs_buf++ )
4257 {
4258 *hs_buf = *(hs_buf + 1);
4259 }
4260
4261 /* Create a fresh last entry */
4262 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004263 }
4264#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004265}
4266
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004267/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004268 * DTLS anti-replay: RFC 6347 4.1.2.6
4269 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004270 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4271 * Bit n is set iff record number in_window_top - n has been seen.
4272 *
4273 * Usually, in_window_top is the last record number seen and the lsb of
4274 * in_window is set. The only exception is the initial state (record number 0
4275 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4278static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004279{
4280 ssl->in_window_top = 0;
4281 ssl->in_window = 0;
4282}
4283
4284static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4285{
4286 return( ( (uint64_t) buf[0] << 40 ) |
4287 ( (uint64_t) buf[1] << 32 ) |
4288 ( (uint64_t) buf[2] << 24 ) |
4289 ( (uint64_t) buf[3] << 16 ) |
4290 ( (uint64_t) buf[4] << 8 ) |
4291 ( (uint64_t) buf[5] ) );
4292}
4293
4294/*
4295 * Return 0 if sequence number is acceptable, -1 otherwise
4296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004298{
4299 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4300 uint64_t bit;
4301
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004302 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004303 return( 0 );
4304
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004305 if( rec_seqnum > ssl->in_window_top )
4306 return( 0 );
4307
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004308 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004309
4310 if( bit >= 64 )
4311 return( -1 );
4312
4313 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4314 return( -1 );
4315
4316 return( 0 );
4317}
4318
4319/*
4320 * Update replay window on new validated record
4321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004323{
4324 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4325
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004326 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004327 return;
4328
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004329 if( rec_seqnum > ssl->in_window_top )
4330 {
4331 /* Update window_top and the contents of the window */
4332 uint64_t shift = rec_seqnum - ssl->in_window_top;
4333
4334 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004335 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004336 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004337 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004338 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004339 ssl->in_window |= 1;
4340 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004341
4342 ssl->in_window_top = rec_seqnum;
4343 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004344 else
4345 {
4346 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004347 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004348
4349 if( bit < 64 ) /* Always true, but be extra sure */
4350 ssl->in_window |= (uint64_t) 1 << bit;
4351 }
4352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004353#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004354
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004355#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004356/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004357static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4358
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004359/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004360 * Without any SSL context, check if a datagram looks like a ClientHello with
4361 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004362 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004363 *
4364 * - if cookie is valid, return 0
4365 * - if ClientHello looks superficially valid but cookie is not,
4366 * fill obuf and set olen, then
4367 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4368 * - otherwise return a specific error code
4369 */
4370static int ssl_check_dtls_clihlo_cookie(
4371 mbedtls_ssl_cookie_write_t *f_cookie_write,
4372 mbedtls_ssl_cookie_check_t *f_cookie_check,
4373 void *p_cookie,
4374 const unsigned char *cli_id, size_t cli_id_len,
4375 const unsigned char *in, size_t in_len,
4376 unsigned char *obuf, size_t buf_len, size_t *olen )
4377{
4378 size_t sid_len, cookie_len;
4379 unsigned char *p;
4380
4381 if( f_cookie_write == NULL || f_cookie_check == NULL )
4382 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4383
4384 /*
4385 * Structure of ClientHello with record and handshake headers,
4386 * and expected values. We don't need to check a lot, more checks will be
4387 * done when actually parsing the ClientHello - skipping those checks
4388 * avoids code duplication and does not make cookie forging any easier.
4389 *
4390 * 0-0 ContentType type; copied, must be handshake
4391 * 1-2 ProtocolVersion version; copied
4392 * 3-4 uint16 epoch; copied, must be 0
4393 * 5-10 uint48 sequence_number; copied
4394 * 11-12 uint16 length; (ignored)
4395 *
4396 * 13-13 HandshakeType msg_type; (ignored)
4397 * 14-16 uint24 length; (ignored)
4398 * 17-18 uint16 message_seq; copied
4399 * 19-21 uint24 fragment_offset; copied, must be 0
4400 * 22-24 uint24 fragment_length; (ignored)
4401 *
4402 * 25-26 ProtocolVersion client_version; (ignored)
4403 * 27-58 Random random; (ignored)
4404 * 59-xx SessionID session_id; 1 byte len + sid_len content
4405 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4406 * ...
4407 *
4408 * Minimum length is 61 bytes.
4409 */
4410 if( in_len < 61 ||
4411 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4412 in[3] != 0 || in[4] != 0 ||
4413 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4414 {
4415 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4416 }
4417
4418 sid_len = in[59];
4419 if( sid_len > in_len - 61 )
4420 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4421
4422 cookie_len = in[60 + sid_len];
4423 if( cookie_len > in_len - 60 )
4424 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4425
4426 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4427 cli_id, cli_id_len ) == 0 )
4428 {
4429 /* Valid cookie */
4430 return( 0 );
4431 }
4432
4433 /*
4434 * If we get here, we've got an invalid cookie, let's prepare HVR.
4435 *
4436 * 0-0 ContentType type; copied
4437 * 1-2 ProtocolVersion version; copied
4438 * 3-4 uint16 epoch; copied
4439 * 5-10 uint48 sequence_number; copied
4440 * 11-12 uint16 length; olen - 13
4441 *
4442 * 13-13 HandshakeType msg_type; hello_verify_request
4443 * 14-16 uint24 length; olen - 25
4444 * 17-18 uint16 message_seq; copied
4445 * 19-21 uint24 fragment_offset; copied
4446 * 22-24 uint24 fragment_length; olen - 25
4447 *
4448 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4449 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4450 *
4451 * Minimum length is 28.
4452 */
4453 if( buf_len < 28 )
4454 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4455
4456 /* Copy most fields and adapt others */
4457 memcpy( obuf, in, 25 );
4458 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4459 obuf[25] = 0xfe;
4460 obuf[26] = 0xff;
4461
4462 /* Generate and write actual cookie */
4463 p = obuf + 28;
4464 if( f_cookie_write( p_cookie,
4465 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4466 {
4467 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4468 }
4469
4470 *olen = p - obuf;
4471
4472 /* Go back and fill length fields */
4473 obuf[27] = (unsigned char)( *olen - 28 );
4474
4475 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4476 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4477 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4478
4479 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4480 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4481
4482 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4483}
4484
4485/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004486 * Handle possible client reconnect with the same UDP quadruplet
4487 * (RFC 6347 Section 4.2.8).
4488 *
4489 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4490 * that looks like a ClientHello.
4491 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004492 * - if the input looks like a ClientHello without cookies,
4493 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004494 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004495 * - if the input looks like a ClientHello with a valid cookie,
4496 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004497 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004498 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004499 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004500 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004501 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4502 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004503 */
4504static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4505{
4506 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004507 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004508
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004509 ret = ssl_check_dtls_clihlo_cookie(
4510 ssl->conf->f_cookie_write,
4511 ssl->conf->f_cookie_check,
4512 ssl->conf->p_cookie,
4513 ssl->cli_id, ssl->cli_id_len,
4514 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004515 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004516
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4518
4519 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004521 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004522 * If the error is permanent we'll catch it later,
4523 * if it's not, then hopefully it'll work next time. */
4524 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4525
4526 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004527 }
4528
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004529 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004530 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004531 /* Got a valid cookie, partially reset context */
4532 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4533 {
4534 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4535 return( ret );
4536 }
4537
4538 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539 }
4540
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004541 return( ret );
4542}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004543#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004544
Hanno Becker46483f12019-05-03 13:25:54 +01004545static int ssl_check_record_type( uint8_t record_type )
4546{
4547 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4548 record_type != MBEDTLS_SSL_MSG_ALERT &&
4549 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4550 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4551 {
4552 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4553 }
4554
4555 return( 0 );
4556}
4557
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004558/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004559 * ContentType type;
4560 * ProtocolVersion version;
4561 * uint16 epoch; // DTLS only
4562 * uint48 sequence_number; // DTLS only
4563 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004564 *
4565 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004566 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004567 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4568 *
4569 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004570 * 1. proceed with the record if this function returns 0
4571 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4572 * 3. return CLIENT_RECONNECT if this function return that value
4573 * 4. drop the whole datagram if this function returns anything else.
4574 * Point 2 is needed when the peer is resending, and we have already received
4575 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004578{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004579 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004580 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Hanno Becker8b09b732019-05-08 12:03:28 +01004582 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004583
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004585 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004586
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004587 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004589 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004590 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4591 ssl->conf->cid_len != 0 )
4592 {
4593 /* Shift pointers to account for record header including CID
4594 * struct {
4595 * ContentType special_type = tls12_cid;
4596 * ProtocolVersion version;
4597 * uint16 epoch;
4598 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004599 * opaque cid[cid_length]; // Additional field compared to
4600 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004601 * uint16 length;
4602 * opaque enc_content[DTLSCiphertext.length];
4603 * } DTLSCiphertext;
4604 */
4605
4606 /* So far, we only support static CID lengths
4607 * fixed in the configuration. */
4608 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4609 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4610 }
4611 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004613 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004616
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004617#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004618 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004619 * Section 4.1.2.7, that is, send alert only with TLS */
4620 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4621 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004622 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4623 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004624 }
4625#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004628 }
4629
4630 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004631 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4634 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004635 }
4636
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004637 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4640 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 }
4642
Hanno Becker8b09b732019-05-08 12:03:28 +01004643 /* Now that the total length of the record header is known, ensure
4644 * that the current datagram is large enough to hold it.
4645 * This would fail, for example, if we received a datagram of
4646 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4647 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4648 if( ret != 0 )
4649 {
4650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4651 return( ret );
4652 }
4653 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4654
4655 /* Parse and validate record length
4656 * This must happen after the CID parsing because
4657 * its position in the record header depends on
4658 * the presence of a CID. */
4659
4660 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004661 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004662 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4665 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004666 }
4667
Hanno Becker8b09b732019-05-08 12:03:28 +01004668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004669 "version = [%d:%d], msglen = %d",
4670 ssl->in_msgtype,
4671 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004672
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004673 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004674 * DTLS-related tests.
4675 * Check epoch before checking length constraint because
4676 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4677 * message gets duplicated before the corresponding Finished message,
4678 * the second ChangeCipherSpec should be discarded because it belongs
4679 * to an old epoch, but not because its length is shorter than
4680 * the minimum record length for packets using the new record transform.
4681 * Note that these two kinds of failures are handled differently,
4682 * as an unexpected record is silently skipped but an invalid
4683 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004684 */
4685#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004686 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004687 {
4688 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 /* Check epoch (and sequence number) with DTLS */
4691 if( rec_epoch != ssl->in_epoch )
4692 {
4693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4694 "expected %d, received %d",
4695 ssl->in_epoch, rec_epoch ) );
4696
4697#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4698 /*
4699 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4700 * access the first byte of record content (handshake type), as we
4701 * have an active transform (possibly iv_len != 0), so use the
4702 * fact that the record header len is 13 instead.
4703 */
4704 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4705 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4706 rec_epoch == 0 &&
4707 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4708 ssl->in_left > 13 &&
4709 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4710 {
4711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4712 "from the same port" ) );
4713 return( ssl_handle_possible_reconnect( ssl ) );
4714 }
4715 else
4716#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004717 {
4718 /* Consider buffering the record. */
4719 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4720 {
4721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4722 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4723 }
4724
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004725 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004726 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004727 }
4728
4729#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4730 /* Replay detection only works for the current epoch */
4731 if( rec_epoch == ssl->in_epoch &&
4732 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4733 {
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4735 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4736 }
4737#endif
4738 }
4739#endif /* MBEDTLS_SSL_PROTO_DTLS */
4740
Hanno Becker52c6dc62017-05-26 16:07:36 +01004741
4742 /* Check length against bounds of the current transform and version */
4743 if( ssl->transform_in == NULL )
4744 {
4745 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004746 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004747 {
4748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4749 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4750 }
4751 }
4752 else
4753 {
4754 if( ssl->in_msglen < ssl->transform_in->minlen )
4755 {
4756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4757 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4758 }
4759
4760#if defined(MBEDTLS_SSL_PROTO_SSL3)
4761 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004762 ssl->in_msglen > ssl->transform_in->minlen + 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#endif
4768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4769 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4770 /*
4771 * TLS encrypted messages can have up to 256 bytes of padding
4772 */
4773 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4774 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004775 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004776 {
4777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4778 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4779 }
4780#endif
4781 }
4782
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783 return( 0 );
4784}
Paul Bakker5121ce52009-01-03 21:22:43 +00004785
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004786/*
4787 * If applicable, decrypt (and decompress) record content
4788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004790{
4791 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004793 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004794 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4797 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004801 ret = mbedtls_ssl_hw_record_read( ssl );
4802 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4805 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004806 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004807
4808 if( ret == 0 )
4809 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004812 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004814 mbedtls_record rec;
4815
4816 rec.buf = ssl->in_iv;
4817 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4818 - ( ssl->in_iv - ssl->in_buf );
4819 rec.data_len = ssl->in_msglen;
4820 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004821#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004822 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004823 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004824#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004825
4826 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4827 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4828 ssl->conf->transport, rec.ver );
4829 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004830 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4831 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004834
Hanno Beckera5a2b082019-05-15 14:03:01 +01004835#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004836 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4837 ssl->conf->ignore_unexpected_cid
4838 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4839 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004841 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004844
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 return( ret );
4846 }
4847
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004848 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004849 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004850 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4851 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004852 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004853
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004854 /* The record content type may change during decryption,
4855 * so re-read it. */
4856 ssl->in_msgtype = rec.type;
4857 /* Also update the input buffer, because unfortunately
4858 * the server-side ssl_parse_client_hello() reparses the
4859 * record header when receiving a ClientHello initiating
4860 * a renegotiation. */
4861 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004862 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004863 ssl->in_msglen = rec.data_len;
4864 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4865 ssl->in_len[1] = (unsigned char)( rec.data_len );
4866
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4868 ssl->in_msg, ssl->in_msglen );
4869
Hanno Beckera5a2b082019-05-15 14:03:01 +01004870#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004871 /* We have already checked the record content type
4872 * in ssl_parse_record_header(), failing or silently
4873 * dropping the record in the case of an unknown type.
4874 *
4875 * Since with the use of CIDs, the record content type
4876 * might change during decryption, re-check the record
4877 * content type, but treat a failure as fatal this time. */
4878 if( ssl_check_record_type( ssl->in_msgtype ) )
4879 {
4880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4881 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4882 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004883#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004884
Angus Grattond8213d02016-05-25 20:56:48 +10004885 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4888 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004889 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004890 else if( ssl->in_msglen == 0 )
4891 {
4892#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4893 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4894 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4895 {
4896 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4899 }
4900#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4901
4902 ssl->nb_zero++;
4903
4904 /*
4905 * Three or more empty messages may be a DoS attack
4906 * (excessive CPU consumption).
4907 */
4908 if( ssl->nb_zero > 3 )
4909 {
4910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004911 "messages, possible DoS attack" ) );
4912 /* Treat the records as if they were not properly authenticated,
4913 * thereby failing the connection if we see more than allowed
4914 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004915 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4916 }
4917 }
4918 else
4919 ssl->nb_zero = 0;
4920
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004921 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4922#if defined(MBEDTLS_SSL_PROTO_TLS)
4923 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004924 {
4925 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004926 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004927 if( ++ssl->in_ctr[i - 1] != 0 )
4928 break;
4929
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004930 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004931 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4934 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4935 }
4936 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004937#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004938
Paul Bakker5121ce52009-01-03 21:22:43 +00004939 }
4940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004942 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004944 {
4945 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004947 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004948 return( ret );
4949 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004950 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004953#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004954 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004956 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004957 }
4958#endif
4959
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004960 return( 0 );
4961}
4962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004963static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004964
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004965/*
4966 * Read a record.
4967 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004968 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4969 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4970 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004971 */
Hanno Becker1097b342018-08-15 14:09:41 +01004972
4973/* Helper functions for mbedtls_ssl_read_record(). */
4974static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004975static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4976static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004977
Hanno Becker327c93b2018-08-15 13:56:18 +01004978int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004979 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004980{
4981 int ret;
4982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004984
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004985 if( ssl->keep_current_message == 0 )
4986 {
4987 do {
Simon Butcher99000142016-10-13 17:21:01 +01004988
Hanno Becker26994592018-08-15 14:14:59 +01004989 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004990 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004991 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004992
Hanno Beckere74d5562018-08-15 14:26:08 +01004993 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004994 {
Hanno Becker40f50842018-08-15 14:48:01 +01004995#if defined(MBEDTLS_SSL_PROTO_DTLS)
4996 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004997
Hanno Becker40f50842018-08-15 14:48:01 +01004998 /* We only check for buffered messages if the
4999 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005000 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005001 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005002 {
Hanno Becker40f50842018-08-15 14:48:01 +01005003 if( ssl_load_buffered_message( ssl ) == 0 )
5004 have_buffered = 1;
5005 }
5006
5007 if( have_buffered == 0 )
5008#endif /* MBEDTLS_SSL_PROTO_DTLS */
5009 {
5010 ret = ssl_get_next_record( ssl );
5011 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5012 continue;
5013
5014 if( ret != 0 )
5015 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005016 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005017 return( ret );
5018 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005019 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005020 }
5021
5022 ret = mbedtls_ssl_handle_message_type( ssl );
5023
Hanno Becker40f50842018-08-15 14:48:01 +01005024#if defined(MBEDTLS_SSL_PROTO_DTLS)
5025 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5026 {
5027 /* Buffer future message */
5028 ret = ssl_buffer_message( ssl );
5029 if( ret != 0 )
5030 return( ret );
5031
5032 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5033 }
5034#endif /* MBEDTLS_SSL_PROTO_DTLS */
5035
Hanno Becker90333da2017-10-10 11:27:13 +01005036 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5037 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005038
5039 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005040 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005041 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005042 return( ret );
5043 }
5044
Hanno Becker327c93b2018-08-15 13:56:18 +01005045 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005046 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005047 {
5048 mbedtls_ssl_update_handshake_status( ssl );
5049 }
Simon Butcher99000142016-10-13 17:21:01 +01005050 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005051 else
Simon Butcher99000142016-10-13 17:21:01 +01005052 {
Hanno Becker02f59072018-08-15 14:00:24 +01005053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005054 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005055 }
5056
5057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5058
5059 return( 0 );
5060}
5061
Hanno Becker40f50842018-08-15 14:48:01 +01005062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005063static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005064{
Hanno Becker40f50842018-08-15 14:48:01 +01005065 if( ssl->in_left > ssl->next_record_offset )
5066 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005067
Hanno Becker40f50842018-08-15 14:48:01 +01005068 return( 0 );
5069}
5070
5071static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5072{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005073 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005074 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005075 int ret = 0;
5076
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005077 if( hs == NULL )
5078 return( -1 );
5079
Hanno Beckere00ae372018-08-20 09:39:42 +01005080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5081
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005082 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5083 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5084 {
5085 /* Check if we have seen a ChangeCipherSpec before.
5086 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005087 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005088 {
5089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5090 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005091 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005092 }
5093
Hanno Becker39b8bc92018-08-28 17:17:13 +01005094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005095 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5096 ssl->in_msglen = 1;
5097 ssl->in_msg[0] = 1;
5098
5099 /* As long as they are equal, the exact value doesn't matter. */
5100 ssl->in_left = 0;
5101 ssl->next_record_offset = 0;
5102
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005103 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005104 goto exit;
5105 }
Hanno Becker37f95322018-08-16 13:55:32 +01005106
Hanno Beckerb8f50142018-08-28 10:01:34 +01005107#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005108 /* Debug only */
5109 {
5110 unsigned offset;
5111 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5112 {
5113 hs_buf = &hs->buffering.hs[offset];
5114 if( hs_buf->is_valid == 1 )
5115 {
5116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5117 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005118 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005119 }
5120 }
5121 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005122#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005123
5124 /* Check if we have buffered and/or fully reassembled the
5125 * next handshake message. */
5126 hs_buf = &hs->buffering.hs[0];
5127 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5128 {
5129 /* Synthesize a record containing the buffered HS message. */
5130 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5131 ( hs_buf->data[2] << 8 ) |
5132 hs_buf->data[3];
5133
5134 /* Double-check that we haven't accidentally buffered
5135 * a message that doesn't fit into the input buffer. */
5136 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5137 {
5138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5139 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5140 }
5141
5142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5143 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5144 hs_buf->data, msg_len + 12 );
5145
5146 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5147 ssl->in_hslen = msg_len + 12;
5148 ssl->in_msglen = msg_len + 12;
5149 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5150
5151 ret = 0;
5152 goto exit;
5153 }
5154 else
5155 {
5156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5157 hs->in_msg_seq ) );
5158 }
5159
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005160 ret = -1;
5161
5162exit:
5163
5164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5165 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005166}
5167
Hanno Beckera02b0b42018-08-21 17:20:27 +01005168static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5169 size_t desired )
5170{
5171 int offset;
5172 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5174 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005175
Hanno Becker01315ea2018-08-21 17:22:17 +01005176 /* Get rid of future records epoch first, if such exist. */
5177 ssl_free_buffered_record( ssl );
5178
5179 /* Check if we have enough space available now. */
5180 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5181 hs->buffering.total_bytes_buffered ) )
5182 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005184 return( 0 );
5185 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005186
Hanno Becker4f432ad2018-08-28 10:02:32 +01005187 /* We don't have enough space to buffer the next expected handshake
5188 * message. Remove buffers used for future messages to gain space,
5189 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005190 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5191 offset >= 0; offset-- )
5192 {
5193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5194 offset ) );
5195
Hanno Beckerb309b922018-08-23 13:18:05 +01005196 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005197
5198 /* Check if we have enough space available now. */
5199 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5200 hs->buffering.total_bytes_buffered ) )
5201 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005203 return( 0 );
5204 }
5205 }
5206
5207 return( -1 );
5208}
5209
Hanno Becker40f50842018-08-15 14:48:01 +01005210static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5211{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005212 int ret = 0;
5213 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5214
5215 if( hs == NULL )
5216 return( 0 );
5217
5218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5219
5220 switch( ssl->in_msgtype )
5221 {
5222 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005224
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005225 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005226 break;
5227
5228 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005229 {
5230 unsigned recv_msg_seq_offset;
5231 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5232 mbedtls_ssl_hs_buffer *hs_buf;
5233 size_t msg_len = ssl->in_hslen - 12;
5234
5235 /* We should never receive an old handshake
5236 * message - double-check nonetheless. */
5237 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5238 {
5239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5240 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5241 }
5242
5243 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5244 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5245 {
5246 /* Silently ignore -- message too far in the future */
5247 MBEDTLS_SSL_DEBUG_MSG( 2,
5248 ( "Ignore future HS message with sequence number %u, "
5249 "buffering window %u - %u",
5250 recv_msg_seq, ssl->handshake->in_msg_seq,
5251 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5252
5253 goto exit;
5254 }
5255
5256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5257 recv_msg_seq, recv_msg_seq_offset ) );
5258
5259 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5260
5261 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005262 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005263 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005264 size_t reassembly_buf_sz;
5265
Hanno Becker37f95322018-08-16 13:55:32 +01005266 hs_buf->is_fragmented =
5267 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5268
5269 /* We copy the message back into the input buffer
5270 * after reassembly, so check that it's not too large.
5271 * This is an implementation-specific limitation
5272 * and not one from the standard, hence it is not
5273 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005274 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005275 {
5276 /* Ignore message */
5277 goto exit;
5278 }
5279
Hanno Beckere0b150f2018-08-21 15:51:03 +01005280 /* Check if we have enough space to buffer the message. */
5281 if( hs->buffering.total_bytes_buffered >
5282 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5283 {
5284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5286 }
5287
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005288 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5289 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005290
5291 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5292 hs->buffering.total_bytes_buffered ) )
5293 {
5294 if( recv_msg_seq_offset > 0 )
5295 {
5296 /* If we can't buffer a future message because
5297 * of space limitations -- ignore. */
5298 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",
5299 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5300 (unsigned) hs->buffering.total_bytes_buffered ) );
5301 goto exit;
5302 }
Hanno Beckere1801392018-08-21 16:51:05 +01005303 else
5304 {
5305 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",
5306 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5307 (unsigned) hs->buffering.total_bytes_buffered ) );
5308 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005309
Hanno Beckera02b0b42018-08-21 17:20:27 +01005310 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005311 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005312 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",
5313 (unsigned) msg_len,
5314 (unsigned) reassembly_buf_sz,
5315 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005316 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005317 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5318 goto exit;
5319 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005320 }
5321
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5323 msg_len ) );
5324
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005325 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5326 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005327 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005328 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005329 goto exit;
5330 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005331 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005332
5333 /* Prepare final header: copy msg_type, length and message_seq,
5334 * then add standardised fragment_offset and fragment_length */
5335 memcpy( hs_buf->data, ssl->in_msg, 6 );
5336 memset( hs_buf->data + 6, 0, 3 );
5337 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5338
5339 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005340
5341 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005342 }
5343 else
5344 {
5345 /* Make sure msg_type and length are consistent */
5346 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5347 {
5348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5349 /* Ignore */
5350 goto exit;
5351 }
5352 }
5353
Hanno Becker4422bbb2018-08-20 09:40:19 +01005354 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005355 {
5356 size_t frag_len, frag_off;
5357 unsigned char * const msg = hs_buf->data + 12;
5358
5359 /*
5360 * Check and copy current fragment
5361 */
5362
5363 /* Validation of header fields already done in
5364 * mbedtls_ssl_prepare_handshake_record(). */
5365 frag_off = ssl_get_hs_frag_off( ssl );
5366 frag_len = ssl_get_hs_frag_len( ssl );
5367
5368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5369 frag_off, frag_len ) );
5370 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5371
5372 if( hs_buf->is_fragmented )
5373 {
5374 unsigned char * const bitmask = msg + msg_len;
5375 ssl_bitmask_set( bitmask, frag_off, frag_len );
5376 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5377 msg_len ) == 0 );
5378 }
5379 else
5380 {
5381 hs_buf->is_complete = 1;
5382 }
5383
5384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5385 hs_buf->is_complete ? "" : "not yet " ) );
5386 }
5387
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005388 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005389 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005390
5391 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005392 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005393 break;
5394 }
5395
5396exit:
5397
5398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5399 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005400}
5401#endif /* MBEDTLS_SSL_PROTO_DTLS */
5402
Hanno Becker1097b342018-08-15 14:09:41 +01005403static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005404{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005405 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 * Consume last content-layer message and potentially
5407 * update in_msglen which keeps track of the contents'
5408 * consumption state.
5409 *
5410 * (1) Handshake messages:
5411 * Remove last handshake message, move content
5412 * and adapt in_msglen.
5413 *
5414 * (2) Alert messages:
5415 * Consume whole record content, in_msglen = 0.
5416 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005417 * (3) Change cipher spec:
5418 * Consume whole record content, in_msglen = 0.
5419 *
5420 * (4) Application data:
5421 * Don't do anything - the record layer provides
5422 * the application data as a stream transport
5423 * and consumes through mbedtls_ssl_read only.
5424 *
5425 */
5426
5427 /* Case (1): Handshake messages */
5428 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005429 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005430 /* Hard assertion to be sure that no application data
5431 * is in flight, as corrupting ssl->in_msglen during
5432 * ssl->in_offt != NULL is fatal. */
5433 if( ssl->in_offt != NULL )
5434 {
5435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5437 }
5438
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005439 /*
5440 * Get next Handshake message in the current record
5441 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005442
Hanno Becker4a810fb2017-05-24 16:27:30 +01005443 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005444 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005445 * current handshake content: If DTLS handshake
5446 * fragmentation is used, that's the fragment
5447 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005448 * size here is faulty and should be changed at
5449 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005450 * (2) While it doesn't seem to cause problems, one
5451 * has to be very careful not to assume that in_hslen
5452 * is always <= in_msglen in a sensible communication.
5453 * Again, it's wrong for DTLS handshake fragmentation.
5454 * The following check is therefore mandatory, and
5455 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005456 * Additionally, ssl->in_hslen might be arbitrarily out of
5457 * bounds after handling a DTLS message with an unexpected
5458 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 */
5460 if( ssl->in_hslen < ssl->in_msglen )
5461 {
5462 ssl->in_msglen -= ssl->in_hslen;
5463 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5464 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005465
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5467 ssl->in_msg, ssl->in_msglen );
5468 }
5469 else
5470 {
5471 ssl->in_msglen = 0;
5472 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005473
Hanno Becker4a810fb2017-05-24 16:27:30 +01005474 ssl->in_hslen = 0;
5475 }
5476 /* Case (4): Application data */
5477 else if( ssl->in_offt != NULL )
5478 {
5479 return( 0 );
5480 }
5481 /* Everything else (CCS & Alerts) */
5482 else
5483 {
5484 ssl->in_msglen = 0;
5485 }
5486
Hanno Becker1097b342018-08-15 14:09:41 +01005487 return( 0 );
5488}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005489
Hanno Beckere74d5562018-08-15 14:26:08 +01005490static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5491{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005492 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005493 return( 1 );
5494
5495 return( 0 );
5496}
5497
Hanno Becker5f066e72018-08-16 14:56:31 +01005498#if defined(MBEDTLS_SSL_PROTO_DTLS)
5499
5500static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5501{
5502 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5503 if( hs == NULL )
5504 return;
5505
Hanno Becker01315ea2018-08-21 17:22:17 +01005506 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005507 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005508 hs->buffering.total_bytes_buffered -=
5509 hs->buffering.future_record.len;
5510
5511 mbedtls_free( hs->buffering.future_record.data );
5512 hs->buffering.future_record.data = NULL;
5513 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005514}
5515
5516static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5517{
5518 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5519 unsigned char * rec;
5520 size_t rec_len;
5521 unsigned rec_epoch;
5522
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005523 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005524 return( 0 );
5525
5526 if( hs == NULL )
5527 return( 0 );
5528
Hanno Becker5f066e72018-08-16 14:56:31 +01005529 rec = hs->buffering.future_record.data;
5530 rec_len = hs->buffering.future_record.len;
5531 rec_epoch = hs->buffering.future_record.epoch;
5532
5533 if( rec == NULL )
5534 return( 0 );
5535
Hanno Becker4cb782d2018-08-20 11:19:05 +01005536 /* Only consider loading future records if the
5537 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005538 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005539 return( 0 );
5540
Hanno Becker5f066e72018-08-16 14:56:31 +01005541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5542
5543 if( rec_epoch != ssl->in_epoch )
5544 {
5545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5546 goto exit;
5547 }
5548
5549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5550
5551 /* Double-check that the record is not too large */
5552 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5553 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5554 {
5555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5557 }
5558
5559 memcpy( ssl->in_hdr, rec, rec_len );
5560 ssl->in_left = rec_len;
5561 ssl->next_record_offset = 0;
5562
5563 ssl_free_buffered_record( ssl );
5564
5565exit:
5566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5567 return( 0 );
5568}
5569
5570static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5571{
5572 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5573 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005574 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005575
5576 /* Don't buffer future records outside handshakes. */
5577 if( hs == NULL )
5578 return( 0 );
5579
5580 /* Only buffer handshake records (we are only interested
5581 * in Finished messages). */
5582 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5583 return( 0 );
5584
5585 /* Don't buffer more than one future epoch record. */
5586 if( hs->buffering.future_record.data != NULL )
5587 return( 0 );
5588
Hanno Becker01315ea2018-08-21 17:22:17 +01005589 /* Don't buffer record if there's not enough buffering space remaining. */
5590 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5591 hs->buffering.total_bytes_buffered ) )
5592 {
5593 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",
5594 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5595 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005596 return( 0 );
5597 }
5598
Hanno Becker5f066e72018-08-16 14:56:31 +01005599 /* Buffer record */
5600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5601 ssl->in_epoch + 1 ) );
5602 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5603 rec_hdr_len + ssl->in_msglen );
5604
5605 /* ssl_parse_record_header() only considers records
5606 * of the next epoch as candidates for buffering. */
5607 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005608 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005609
5610 hs->buffering.future_record.data =
5611 mbedtls_calloc( 1, hs->buffering.future_record.len );
5612 if( hs->buffering.future_record.data == NULL )
5613 {
5614 /* If we run out of RAM trying to buffer a
5615 * record from the next epoch, just ignore. */
5616 return( 0 );
5617 }
5618
Hanno Becker01315ea2018-08-21 17:22:17 +01005619 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005620
Hanno Becker01315ea2018-08-21 17:22:17 +01005621 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005622 return( 0 );
5623}
5624
5625#endif /* MBEDTLS_SSL_PROTO_DTLS */
5626
Hanno Beckere74d5562018-08-15 14:26:08 +01005627static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005628{
5629 int ret;
5630
Hanno Becker5f066e72018-08-16 14:56:31 +01005631#if defined(MBEDTLS_SSL_PROTO_DTLS)
5632 /* We might have buffered a future record; if so,
5633 * and if the epoch matches now, load it.
5634 * On success, this call will set ssl->in_left to
5635 * the length of the buffered record, so that
5636 * the calls to ssl_fetch_input() below will
5637 * essentially be no-ops. */
5638 ret = ssl_load_buffered_record( ssl );
5639 if( ret != 0 )
5640 return( ret );
5641#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005642
Hanno Becker8b09b732019-05-08 12:03:28 +01005643 /* Reset in pointers to default state for TLS/DTLS records,
5644 * assuming no CID and no offset between record content and
5645 * record plaintext. */
5646 ssl_update_in_pointers( ssl );
5647
5648 /* Ensure that we have enough space available for the default form
5649 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5650 * with no space for CIDs counted in). */
5651 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5652 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005654 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005655 return( ret );
5656 }
5657
5658 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005661 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005662 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005663 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005664 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5665 {
5666 ret = ssl_buffer_future_record( ssl );
5667 if( ret != 0 )
5668 return( ret );
5669
5670 /* Fall through to handling of unexpected records */
5671 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5672 }
5673
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005674 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5675 {
5676 /* Skip unexpected record (but not whole datagram) */
5677 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005678 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005679
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5681 "(header)" ) );
5682 }
5683 else
5684 {
5685 /* Skip invalid record and the rest of the datagram */
5686 ssl->next_record_offset = 0;
5687 ssl->in_left = 0;
5688
5689 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5690 "(header)" ) );
5691 }
5692
5693 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005694 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005695 }
5696#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005697 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005698 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005699
5700 /*
5701 * Read and optionally decrypt the message contents
5702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005704 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005707 return( ret );
5708 }
5709
5710 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005712 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005713 {
Hanno Becker43395762019-05-03 14:46:38 +01005714 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005715 if( ssl->next_record_offset < ssl->in_left )
5716 {
5717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5718 }
5719 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005720 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005721#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005722#if defined(MBEDTLS_SSL_PROTO_TLS)
5723 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005725 }
5726#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005727
5728 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005731 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005732 {
5733 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005734 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005735 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005736 /* Except when waiting for Finished as a bad mac here
5737 * probably means something went wrong in the handshake
5738 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5739 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5740 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5741 {
5742#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5743 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5744 {
5745 mbedtls_ssl_send_alert_message( ssl,
5746 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5747 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5748 }
5749#endif
5750 return( ret );
5751 }
5752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005754 if( ssl->conf->badmac_limit != 0 &&
5755 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5758 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005759 }
5760#endif
5761
Hanno Becker4a810fb2017-05-24 16:27:30 +01005762 /* As above, invalid records cause
5763 * dismissal of the whole datagram. */
5764
5765 ssl->next_record_offset = 0;
5766 ssl->in_left = 0;
5767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005769 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005770 }
5771
5772 return( ret );
5773 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005774 MBEDTLS_SSL_TRANSPORT_ELSE
5775#endif /* MBEDTLS_SSL_PROTO_DTLS */
5776#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005777 {
5778 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005779#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5780 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005782 mbedtls_ssl_send_alert_message( ssl,
5783 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5784 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005785 }
5786#endif
5787 return( ret );
5788 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005789#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005790 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005791
Simon Butcher99000142016-10-13 17:21:01 +01005792 return( 0 );
5793}
5794
5795int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5796{
5797 int ret;
5798
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005799 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005800 * Handle particular types of records
5801 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005803 {
Simon Butcher99000142016-10-13 17:21:01 +01005804 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5805 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005806 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 }
5809
Hanno Beckere678eaa2018-08-21 14:57:46 +01005810 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005811 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005812 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005813 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5815 ssl->in_msglen ) );
5816 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005817 }
5818
Hanno Beckere678eaa2018-08-21 14:57:46 +01005819 if( ssl->in_msg[0] != 1 )
5820 {
5821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5822 ssl->in_msg[0] ) );
5823 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5824 }
5825
5826#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005827 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005828 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5829 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5830 {
5831 if( ssl->handshake == NULL )
5832 {
5833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5834 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5835 }
5836
5837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5838 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5839 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005840#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005841 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005843 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005845 if( ssl->in_msglen != 2 )
5846 {
5847 /* Note: Standard allows for more than one 2 byte alert
5848 to be packed in a single message, but Mbed TLS doesn't
5849 currently support this. */
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5851 ssl->in_msglen ) );
5852 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5853 }
5854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 ssl->in_msg[0], ssl->in_msg[1] ) );
5857
5858 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005859 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005861 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005864 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005866 }
5867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005868 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5869 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5872 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005874
5875#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5876 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5877 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5878 {
Hanno Becker90333da2017-10-10 11:27:13 +01005879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005880 /* Will be handled when trying to parse ServerHello */
5881 return( 0 );
5882 }
5883#endif
5884
5885#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5886 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5887 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5888 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5889 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5890 {
5891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5892 /* Will be handled in mbedtls_ssl_parse_certificate() */
5893 return( 0 );
5894 }
5895#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5896
5897 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005898 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 }
5900
Hanno Beckerc76c6192017-06-06 10:03:17 +01005901#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005902 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005903 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005904 /* Drop unexpected ApplicationData records,
5905 * except at the beginning of renegotiations */
5906 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5907 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5908#if defined(MBEDTLS_SSL_RENEGOTIATION)
5909 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5910 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005911#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005912 )
5913 {
5914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5915 return( MBEDTLS_ERR_SSL_NON_FATAL );
5916 }
5917
5918 if( ssl->handshake != NULL &&
5919 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5920 {
5921 ssl_handshake_wrapup_free_hs_transform( ssl );
5922 }
5923 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005925
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 return( 0 );
5927}
5928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005930{
5931 int ret;
5932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5934 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5935 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005936 {
5937 return( ret );
5938 }
5939
5940 return( 0 );
5941}
5942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005944 unsigned char level,
5945 unsigned char message )
5946{
5947 int ret;
5948
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005949 if( ssl == NULL || ssl->conf == NULL )
5950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005955 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005956 ssl->out_msglen = 2;
5957 ssl->out_msg[0] = level;
5958 ssl->out_msg[1] = message;
5959
Hanno Becker67bc7c32018-08-06 11:33:50 +01005960 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005962 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005963 return( ret );
5964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005966
5967 return( 0 );
5968}
5969
Paul Bakker5121ce52009-01-03 21:22:43 +00005970/*
5971 * Handshake functions
5972 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005973#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005974/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005976{
Hanno Becker8759e162017-12-27 21:34:08 +00005977 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005980
Hanno Becker5097cba2019-02-05 13:36:46 +00005981 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005984 ssl->state++;
5985 return( 0 );
5986 }
5987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005990}
5991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005993{
Hanno Becker8759e162017-12-27 21:34:08 +00005994 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005997
Hanno Becker5097cba2019-02-05 13:36:46 +00005998 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006001 ssl->state++;
6002 return( 0 );
6003 }
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006007}
Gilles Peskinef9828522017-05-03 12:28:43 +02006008
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006009#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006010/* Some certificate support -> implement write and parse */
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006013{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006014 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006015 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006016 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006017 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020
Hanno Becker5097cba2019-02-05 13:36:46 +00006021 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006024 ssl->state++;
6025 return( 0 );
6026 }
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006029 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 {
6031 if( ssl->client_auth == 0 )
6032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006034 ssl->state++;
6035 return( 0 );
6036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 /*
6040 * If using SSLv3 and got no cert, send an Alert message
6041 * (otherwise an empty Certificate message will be sent).
6042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6044 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006045 {
6046 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6048 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6049 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006052 goto write_msg;
6053 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#endif /* MBEDTLS_SSL_CLI_C */
6057#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006058 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6063 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 }
6065 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006069
6070 /*
6071 * 0 . 0 handshake type
6072 * 1 . 3 handshake length
6073 * 4 . 6 length of all certs
6074 * 7 . 9 length of cert. 1
6075 * 10 . n-1 peer certificate
6076 * n . n+2 length of cert. 2
6077 * n+3 . ... upper level cert, etc.
6078 */
6079 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006080 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081
Paul Bakker29087132010-03-21 21:03:34 +00006082 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 {
6084 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006085 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006088 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006089 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 }
6091
6092 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6093 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6094 ssl->out_msg[i + 2] = (unsigned char)( n );
6095
6096 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6097 i += n; crt = crt->next;
6098 }
6099
6100 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6101 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6102 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6103
6104 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6106 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006107
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006108#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006109write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006110#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006111
6112 ssl->state++;
6113
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006114 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006115 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 return( ret );
6118 }
6119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Paul Bakkered27a042013-04-18 22:46:23 +02006122 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006123}
6124
Hanno Becker285ff0c2019-01-31 07:44:03 +00006125#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006126static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6127 unsigned char *crt_buf,
6128 size_t crt_buf_len )
6129{
6130 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6131
6132 if( peer_crt == NULL )
6133 return( -1 );
6134
6135 if( peer_crt->raw.len != crt_buf_len )
6136 return( -1 );
6137
Hanno Becker68b856d2019-02-08 14:00:04 +00006138 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006139}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006140#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006141
Hanno Becker22141592019-02-05 12:38:15 +00006142static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6143{
6144 if( session->peer_cert != NULL )
6145 {
6146 mbedtls_x509_crt_free( session->peer_cert );
6147 mbedtls_free( session->peer_cert );
6148 session->peer_cert = NULL;
6149 }
6150}
6151
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006152/*
6153 * Once the certificate message is read, parse it into a cert chain and
6154 * perform basic checks, but leave actual verification to the caller
6155 */
Hanno Becker35e41772019-02-05 15:37:23 +00006156static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6157 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006158{
Hanno Becker35e41772019-02-05 15:37:23 +00006159 int ret;
6160#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6161 int crt_cnt=0;
6162#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006163 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006164 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006169 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6170 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006172 }
6173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006174 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6175 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6179 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006181 }
6182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006184
Paul Bakker5121ce52009-01-03 21:22:43 +00006185 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006186 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006188 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006189
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006190 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006194 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6195 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006197 }
6198
Hanno Becker33c3dc82019-01-30 14:46:46 +00006199 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6200 i += 3;
6201
Hanno Becker33c3dc82019-01-30 14:46:46 +00006202 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006203 while( i < ssl->in_hslen )
6204 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006205 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006206 if ( i + 3 > ssl->in_hslen ) {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006208 mbedtls_ssl_send_alert_message( ssl,
6209 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6210 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006211 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6212 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006213 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6214 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 if( ssl->in_msg[i] != 0 )
6216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006218 mbedtls_ssl_send_alert_message( ssl,
6219 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6220 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 }
6223
Hanno Becker33c3dc82019-01-30 14:46:46 +00006224 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006225 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6226 | (unsigned int) ssl->in_msg[i + 2];
6227 i += 3;
6228
6229 if( n < 128 || i + n > ssl->in_hslen )
6230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006232 mbedtls_ssl_send_alert_message( ssl,
6233 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6234 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 }
6237
Hanno Becker33c3dc82019-01-30 14:46:46 +00006238 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006239#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6240 if( crt_cnt++ == 0 &&
6241 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6242 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006243 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006244 /* During client-side renegotiation, check that the server's
6245 * end-CRTs hasn't changed compared to the initial handshake,
6246 * mitigating the triple handshake attack. On success, reuse
6247 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6249 if( ssl_check_peer_crt_unchanged( ssl,
6250 &ssl->in_msg[i],
6251 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006252 {
Hanno Becker35e41772019-02-05 15:37:23 +00006253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6254 mbedtls_ssl_send_alert_message( ssl,
6255 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6256 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6257 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006258 }
Hanno Becker35e41772019-02-05 15:37:23 +00006259
6260 /* Now we can safely free the original chain. */
6261 ssl_clear_peer_cert( ssl->session );
6262 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006263#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6264
Hanno Becker33c3dc82019-01-30 14:46:46 +00006265 /* Parse the next certificate in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006266 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006267 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006268 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006269 case 0: /*ok*/
6270 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6271 /* Ignore certificate with an unknown algorithm: maybe a
6272 prior certificate was already trusted. */
6273 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006274
Hanno Becker33c3dc82019-01-30 14:46:46 +00006275 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6276 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6277 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006278
Hanno Becker33c3dc82019-01-30 14:46:46 +00006279 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6280 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6281 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006282
Hanno Becker33c3dc82019-01-30 14:46:46 +00006283 default:
6284 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6285 crt_parse_der_failed:
6286 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6287 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6288 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006289 }
6290
6291 i += n;
6292 }
6293
Hanno Becker35e41772019-02-05 15:37:23 +00006294 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006295 return( 0 );
6296}
6297
Hanno Beckerb8a08572019-02-05 12:49:06 +00006298#if defined(MBEDTLS_SSL_SRV_C)
6299static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6300{
6301 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6302 return( -1 );
6303
6304#if defined(MBEDTLS_SSL_PROTO_SSL3)
6305 /*
6306 * Check if the client sent an empty certificate
6307 */
6308 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6309 {
6310 if( ssl->in_msglen == 2 &&
6311 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6312 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6313 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6314 {
6315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6316 return( 0 );
6317 }
6318
6319 return( -1 );
6320 }
6321#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6322
6323#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6324 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6325 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6326 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6327 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6328 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6329 {
6330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6331 return( 0 );
6332 }
6333
6334 return( -1 );
6335#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6336 MBEDTLS_SSL_PROTO_TLS1_2 */
6337}
6338#endif /* MBEDTLS_SSL_SRV_C */
6339
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006340/* Check if a certificate message is expected.
6341 * Return either
6342 * - SSL_CERTIFICATE_EXPECTED, or
6343 * - SSL_CERTIFICATE_SKIP
6344 * indicating whether a Certificate message is expected or not.
6345 */
6346#define SSL_CERTIFICATE_EXPECTED 0
6347#define SSL_CERTIFICATE_SKIP 1
6348static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6349 int authmode )
6350{
6351 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6352 ssl->handshake->ciphersuite_info;
6353
6354 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6355 return( SSL_CERTIFICATE_SKIP );
6356
6357#if defined(MBEDTLS_SSL_SRV_C)
6358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6359 {
6360 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6361 return( SSL_CERTIFICATE_SKIP );
6362
6363 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6364 {
6365 /* NOTE: Is it intentional that we set verify_result
6366 * to SKIP_VERIFY on server-side only? */
6367 ssl->session_negotiate->verify_result =
6368 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6369 return( SSL_CERTIFICATE_SKIP );
6370 }
6371 }
6372#endif /* MBEDTLS_SSL_SRV_C */
6373
6374 return( SSL_CERTIFICATE_EXPECTED );
6375}
6376
Hanno Becker3cf50612019-02-05 14:36:34 +00006377static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6378 int authmode,
6379 mbedtls_x509_crt *chain,
6380 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006381{
Hanno Becker613d4902019-02-05 13:11:17 +00006382 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006383 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6384 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006385 mbedtls_x509_crt *ca_chain;
6386 mbedtls_x509_crl *ca_crl;
6387
6388 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6389 return( 0 );
6390
6391#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6392 if( ssl->handshake->sni_ca_chain != NULL )
6393 {
6394 ca_chain = ssl->handshake->sni_ca_chain;
6395 ca_crl = ssl->handshake->sni_ca_crl;
6396 }
6397 else
6398#endif
6399 {
6400 ca_chain = ssl->conf->ca_chain;
6401 ca_crl = ssl->conf->ca_crl;
6402 }
6403
6404 /*
6405 * Main check: verify certificate
6406 */
6407 ret = mbedtls_x509_crt_verify_restartable(
6408 chain,
6409 ca_chain, ca_crl,
6410 ssl->conf->cert_profile,
6411 ssl->hostname,
6412 &ssl->session_negotiate->verify_result,
6413 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6414
6415 if( ret != 0 )
6416 {
6417 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6418 }
6419
6420#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6421 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6422 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6423#endif
6424
6425 /*
6426 * Secondary checks: always done, but change 'ret' only if it was 0
6427 */
6428
6429#if defined(MBEDTLS_ECP_C)
6430 {
6431 const mbedtls_pk_context *pk = &chain->pk;
6432
6433 /* If certificate uses an EC key, make sure the curve is OK */
6434 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6435 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6436 {
6437 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6438
6439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6440 if( ret == 0 )
6441 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6442 }
6443 }
6444#endif /* MBEDTLS_ECP_C */
6445
6446 if( mbedtls_ssl_check_cert_usage( chain,
6447 ciphersuite_info,
6448 ! ssl->conf->endpoint,
6449 &ssl->session_negotiate->verify_result ) != 0 )
6450 {
6451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6452 if( ret == 0 )
6453 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6454 }
6455
6456 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6457 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6458 * with details encoded in the verification flags. All other kinds
6459 * of error codes, including those from the user provided f_vrfy
6460 * functions, are treated as fatal and lead to a failure of
6461 * ssl_parse_certificate even if verification was optional. */
6462 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6463 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6464 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6465 {
6466 ret = 0;
6467 }
6468
6469 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6470 {
6471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6472 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6473 }
6474
6475 if( ret != 0 )
6476 {
6477 uint8_t alert;
6478
6479 /* The certificate may have been rejected for several reasons.
6480 Pick one and send the corresponding alert. Which alert to send
6481 may be a subject of debate in some cases. */
6482 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6483 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6484 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6485 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6486 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6487 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6488 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6489 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6490 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6491 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6492 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6493 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6494 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6495 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6496 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6497 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6498 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6499 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6500 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6501 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6502 else
6503 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6505 alert );
6506 }
6507
6508#if defined(MBEDTLS_DEBUG_C)
6509 if( ssl->session_negotiate->verify_result != 0 )
6510 {
6511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6512 ssl->session_negotiate->verify_result ) );
6513 }
6514 else
6515 {
6516 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6517 }
6518#endif /* MBEDTLS_DEBUG_C */
6519
6520 return( ret );
6521}
6522
6523int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6524{
6525 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006526 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006527#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6528 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6529 ? ssl->handshake->sni_authmode
6530 : ssl->conf->authmode;
6531#else
6532 const int authmode = ssl->conf->authmode;
6533#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006534 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006535
6536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6537
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006538 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6539 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006540 {
6541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006542 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006543 }
6544
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006545#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6546 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006547 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006548 {
6549 goto crt_verify;
6550 }
6551#endif
6552
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006553 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006554 {
6555 /* mbedtls_ssl_read_record may have sent an alert already. We
6556 let it decide whether to alert. */
6557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6558 return( ret );
6559 }
6560
Hanno Beckerb8a08572019-02-05 12:49:06 +00006561#if defined(MBEDTLS_SSL_SRV_C)
6562 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6563 {
6564 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006565
6566 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006567 ret = 0;
6568 else
6569 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006570
Hanno Becker613d4902019-02-05 13:11:17 +00006571 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006572 }
6573#endif /* MBEDTLS_SSL_SRV_C */
6574
Hanno Becker35e41772019-02-05 15:37:23 +00006575 /* Clear existing peer CRT structure in case we tried to
6576 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006577 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker35e41772019-02-05 15:37:23 +00006578 ssl->session_negotiate->peer_cert =
6579 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6580 if( ssl->session_negotiate->peer_cert == NULL )
6581 {
6582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6583 sizeof( mbedtls_x509_crt ) ) );
6584 mbedtls_ssl_send_alert_message( ssl,
6585 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6586 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6587 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6588 }
6589 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
Hanno Beckera46c2872019-02-05 13:08:01 +00006590
Hanno Becker35e41772019-02-05 15:37:23 +00006591 ret = ssl_parse_certificate_chain( ssl, ssl->session_negotiate->peer_cert );
6592 if( ret != 0 )
Hanno Beckera7c1df62019-02-05 14:35:46 +00006593 return( ret );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006594
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006595#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6596 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006597 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006598
6599crt_verify:
6600 if( ssl->handshake->ecrs_enabled)
6601 rs_ctx = &ssl->handshake->ecrs_ctx;
6602#endif
6603
Hanno Becker3cf50612019-02-05 14:36:34 +00006604 ret = ssl_parse_certificate_verify( ssl, authmode,
6605 ssl->session_negotiate->peer_cert,
6606 rs_ctx );
6607 if( ret != 0 )
6608 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611
Hanno Becker613d4902019-02-05 13:11:17 +00006612exit:
6613
6614 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 return( ret );
6616}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006617#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006620{
6621 int ret;
6622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006626 ssl->out_msglen = 1;
6627 ssl->out_msg[0] = 1;
6628
Paul Bakker5121ce52009-01-03 21:22:43 +00006629 ssl->state++;
6630
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006631 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634 return( ret );
6635 }
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006638
6639 return( 0 );
6640}
6641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006643{
6644 int ret;
6645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Hanno Becker327c93b2018-08-15 13:56:18 +01006648 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 return( ret );
6652 }
6653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006657 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6658 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 }
6661
Hanno Beckere678eaa2018-08-21 14:57:46 +01006662 /* CCS records are only accepted if they have length 1 and content '1',
6663 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006664
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006665 /*
6666 * Switch to our negotiated transform and session parameters for inbound
6667 * data.
6668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006670 ssl->transform_in = ssl->transform_negotiate;
6671 ssl->session_in = ssl->session_negotiate;
6672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006674 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006677 ssl_dtls_replay_reset( ssl );
6678#endif
6679
6680 /* Increment epoch */
6681 if( ++ssl->in_epoch == 0 )
6682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006684 /* This is highly unlikely to happen for legitimate reasons, so
6685 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006686 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006687 }
6688 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006689 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006691#if defined(MBEDTLS_SSL_PROTO_TLS)
6692 {
6693 memset( ssl->in_ctr, 0, 8 );
6694 }
6695#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006696
Hanno Beckerf5970a02019-05-08 09:38:41 +01006697 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6700 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006705 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6706 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006708 }
6709 }
6710#endif
6711
Paul Bakker5121ce52009-01-03 21:22:43 +00006712 ssl->state++;
6713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006715
6716 return( 0 );
6717}
6718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6720 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006721{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006722 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6725 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6726 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006727 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006728 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6731#if defined(MBEDTLS_SHA512_C)
6732 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006733 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6734 else
6735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006736#if defined(MBEDTLS_SHA256_C)
6737 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006738 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006739 else
6740#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006741#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006743 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006744 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006745 }
Paul Bakker380da532012-04-18 16:10:25 +00006746}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006750#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6751 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006752 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6753 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006755#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6756#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006757 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006759#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006760 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006762#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006763}
6764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006765static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006766 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006767{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006768#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6769 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006770 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6771 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006772#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6774#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006775 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006777#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006778 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006780#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006781}
6782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006783#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6784 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6785static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006786 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006787{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006788 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6789 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006790}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006791#endif
Paul Bakker380da532012-04-18 16:10:25 +00006792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006793#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6794#if defined(MBEDTLS_SHA256_C)
6795static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006796 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006797{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006798 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006799}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006800#endif
Paul Bakker380da532012-04-18 16:10:25 +00006801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802#if defined(MBEDTLS_SHA512_C)
6803static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006804 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006805{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006806 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006807}
Paul Bakker769075d2012-11-24 11:26:46 +01006808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006809#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006812static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006814{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006815 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006816 mbedtls_md5_context md5;
6817 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006818
Paul Bakker5121ce52009-01-03 21:22:43 +00006819 unsigned char padbuf[48];
6820 unsigned char md5sum[16];
6821 unsigned char sha1sum[20];
6822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006824 if( !session )
6825 session = ssl->session;
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006828
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006829 mbedtls_md5_init( &md5 );
6830 mbedtls_sha1_init( &sha1 );
6831
6832 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6833 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006834
6835 /*
6836 * SSLv3:
6837 * hash =
6838 * MD5( master + pad2 +
6839 * MD5( handshake + sender + master + pad1 ) )
6840 * + SHA1( master + pad2 +
6841 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006842 */
6843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006845 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6846 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006850 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6851 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006854 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006855 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
Paul Bakker1ef83d62012-04-11 12:09:53 +00006857 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006859 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6860 mbedtls_md5_update_ret( &md5, session->master, 48 );
6861 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6862 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006863
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006864 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6865 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6866 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6867 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006868
Paul Bakker1ef83d62012-04-11 12:09:53 +00006869 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006870
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006871 mbedtls_md5_starts_ret( &md5 );
6872 mbedtls_md5_update_ret( &md5, session->master, 48 );
6873 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6874 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6875 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006876
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006877 mbedtls_sha1_starts_ret( &sha1 );
6878 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6879 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6880 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6881 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006884
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006885 mbedtls_md5_free( &md5 );
6886 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006887
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006888 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6889 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6890 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006893}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006897static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006899{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006900 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006901 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006902 mbedtls_md5_context md5;
6903 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006904 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006906 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006907 if( !session )
6908 session = ssl->session;
6909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006911
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006912 mbedtls_md5_init( &md5 );
6913 mbedtls_sha1_init( &sha1 );
6914
6915 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6916 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006917
Paul Bakker1ef83d62012-04-11 12:09:53 +00006918 /*
6919 * TLSv1:
6920 * hash = PRF( master, finished_label,
6921 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6922 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006925 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6926 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006927#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006929#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006930 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6931 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006932#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006935 ? "client finished"
6936 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006937
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006938 mbedtls_md5_finish_ret( &md5, padbuf );
6939 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006940
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006941 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006942 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006945
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006946 mbedtls_md5_free( &md5 );
6947 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006948
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006949 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6956#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006957static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006959{
6960 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006961 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006962 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006963 unsigned char padbuf[32];
6964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006966 if( !session )
6967 session = ssl->session;
6968
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006969 mbedtls_sha256_init( &sha256 );
6970
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006972
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006973 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006974
6975 /*
6976 * TLSv1.2:
6977 * hash = PRF( master, finished_label,
6978 * Hash( handshake ) )[0.11]
6979 */
6980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006981#if !defined(MBEDTLS_SHA256_ALT)
6982 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006983 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006984#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006987 ? "client finished"
6988 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006989
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006990 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006991
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006992 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006993 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006996
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006997 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006998
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006999 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007006static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007008{
7009 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007010 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007011 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007012 unsigned char padbuf[48];
7013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007014 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007015 if( !session )
7016 session = ssl->session;
7017
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007018 mbedtls_sha512_init( &sha512 );
7019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007021
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007022 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007023
7024 /*
7025 * TLSv1.2:
7026 * hash = PRF( master, finished_label,
7027 * Hash( handshake ) )[0.11]
7028 */
7029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007030#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007031 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7032 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007033#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007036 ? "client finished"
7037 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007038
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007039 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007040
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007041 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007042 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007044 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007045
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007046 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007047
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007048 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007051}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052#endif /* MBEDTLS_SHA512_C */
7053#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007058
7059 /*
7060 * Free our handshake params
7061 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007062 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007064 ssl->handshake = NULL;
7065
7066 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007067 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007068 */
7069 if( ssl->transform )
7070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 mbedtls_ssl_transform_free( ssl->transform );
7072 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007073 }
7074 ssl->transform = ssl->transform_negotiate;
7075 ssl->transform_negotiate = NULL;
7076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007078}
7079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007080void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007081{
7082 int resume = ssl->handshake->resume;
7083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086#if defined(MBEDTLS_SSL_RENEGOTIATION)
7087 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007090 ssl->renego_records_seen = 0;
7091 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007092#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007093
7094 /*
7095 * Free the previous session and switch in the current one
7096 */
Paul Bakker0a597072012-09-25 21:55:46 +00007097 if( ssl->session )
7098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007100 /* RFC 7366 3.1: keep the EtM state */
7101 ssl->session_negotiate->encrypt_then_mac =
7102 ssl->session->encrypt_then_mac;
7103#endif
7104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 mbedtls_ssl_session_free( ssl->session );
7106 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007107 }
7108 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007109 ssl->session_negotiate = NULL;
7110
Paul Bakker0a597072012-09-25 21:55:46 +00007111 /*
7112 * Add cache entry
7113 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007114 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007115 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007116 resume == 0 )
7117 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007118 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007120 }
Paul Bakker0a597072012-09-25 21:55:46 +00007121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007123 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007124 ssl->handshake->flight != NULL )
7125 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007126 /* Cancel handshake timer */
7127 ssl_set_timer( ssl, 0 );
7128
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007129 /* Keep last flight around in case we need to resend it:
7130 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007132 }
7133 else
7134#endif
7135 ssl_handshake_wrapup_free_hs_transform( ssl );
7136
Paul Bakker48916f92012-09-16 19:57:18 +00007137 ssl->state++;
7138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007140}
7141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007143{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007144 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007147
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007148 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007149
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007150 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007151
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007152 /*
7153 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7154 * may define some other value. Currently (early 2016), no defined
7155 * ciphersuite does this (and this is unlikely to change as activity has
7156 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007161 ssl->verify_data_len = hash_len;
7162 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007163#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007164
Paul Bakker5121ce52009-01-03 21:22:43 +00007165 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7167 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007168
7169 /*
7170 * In case of session resuming, invert the client and server
7171 * ChangeCipherSpec messages order.
7172 */
Paul Bakker0a597072012-09-25 21:55:46 +00007173 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007178#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007180 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007183 }
7184 else
7185 ssl->state++;
7186
Paul Bakker48916f92012-09-16 19:57:18 +00007187 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007188 * Switch to our negotiated transform and session parameters for outbound
7189 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007194 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007195 {
7196 unsigned char i;
7197
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007198 /* Remember current epoch settings for resending */
7199 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007200 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007201
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007202 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007203 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007204
7205 /* Increment epoch */
7206 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007207 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007208 break;
7209
7210 /* The loop goes to its end iff the counter is wrapping */
7211 if( i == 0 )
7212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7214 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007215 }
7216 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007217 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007219#if defined(MBEDTLS_SSL_PROTO_TLS)
7220 {
7221 memset( ssl->cur_out_ctr, 0, 8 );
7222 }
7223#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007224
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007225 ssl->transform_out = ssl->transform_negotiate;
7226 ssl->session_out = ssl->session_negotiate;
7227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7229 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7234 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007235 }
7236 }
7237#endif
7238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007240 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007242#endif
7243
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007244 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007245 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007246 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007247 return( ret );
7248 }
7249
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007250#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007251 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007252 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7253 {
7254 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7255 return( ret );
7256 }
7257#endif
7258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
7261 return( 0 );
7262}
7263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007265#define SSL_MAX_HASH_LEN 36
7266#else
7267#define SSL_MAX_HASH_LEN 12
7268#endif
7269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007271{
Paul Bakker23986e52011-04-24 08:57:21 +00007272 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007273 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007274 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007277
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007278 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007279
Hanno Becker327c93b2018-08-15 13:56:18 +01007280 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007282 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007283 return( ret );
7284 }
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7290 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007292 }
7293
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007294 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295#if defined(MBEDTLS_SSL_PROTO_SSL3)
7296 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007297 hash_len = 36;
7298 else
7299#endif
7300 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7303 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007306 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7307 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 }
7310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007311 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007312 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007315 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7316 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007318 }
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007321 ssl->verify_data_len = hash_len;
7322 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007323#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007324
Paul Bakker0a597072012-09-25 21:55:46 +00007325 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007328 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007334#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007335 }
7336 else
7337 ssl->state++;
7338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007340 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007342#endif
7343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
7346 return( 0 );
7347}
7348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007350{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007351 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7354 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7355 mbedtls_md5_init( &handshake->fin_md5 );
7356 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007357 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7358 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007359#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7361#if defined(MBEDTLS_SHA256_C)
7362 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007363 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007365#if defined(MBEDTLS_SHA512_C)
7366 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007367 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007370
7371 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007372
7373#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7374 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7375 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7376#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_DHM_C)
7379 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007380#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#if defined(MBEDTLS_ECDH_C)
7382 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007383#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007384#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007385 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007386#if defined(MBEDTLS_SSL_CLI_C)
7387 handshake->ecjpake_cache = NULL;
7388 handshake->ecjpake_cache_len = 0;
7389#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007390#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007391
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007392#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007393 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007394#endif
7395
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007396#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7397 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7398#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007399}
7400
Hanno Becker611a83b2018-01-03 14:27:32 +00007401void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007402{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7406 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007407
Hanno Becker92231322018-01-03 15:32:51 +00007408#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 mbedtls_md_init( &transform->md_ctx_enc );
7410 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007411#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007412}
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007417}
7418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007420{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007421 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007422 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007424 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007426 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007427 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007428
7429 /*
7430 * Either the pointers are now NULL or cleared properly and can be freed.
7431 * Now allocate missing structures.
7432 */
7433 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007434 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007435 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007436 }
Paul Bakker48916f92012-09-16 19:57:18 +00007437
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007438 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007439 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007440 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007441 }
Paul Bakker48916f92012-09-16 19:57:18 +00007442
Paul Bakker82788fb2014-10-20 13:59:19 +02007443 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007444 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007445 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007446 }
Paul Bakker48916f92012-09-16 19:57:18 +00007447
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007448 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007449 if( ssl->handshake == NULL ||
7450 ssl->transform_negotiate == NULL ||
7451 ssl->session_negotiate == NULL )
7452 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 mbedtls_free( ssl->handshake );
7456 mbedtls_free( ssl->transform_negotiate );
7457 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007458
7459 ssl->handshake = NULL;
7460 ssl->transform_negotiate = NULL;
7461 ssl->session_negotiate = NULL;
7462
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007463 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007464 }
7465
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007466 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007467 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007468 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007469 ssl_handshake_params_init( ssl->handshake );
7470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007472 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007473 {
7474 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007475
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007476 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7477 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7478 else
7479 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007480
7481 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007482 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007483#endif
7484
Paul Bakker48916f92012-09-16 19:57:18 +00007485 return( 0 );
7486}
7487
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007488#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007489/* Dummy cookie callbacks for defaults */
7490static int ssl_cookie_write_dummy( void *ctx,
7491 unsigned char **p, unsigned char *end,
7492 const unsigned char *cli_id, size_t cli_id_len )
7493{
7494 ((void) ctx);
7495 ((void) p);
7496 ((void) end);
7497 ((void) cli_id);
7498 ((void) cli_id_len);
7499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007501}
7502
7503static int ssl_cookie_check_dummy( void *ctx,
7504 const unsigned char *cookie, size_t cookie_len,
7505 const unsigned char *cli_id, size_t cli_id_len )
7506{
7507 ((void) ctx);
7508 ((void) cookie);
7509 ((void) cookie_len);
7510 ((void) cli_id);
7511 ((void) cli_id_len);
7512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007514}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007515#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007516
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007517/* Once ssl->out_hdr as the address of the beginning of the
7518 * next outgoing record is set, deduce the other pointers.
7519 *
7520 * Note: For TLS, we save the implicit record sequence number
7521 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7522 * and the caller has to make sure there's space for this.
7523 */
7524
7525static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7526 mbedtls_ssl_transform *transform )
7527{
7528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007529 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007530 {
7531 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007533 ssl->out_cid = ssl->out_ctr + 8;
7534 ssl->out_len = ssl->out_cid;
7535 if( transform != NULL )
7536 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007537#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007538 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007539#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007540 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007541 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007542 MBEDTLS_SSL_TRANSPORT_ELSE
7543#endif /* MBEDTLS_SSL_PROTO_DTLS */
7544#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007545 {
7546 ssl->out_ctr = ssl->out_hdr - 8;
7547 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007549 ssl->out_cid = ssl->out_len;
7550#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007551 ssl->out_iv = ssl->out_hdr + 5;
7552 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007553#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007554
7555 /* Adjust out_msg to make space for explicit IV, if used. */
7556 if( transform != NULL &&
7557 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7558 {
7559 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7560 }
7561 else
7562 ssl->out_msg = ssl->out_iv;
7563}
7564
7565/* Once ssl->in_hdr as the address of the beginning of the
7566 * next incoming record is set, deduce the other pointers.
7567 *
7568 * Note: For TLS, we save the implicit record sequence number
7569 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7570 * and the caller has to make sure there's space for this.
7571 */
7572
Hanno Beckerf5970a02019-05-08 09:38:41 +01007573static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007574{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007575 /* This function sets the pointers to match the case
7576 * of unprotected TLS/DTLS records, with both ssl->in_iv
7577 * and ssl->in_msg pointing to the beginning of the record
7578 * content.
7579 *
7580 * When decrypting a protected record, ssl->in_msg
7581 * will be shifted to point to the beginning of the
7582 * record plaintext.
7583 */
7584
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007586 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007587 {
Hanno Becker70e79282019-05-03 14:34:53 +01007588 /* This sets the header pointers to match records
7589 * without CID. When we receive a record containing
7590 * a CID, the fields are shifted accordingly in
7591 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007592 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007593#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007594 ssl->in_cid = ssl->in_ctr + 8;
7595 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007596#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007597 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007598#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007599 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007600 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007601 MBEDTLS_SSL_TRANSPORT_ELSE
7602#endif /* MBEDTLS_SSL_PROTO_DTLS */
7603#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007604 {
7605 ssl->in_ctr = ssl->in_hdr - 8;
7606 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007607#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007608 ssl->in_cid = ssl->in_len;
7609#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007610 ssl->in_iv = ssl->in_hdr + 5;
7611 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007612#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007613
Hanno Beckerf5970a02019-05-08 09:38:41 +01007614 /* This will be adjusted at record decryption time. */
7615 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007616}
7617
Paul Bakker5121ce52009-01-03 21:22:43 +00007618/*
7619 * Initialize an SSL context
7620 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007621void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7622{
7623 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7624}
7625
7626/*
7627 * Setup an SSL context
7628 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007629
7630static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7631{
7632 /* Set the incoming and outgoing record pointers. */
7633#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007634 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007635 {
7636 ssl->out_hdr = ssl->out_buf;
7637 ssl->in_hdr = ssl->in_buf;
7638 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007639 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007640#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007641#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007642 {
7643 ssl->out_hdr = ssl->out_buf + 8;
7644 ssl->in_hdr = ssl->in_buf + 8;
7645 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007646#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007647
7648 /* Derive other internal pointers. */
7649 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007650 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007651}
7652
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007653int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007654 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007655{
Paul Bakker48916f92012-09-16 19:57:18 +00007656 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007657
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007658 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007659
7660 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007661 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007662 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007663
7664 /* Set to NULL in case of an error condition */
7665 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007666
Angus Grattond8213d02016-05-25 20:56:48 +10007667 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7668 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007669 {
Angus Grattond8213d02016-05-25 20:56:48 +10007670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007671 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007672 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007673 }
7674
7675 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7676 if( ssl->out_buf == NULL )
7677 {
7678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007679 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007680 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007681 }
7682
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007683 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007684
Paul Bakker48916f92012-09-16 19:57:18 +00007685 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007686 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007687
7688 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007689
7690error:
7691 mbedtls_free( ssl->in_buf );
7692 mbedtls_free( ssl->out_buf );
7693
7694 ssl->conf = NULL;
7695
7696 ssl->in_buf = NULL;
7697 ssl->out_buf = NULL;
7698
7699 ssl->in_hdr = NULL;
7700 ssl->in_ctr = NULL;
7701 ssl->in_len = NULL;
7702 ssl->in_iv = NULL;
7703 ssl->in_msg = NULL;
7704
7705 ssl->out_hdr = NULL;
7706 ssl->out_ctr = NULL;
7707 ssl->out_len = NULL;
7708 ssl->out_iv = NULL;
7709 ssl->out_msg = NULL;
7710
k-stachowiak9f7798e2018-07-31 16:52:32 +02007711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007712}
7713
7714/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007715 * Reset an initialized and used SSL context for re-use while retaining
7716 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007717 *
7718 * If partial is non-zero, keep data in the input buffer and client ID.
7719 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007720 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007721static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007722{
Paul Bakker48916f92012-09-16 19:57:18 +00007723 int ret;
7724
Hanno Becker7e772132018-08-10 12:38:21 +01007725#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7726 !defined(MBEDTLS_SSL_SRV_C)
7727 ((void) partial);
7728#endif
7729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007730 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007731
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007732 /* Cancel any possibly running timer */
7733 ssl_set_timer( ssl, 0 );
7734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735#if defined(MBEDTLS_SSL_RENEGOTIATION)
7736 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007737 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007738
7739 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7741 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007744
Paul Bakker7eb013f2011-10-06 12:37:39 +00007745 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007746 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007747
7748 ssl->in_msgtype = 0;
7749 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007751 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007752 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007755 ssl_dtls_replay_reset( ssl );
7756#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007757
7758 ssl->in_hslen = 0;
7759 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007760
7761 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007762
7763 ssl->out_msgtype = 0;
7764 ssl->out_msglen = 0;
7765 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7767 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007768 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007769#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007770
Hanno Becker19859472018-08-06 09:40:20 +01007771 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7772
Paul Bakker48916f92012-09-16 19:57:18 +00007773 ssl->transform_in = NULL;
7774 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007775
Hanno Becker78640902018-08-13 16:35:15 +01007776 ssl->session_in = NULL;
7777 ssl->session_out = NULL;
7778
Angus Grattond8213d02016-05-25 20:56:48 +10007779 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007780
7781#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007782 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007783#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7784 {
7785 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007786 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007787 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7790 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7793 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7796 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007797 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007798 }
7799#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007800
Paul Bakker48916f92012-09-16 19:57:18 +00007801 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 mbedtls_ssl_transform_free( ssl->transform );
7804 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007805 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007806 }
Paul Bakker48916f92012-09-16 19:57:18 +00007807
Paul Bakkerc0463502013-02-14 11:19:38 +01007808 if( ssl->session )
7809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810 mbedtls_ssl_session_free( ssl->session );
7811 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007812 ssl->session = NULL;
7813 }
7814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007816 ssl->alpn_chosen = NULL;
7817#endif
7818
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007819#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007820#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007821 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007822#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007823 {
7824 mbedtls_free( ssl->cli_id );
7825 ssl->cli_id = NULL;
7826 ssl->cli_id_len = 0;
7827 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007828#endif
7829
Paul Bakker48916f92012-09-16 19:57:18 +00007830 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7831 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007832
7833 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007834}
7835
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007836/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007837 * Reset an initialized and used SSL context for re-use while retaining
7838 * all application-set variables, function pointers and data.
7839 */
7840int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7841{
7842 return( ssl_session_reset_int( ssl, 0 ) );
7843}
7844
7845/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 * SSL set accessors
7847 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007848void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007850 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007851}
7852
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007853void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007854{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007855 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007856}
7857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007859void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007860{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007861 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007862}
7863#endif
7864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007866void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007867{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007868 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007869}
7870#endif
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007873
Hanno Becker1841b0a2018-08-24 11:13:57 +01007874void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7875 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007876{
7877 ssl->disable_datagram_packing = !allow_packing;
7878}
7879
7880void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7881 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007882{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007883 conf->hs_timeout_min = min;
7884 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007885}
7886#endif
7887
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007888void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007889{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007890 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007891}
7892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007894void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007895 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007896 void *p_vrfy )
7897{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007898 conf->f_vrfy = f_vrfy;
7899 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007900}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007902
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007903void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007904 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007905 void *p_rng )
7906{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007907 conf->f_rng = f_rng;
7908 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007909}
7910
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007911void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007912 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007913 void *p_dbg )
7914{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007915 conf->f_dbg = f_dbg;
7916 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007917}
7918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007920 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007921 mbedtls_ssl_send_t *f_send,
7922 mbedtls_ssl_recv_t *f_recv,
7923 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007924{
7925 ssl->p_bio = p_bio;
7926 ssl->f_send = f_send;
7927 ssl->f_recv = f_recv;
7928 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007929}
7930
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007931#if defined(MBEDTLS_SSL_PROTO_DTLS)
7932void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7933{
7934 ssl->mtu = mtu;
7935}
7936#endif
7937
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007938void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007939{
7940 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007941}
7942
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007943void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7944 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007945 mbedtls_ssl_set_timer_t *f_set_timer,
7946 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007947{
7948 ssl->p_timer = p_timer;
7949 ssl->f_set_timer = f_set_timer;
7950 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007951
7952 /* Make sure we start with no timer running */
7953 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007957void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007958 void *p_cache,
7959 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7960 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007961{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007962 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007963 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007964 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968#if defined(MBEDTLS_SSL_CLI_C)
7969int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007970{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007971 int ret;
7972
7973 if( ssl == NULL ||
7974 session == NULL ||
7975 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007976 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007979 }
7980
Hanno Becker58fccf22019-02-06 14:30:46 +00007981 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
7982 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007983 return( ret );
7984
Paul Bakker0a597072012-09-25 21:55:46 +00007985 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007986
7987 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007990
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007991void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007992 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007993{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007994 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7995 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7996 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7997 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007998}
7999
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008000void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008001 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008002 int major, int minor )
8003{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008005 return;
8006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008008 return;
8009
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008010 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008011}
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008014void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008015 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008016{
8017 conf->cert_profile = profile;
8018}
8019
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008020/* Append a new keycert entry to a (possibly empty) list */
8021static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8022 mbedtls_x509_crt *cert,
8023 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008024{
niisato8ee24222018-06-25 19:05:48 +09008025 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008026
niisato8ee24222018-06-25 19:05:48 +09008027 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8028 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008029 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008030
niisato8ee24222018-06-25 19:05:48 +09008031 new_cert->cert = cert;
8032 new_cert->key = key;
8033 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008034
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008035 /* Update head is the list was null, else add to the end */
8036 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008037 {
niisato8ee24222018-06-25 19:05:48 +09008038 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008039 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008040 else
8041 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008042 mbedtls_ssl_key_cert *cur = *head;
8043 while( cur->next != NULL )
8044 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008045 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008046 }
8047
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008048 return( 0 );
8049}
8050
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008051int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008052 mbedtls_x509_crt *own_cert,
8053 mbedtls_pk_context *pk_key )
8054{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008055 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008056}
8057
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008058void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008059 mbedtls_x509_crt *ca_chain,
8060 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008061{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008062 conf->ca_chain = ca_chain;
8063 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008066
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008067#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8068int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8069 mbedtls_x509_crt *own_cert,
8070 mbedtls_pk_context *pk_key )
8071{
8072 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8073 own_cert, pk_key ) );
8074}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008075
8076void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8077 mbedtls_x509_crt *ca_chain,
8078 mbedtls_x509_crl *ca_crl )
8079{
8080 ssl->handshake->sni_ca_chain = ca_chain;
8081 ssl->handshake->sni_ca_crl = ca_crl;
8082}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008083
8084void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8085 int authmode )
8086{
8087 ssl->handshake->sni_authmode = authmode;
8088}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008089#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8090
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008091#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008092/*
8093 * Set EC J-PAKE password for current handshake
8094 */
8095int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8096 const unsigned char *pw,
8097 size_t pw_len )
8098{
8099 mbedtls_ecjpake_role role;
8100
Janos Follath8eb64132016-06-03 15:40:57 +01008101 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008102 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8103
8104 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8105 role = MBEDTLS_ECJPAKE_SERVER;
8106 else
8107 role = MBEDTLS_ECJPAKE_CLIENT;
8108
8109 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8110 role,
8111 MBEDTLS_MD_SHA256,
8112 MBEDTLS_ECP_DP_SECP256R1,
8113 pw, pw_len ) );
8114}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008115#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008117#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008118int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008119 const unsigned char *psk, size_t psk_len,
8120 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008121{
Paul Bakker6db455e2013-09-18 17:29:31 +02008122 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008127
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008128 /* Identity len will be encoded on two bytes */
8129 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008130 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008131 {
8132 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8133 }
8134
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008135 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008136 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008137 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008138
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008139 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008140 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008141 conf->psk_len = 0;
8142 }
8143 if( conf->psk_identity != NULL )
8144 {
8145 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008146 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008147 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008148 }
8149
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008150 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8151 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008152 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008153 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008154 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008155 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008156 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008157 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008158 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008159
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008160 conf->psk_len = psk_len;
8161 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008162
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008163 memcpy( conf->psk, psk, conf->psk_len );
8164 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008165
8166 return( 0 );
8167}
8168
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008169int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8170 const unsigned char *psk, size_t psk_len )
8171{
8172 if( psk == NULL || ssl->handshake == NULL )
8173 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8174
8175 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8177
8178 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008179 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008180 mbedtls_platform_zeroize( ssl->handshake->psk,
8181 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008182 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008183 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008184 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008185
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008186 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008187 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008188
8189 ssl->handshake->psk_len = psk_len;
8190 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8191
8192 return( 0 );
8193}
8194
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008195void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008197 size_t),
8198 void *p_psk )
8199{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008200 conf->f_psk = f_psk;
8201 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008204
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008205#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008206
8207#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008208int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008209{
8210 int ret;
8211
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008212 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8213 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8214 {
8215 mbedtls_mpi_free( &conf->dhm_P );
8216 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008217 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008218 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008219
8220 return( 0 );
8221}
Hanno Becker470a8c42017-10-04 15:28:46 +01008222#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008223
Hanno Beckera90658f2017-10-04 15:29:08 +01008224int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8225 const unsigned char *dhm_P, size_t P_len,
8226 const unsigned char *dhm_G, size_t G_len )
8227{
8228 int ret;
8229
8230 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8231 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8232 {
8233 mbedtls_mpi_free( &conf->dhm_P );
8234 mbedtls_mpi_free( &conf->dhm_G );
8235 return( ret );
8236 }
8237
8238 return( 0 );
8239}
Paul Bakker5121ce52009-01-03 21:22:43 +00008240
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008241int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008242{
8243 int ret;
8244
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008245 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8246 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8247 {
8248 mbedtls_mpi_free( &conf->dhm_P );
8249 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008250 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008251 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008252
8253 return( 0 );
8254}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008255#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008256
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008257#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8258/*
8259 * Set the minimum length for Diffie-Hellman parameters
8260 */
8261void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8262 unsigned int bitlen )
8263{
8264 conf->dhm_min_bitlen = bitlen;
8265}
8266#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8267
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008268#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008269/*
8270 * Set allowed/preferred hashes for handshake signatures
8271 */
8272void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8273 const int *hashes )
8274{
8275 conf->sig_hashes = hashes;
8276}
Hanno Becker947194e2017-04-07 13:25:49 +01008277#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008278
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008279#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008280/*
8281 * Set the allowed elliptic curves
8282 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008283void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008284 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008285{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008286 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008287}
Hanno Becker947194e2017-04-07 13:25:49 +01008288#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008289
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008290#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008292{
Hanno Becker947194e2017-04-07 13:25:49 +01008293 /* Initialize to suppress unnecessary compiler warning */
8294 size_t hostname_len = 0;
8295
8296 /* Check if new hostname is valid before
8297 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008298 if( hostname != NULL )
8299 {
8300 hostname_len = strlen( hostname );
8301
8302 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8304 }
8305
8306 /* Now it's clear that we will overwrite the old hostname,
8307 * so we can free it safely */
8308
8309 if( ssl->hostname != NULL )
8310 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008311 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008312 mbedtls_free( ssl->hostname );
8313 }
8314
8315 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008316
Paul Bakker5121ce52009-01-03 21:22:43 +00008317 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008318 {
8319 ssl->hostname = NULL;
8320 }
8321 else
8322 {
8323 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008324 if( ssl->hostname == NULL )
8325 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008326
Hanno Becker947194e2017-04-07 13:25:49 +01008327 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008328
Hanno Becker947194e2017-04-07 13:25:49 +01008329 ssl->hostname[hostname_len] = '\0';
8330 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008331
8332 return( 0 );
8333}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008334#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008335
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008336#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008337void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008339 const unsigned char *, size_t),
8340 void *p_sni )
8341{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008342 conf->f_sni = f_sni;
8343 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008344}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008348int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008349{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008350 size_t cur_len, tot_len;
8351 const char **p;
8352
8353 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008354 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8355 * MUST NOT be truncated."
8356 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008357 */
8358 tot_len = 0;
8359 for( p = protos; *p != NULL; p++ )
8360 {
8361 cur_len = strlen( *p );
8362 tot_len += cur_len;
8363
8364 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008366 }
8367
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008368 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008369
8370 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008371}
8372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008374{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008375 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008376}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008377#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008378
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008379void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008380{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008381 conf->max_major_ver = major;
8382 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008383}
8384
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008385void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008386{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008387 conf->min_major_ver = major;
8388 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008389}
8390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008391#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008392void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008393{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008394 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008395}
8396#endif
8397
Janos Follath088ce432017-04-10 12:42:31 +01008398#if defined(MBEDTLS_SSL_SRV_C)
8399void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8400 char cert_req_ca_list )
8401{
8402 conf->cert_req_ca_list = cert_req_ca_list;
8403}
8404#endif
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008407void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008410}
8411#endif
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008415{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008417}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008418
8419void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008420 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008421{
8422 conf->enforce_extended_master_secret = ems_enf;
8423}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008424#endif
8425
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008426#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008427void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008428{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008429 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008430}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008431#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008433#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008434int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008436 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008437 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008440 }
8441
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008442 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008443
8444 return( 0 );
8445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008448#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008449void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008451 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008456void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008457{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008458 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008459}
8460#endif
8461
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008462void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008463{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008464 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008465}
8466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008468void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008469{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008470 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008471}
8472
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008473void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008474{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008475 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008476}
8477
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008478void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008479 const unsigned char period[8] )
8480{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008481 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008485#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008486#if defined(MBEDTLS_SSL_CLI_C)
8487void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008488{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008489 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008490}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008491#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008492
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008493#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008494void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8495 mbedtls_ssl_ticket_write_t *f_ticket_write,
8496 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8497 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008498{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008499 conf->f_ticket_write = f_ticket_write;
8500 conf->f_ticket_parse = f_ticket_parse;
8501 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008502}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008503#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008505
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008506#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8507void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8508 mbedtls_ssl_export_keys_t *f_export_keys,
8509 void *p_export_keys )
8510{
8511 conf->f_export_keys = f_export_keys;
8512 conf->p_export_keys = p_export_keys;
8513}
8514#endif
8515
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008516#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008517void mbedtls_ssl_conf_async_private_cb(
8518 mbedtls_ssl_config *conf,
8519 mbedtls_ssl_async_sign_t *f_async_sign,
8520 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8521 mbedtls_ssl_async_resume_t *f_async_resume,
8522 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008523 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008524{
8525 conf->f_async_sign_start = f_async_sign;
8526 conf->f_async_decrypt_start = f_async_decrypt;
8527 conf->f_async_resume = f_async_resume;
8528 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008529 conf->p_async_config_data = async_config_data;
8530}
8531
Gilles Peskine8f97af72018-04-26 11:46:10 +02008532void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8533{
8534 return( conf->p_async_config_data );
8535}
8536
Gilles Peskine1febfef2018-04-30 11:54:39 +02008537void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008538{
8539 if( ssl->handshake == NULL )
8540 return( NULL );
8541 else
8542 return( ssl->handshake->user_async_ctx );
8543}
8544
Gilles Peskine1febfef2018-04-30 11:54:39 +02008545void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008546 void *ctx )
8547{
8548 if( ssl->handshake != NULL )
8549 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008550}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008551#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008552
Paul Bakker5121ce52009-01-03 21:22:43 +00008553/*
8554 * SSL get accessors
8555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008557{
8558 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8559}
8560
Hanno Becker8b170a02017-10-10 11:51:19 +01008561int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8562{
8563 /*
8564 * Case A: We're currently holding back
8565 * a message for further processing.
8566 */
8567
8568 if( ssl->keep_current_message == 1 )
8569 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008571 return( 1 );
8572 }
8573
8574 /*
8575 * Case B: Further records are pending in the current datagram.
8576 */
8577
8578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008579 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008580 ssl->in_left > ssl->next_record_offset )
8581 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008582 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008583 return( 1 );
8584 }
8585#endif /* MBEDTLS_SSL_PROTO_DTLS */
8586
8587 /*
8588 * Case C: A handshake message is being processed.
8589 */
8590
Hanno Becker8b170a02017-10-10 11:51:19 +01008591 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8592 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008594 return( 1 );
8595 }
8596
8597 /*
8598 * Case D: An application data message is being processed
8599 */
8600 if( ssl->in_offt != NULL )
8601 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008603 return( 1 );
8604 }
8605
8606 /*
8607 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008608 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008609 * we implement support for multiple alerts in single records.
8610 */
8611
8612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8613 return( 0 );
8614}
8615
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008616uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008617{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008618 if( ssl->session != NULL )
8619 return( ssl->session->verify_result );
8620
8621 if( ssl->session_negotiate != NULL )
8622 return( ssl->session_negotiate->verify_result );
8623
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008624 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008625}
8626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008628{
Paul Bakker926c8e42013-03-06 10:23:34 +01008629 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008630 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008633}
8634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008636{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008638 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008639 {
8640 switch( ssl->minor_ver )
8641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008642 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008643 return( "DTLSv1.0" );
8644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008645 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008646 return( "DTLSv1.2" );
8647
8648 default:
8649 return( "unknown (DTLS)" );
8650 }
8651 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008652 MBEDTLS_SSL_TRANSPORT_ELSE
8653#endif /* MBEDTLS_SSL_PROTO_DTLS */
8654#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008655 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008656 switch( ssl->minor_ver )
8657 {
8658 case MBEDTLS_SSL_MINOR_VERSION_0:
8659 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008660
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008661 case MBEDTLS_SSL_MINOR_VERSION_1:
8662 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008663
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008664 case MBEDTLS_SSL_MINOR_VERSION_2:
8665 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008666
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008667 case MBEDTLS_SSL_MINOR_VERSION_3:
8668 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008669
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008670 default:
8671 return( "unknown" );
8672 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008673 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008674#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008675}
8676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008678{
Hanno Becker3136ede2018-08-17 15:28:19 +01008679 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008681 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008682
Hanno Becker43395762019-05-03 14:46:38 +01008683 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8684
Hanno Becker78640902018-08-13 16:35:15 +01008685 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008686 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008688#if defined(MBEDTLS_ZLIB_SUPPORT)
8689 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8690 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008691#endif
8692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008693 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 case MBEDTLS_MODE_GCM:
8696 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008697 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008699 transform_expansion = transform->minlen;
8700 break;
8701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008703
8704 block_size = mbedtls_cipher_get_block_size(
8705 &transform->cipher_ctx_enc );
8706
Hanno Becker3136ede2018-08-17 15:28:19 +01008707 /* Expansion due to the addition of the MAC. */
8708 transform_expansion += transform->maclen;
8709
8710 /* Expansion due to the addition of CBC padding;
8711 * Theoretically up to 256 bytes, but we never use
8712 * more than the block size of the underlying cipher. */
8713 transform_expansion += block_size;
8714
8715 /* For TLS 1.1 or higher, an explicit IV is added
8716 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008717#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8718 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008719 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008720#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008721
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008722 break;
8723
8724 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008726 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008727 }
8728
Hanno Beckera5a2b082019-05-15 14:03:01 +01008729#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008730 if( transform->out_cid_len != 0 )
8731 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008732#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008733
Hanno Becker43395762019-05-03 14:46:38 +01008734 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008735}
8736
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008737#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8738size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8739{
8740 size_t max_len;
8741
8742 /*
8743 * Assume mfl_code is correct since it was checked when set
8744 */
Angus Grattond8213d02016-05-25 20:56:48 +10008745 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008746
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008747 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008748 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008749 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008750 {
Angus Grattond8213d02016-05-25 20:56:48 +10008751 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008752 }
8753
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008754 /* During a handshake, use the value being negotiated */
8755 if( ssl->session_negotiate != NULL &&
8756 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8757 {
8758 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8759 }
8760
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008761 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008762}
8763#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8764
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008765#if defined(MBEDTLS_SSL_PROTO_DTLS)
8766static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8767{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008768 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8769 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8770 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8771 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8772 return ( 0 );
8773
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008774 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8775 return( ssl->mtu );
8776
8777 if( ssl->mtu == 0 )
8778 return( ssl->handshake->mtu );
8779
8780 return( ssl->mtu < ssl->handshake->mtu ?
8781 ssl->mtu : ssl->handshake->mtu );
8782}
8783#endif /* MBEDTLS_SSL_PROTO_DTLS */
8784
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008785int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8786{
8787 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8788
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008789#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8790 !defined(MBEDTLS_SSL_PROTO_DTLS)
8791 (void) ssl;
8792#endif
8793
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008794#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8795 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8796
8797 if( max_len > mfl )
8798 max_len = mfl;
8799#endif
8800
8801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008802 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008803 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008804 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008805 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8806 const size_t overhead = (size_t) ret;
8807
8808 if( ret < 0 )
8809 return( ret );
8810
8811 if( mtu <= overhead )
8812 {
8813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8814 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8815 }
8816
8817 if( max_len > mtu - overhead )
8818 max_len = mtu - overhead;
8819 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008821
Hanno Becker0defedb2018-08-10 12:35:02 +01008822#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8823 !defined(MBEDTLS_SSL_PROTO_DTLS)
8824 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008825#endif
8826
8827 return( (int) max_len );
8828}
8829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008830#if defined(MBEDTLS_X509_CRT_PARSE_C)
8831const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008832{
8833 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008834 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008835
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008836 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008840#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008841int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8842 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008843{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008844 if( ssl == NULL ||
8845 dst == NULL ||
8846 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008847 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008850 }
8851
Hanno Becker58fccf22019-02-06 14:30:46 +00008852 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008855
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008856const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8857{
8858 if( ssl == NULL )
8859 return( NULL );
8860
8861 return( ssl->session );
8862}
8863
Paul Bakker5121ce52009-01-03 21:22:43 +00008864/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008865 * Define ticket header determining Mbed TLS version
8866 * and structure of the ticket.
8867 */
8868
Hanno Becker41527622019-05-16 12:50:45 +01008869/*
Hanno Becker26829e92019-05-28 14:30:45 +01008870 * Define bitflag determining compile-time settings influencing
8871 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008872 */
8873
Hanno Becker26829e92019-05-28 14:30:45 +01008874#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008875#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008876#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008877#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008878#endif /* MBEDTLS_HAVE_TIME */
8879
8880#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008881#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008882#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008883#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008884#endif /* MBEDTLS_X509_CRT_PARSE_C */
8885
8886#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008887#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008888#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008889#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008890#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8891
8892#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008893#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008894#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008895#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008896#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8897
8898#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008899#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008900#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008901#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008902#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8903
8904#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008905#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008906#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008907#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008908#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8909
Hanno Becker41527622019-05-16 12:50:45 +01008910#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8911#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8912#else
8913#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8914#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8915
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008916#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8917#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8918#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8919#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8920#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8921#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8922#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008923
Hanno Becker26829e92019-05-28 14:30:45 +01008924#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008925 ( (uint16_t) ( \
8926 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8927 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8928 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8929 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8930 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8931 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008932 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008933
Hanno Becker557fe9f2019-05-16 12:41:07 +01008934static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008935 MBEDTLS_VERSION_MAJOR,
8936 MBEDTLS_VERSION_MINOR,
8937 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008938 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8939 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008940};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008941
8942/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008943 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008944 * (in the presentation language of TLS, RFC 8446 section 3)
8945 *
Hanno Becker26829e92019-05-28 14:30:45 +01008946 * opaque mbedtls_version[3]; // major, minor, patch
8947 * opaque session_format[2]; // version-specific 16-bit field determining
8948 * // the format of the remaining
8949 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008950 *
8951 * Note: When updating the format, remember to keep
8952 * these version+format bytes.
8953 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008954 * // In this version, `session_format` determines
8955 * // the setting of those compile-time
8956 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008957 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008958 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008959 * uint8 ciphersuite[2]; // defined by the standard
8960 * uint8 compression; // 0 or 1
8961 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008962 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008963 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008964 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008965 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8966 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008967 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008968 * uint8 mfl_code; // up to 255 according to standard
8969 * uint8 trunc_hmac; // 0 or 1
8970 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008971 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008972 * The order is the same as in the definition of the structure, except
8973 * verify_result is put before peer_cert so that all mandatory fields come
8974 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008975 */
8976int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8977 unsigned char *buf,
8978 size_t buf_len,
8979 size_t *olen )
8980{
8981 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008982 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008983#if defined(MBEDTLS_HAVE_TIME)
8984 uint64_t start;
8985#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008986#if defined(MBEDTLS_X509_CRT_PARSE_C)
8987 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008988#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008989
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008990 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008991 * Add version identifier
8992 */
8993
8994 used += sizeof( ssl_serialized_session_header );
8995
8996 if( used <= buf_len )
8997 {
8998 memcpy( p, ssl_serialized_session_header,
8999 sizeof( ssl_serialized_session_header ) );
9000 p += sizeof( ssl_serialized_session_header );
9001 }
9002
9003 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009004 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009005 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009006#if defined(MBEDTLS_HAVE_TIME)
9007 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009008
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009009 if( used <= buf_len )
9010 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009011 start = (uint64_t) session->start;
9012
9013 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9014 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9015 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9016 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9017 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9018 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9019 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9020 *p++ = (unsigned char)( ( start ) & 0xFF );
9021 }
9022#endif /* MBEDTLS_HAVE_TIME */
9023
9024 /*
9025 * Basic mandatory fields
9026 */
9027 used += 2 /* ciphersuite */
9028 + 1 /* compression */
9029 + 1 /* id_len */
9030 + sizeof( session->id )
9031 + sizeof( session->master )
9032 + 4; /* verify_result */
9033
9034 if( used <= buf_len )
9035 {
9036 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9037 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9038
9039 *p++ = (unsigned char)( session->compression & 0xFF );
9040
9041 *p++ = (unsigned char)( session->id_len & 0xFF );
9042 memcpy( p, session->id, 32 );
9043 p += 32;
9044
9045 memcpy( p, session->master, 48 );
9046 p += 48;
9047
9048 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9049 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9050 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9051 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009052 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009053
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009054 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009055 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009056 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009057#if defined(MBEDTLS_X509_CRT_PARSE_C)
9058 if( session->peer_cert == NULL )
9059 cert_len = 0;
9060 else
9061 cert_len = session->peer_cert->raw.len;
9062
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009063 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009064
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009065 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009066 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009067 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9068 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9069 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9070
9071 if( session->peer_cert != NULL )
9072 {
9073 memcpy( p, session->peer_cert->raw.p, cert_len );
9074 p += cert_len;
9075 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009076 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009077#endif /* MBEDTLS_X509_CRT_PARSE_C */
9078
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009079 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009080 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009081 */
9082#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009083 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009084
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009085 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009086 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009087 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9088 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9089 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9090
9091 if( session->ticket != NULL )
9092 {
9093 memcpy( p, session->ticket, session->ticket_len );
9094 p += session->ticket_len;
9095 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009096
9097 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9098 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9099 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9100 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009101 }
9102#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9103
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009104 /*
9105 * Misc extension-related info
9106 */
9107#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9108 used += 1;
9109
9110 if( used <= buf_len )
9111 *p++ = session->mfl_code;
9112#endif
9113
9114#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9115 used += 1;
9116
9117 if( used <= buf_len )
9118 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9119#endif
9120
9121#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9122 used += 1;
9123
9124 if( used <= buf_len )
9125 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9126#endif
9127
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009128 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009129 *olen = used;
9130
9131 if( used > buf_len )
9132 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009133
9134 return( 0 );
9135}
9136
9137/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009138 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009139 *
9140 * This internal version is wrapped by a public function that cleans up in
9141 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009142 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009143static int ssl_session_load( mbedtls_ssl_session *session,
9144 const unsigned char *buf,
9145 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009146{
9147 const unsigned char *p = buf;
9148 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009149#if defined(MBEDTLS_HAVE_TIME)
9150 uint64_t start;
9151#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009152#if defined(MBEDTLS_X509_CRT_PARSE_C)
9153 size_t cert_len;
9154#endif /* MBEDTLS_X509_CRT_PARSE_C */
9155
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009156 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009157 * Check version identifier
9158 */
9159
9160 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009162
9163 if( memcmp( p, ssl_serialized_session_header,
9164 sizeof( ssl_serialized_session_header ) ) != 0 )
9165 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009166 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009167 }
9168 p += sizeof( ssl_serialized_session_header );
9169
9170 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009171 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009172 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009173#if defined(MBEDTLS_HAVE_TIME)
9174 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9176
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009177 start = ( (uint64_t) p[0] << 56 ) |
9178 ( (uint64_t) p[1] << 48 ) |
9179 ( (uint64_t) p[2] << 40 ) |
9180 ( (uint64_t) p[3] << 32 ) |
9181 ( (uint64_t) p[4] << 24 ) |
9182 ( (uint64_t) p[5] << 16 ) |
9183 ( (uint64_t) p[6] << 8 ) |
9184 ( (uint64_t) p[7] );
9185 p += 8;
9186
9187 session->start = (time_t) start;
9188#endif /* MBEDTLS_HAVE_TIME */
9189
9190 /*
9191 * Basic mandatory fields
9192 */
9193 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9195
9196 session->ciphersuite = ( p[0] << 8 ) | p[1];
9197 p += 2;
9198
9199 session->compression = *p++;
9200
9201 session->id_len = *p++;
9202 memcpy( session->id, p, 32 );
9203 p += 32;
9204
9205 memcpy( session->master, p, 48 );
9206 p += 48;
9207
9208 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9209 ( (uint32_t) p[1] << 16 ) |
9210 ( (uint32_t) p[2] << 8 ) |
9211 ( (uint32_t) p[3] );
9212 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009213
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009214 /* Immediately clear invalid pointer values that have been read, in case
9215 * we exit early before we replaced them with valid ones. */
9216#if defined(MBEDTLS_X509_CRT_PARSE_C)
9217 session->peer_cert = NULL;
9218#endif
9219#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9220 session->ticket = NULL;
9221#endif
9222
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009223 /*
9224 * Peer certificate
9225 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009226#if defined(MBEDTLS_X509_CRT_PARSE_C)
9227 if( 3 > (size_t)( end - p ) )
9228 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9229
9230 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9231 p += 3;
9232
9233 if( cert_len == 0 )
9234 {
9235 session->peer_cert = NULL;
9236 }
9237 else
9238 {
9239 int ret;
9240
9241 if( cert_len > (size_t)( end - p ) )
9242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9243
9244 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9245
9246 if( session->peer_cert == NULL )
9247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9248
9249 mbedtls_x509_crt_init( session->peer_cert );
9250
9251 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9252 p, cert_len ) ) != 0 )
9253 {
9254 mbedtls_x509_crt_free( session->peer_cert );
9255 mbedtls_free( session->peer_cert );
9256 session->peer_cert = NULL;
9257 return( ret );
9258 }
9259
9260 p += cert_len;
9261 }
9262#endif /* MBEDTLS_X509_CRT_PARSE_C */
9263
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009264 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009265 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009266 */
9267#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9268 if( 3 > (size_t)( end - p ) )
9269 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9270
9271 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9272 p += 3;
9273
9274 if( session->ticket_len != 0 )
9275 {
9276 if( session->ticket_len > (size_t)( end - p ) )
9277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9278
9279 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9280 if( session->ticket == NULL )
9281 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9282
9283 memcpy( session->ticket, p, session->ticket_len );
9284 p += session->ticket_len;
9285 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009286
9287 if( 4 > (size_t)( end - p ) )
9288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9289
9290 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9291 ( (uint32_t) p[1] << 16 ) |
9292 ( (uint32_t) p[2] << 8 ) |
9293 ( (uint32_t) p[3] );
9294 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009295#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9296
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009297 /*
9298 * Misc extension-related info
9299 */
9300#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9301 if( 1 > (size_t)( end - p ) )
9302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9303
9304 session->mfl_code = *p++;
9305#endif
9306
9307#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9308 if( 1 > (size_t)( end - p ) )
9309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9310
9311 session->trunc_hmac = *p++;
9312#endif
9313
9314#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9315 if( 1 > (size_t)( end - p ) )
9316 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9317
9318 session->encrypt_then_mac = *p++;
9319#endif
9320
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009321 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009322 if( p != end )
9323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9324
9325 return( 0 );
9326}
9327
9328/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009329 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009330 */
9331int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9332 const unsigned char *buf,
9333 size_t len )
9334{
9335 int ret = ssl_session_load( session, buf, len );
9336
9337 if( ret != 0 )
9338 mbedtls_ssl_session_free( session );
9339
9340 return( ret );
9341}
9342
9343/*
Paul Bakker1961b702013-01-25 14:49:24 +01009344 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009349
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009350 if( ssl == NULL || ssl->conf == NULL )
9351 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009354 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009356#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009360#endif
9361
Paul Bakker1961b702013-01-25 14:49:24 +01009362 return( ret );
9363}
9364
9365/*
9366 * Perform the SSL handshake
9367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009368int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009369{
9370 int ret = 0;
9371
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009372 if( ssl == NULL || ssl->conf == NULL )
9373 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009379 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009380
9381 if( ret != 0 )
9382 break;
9383 }
9384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009386
9387 return( ret );
9388}
9389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390#if defined(MBEDTLS_SSL_RENEGOTIATION)
9391#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009392/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009393 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009396{
9397 int ret;
9398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009400
9401 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009402 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9403 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009404
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009405 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009406 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009407 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009408 return( ret );
9409 }
9410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009412
9413 return( 0 );
9414}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009415#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009416
9417/*
9418 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419 * - any side: calling mbedtls_ssl_renegotiate(),
9420 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9421 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009422 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009423 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009427{
9428 int ret;
9429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009431
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009432 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9433 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009434
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009435 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9436 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009437#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009438 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009440 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009441 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009442 ssl->handshake->out_msg_seq = 1;
9443 else
9444 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009445 }
9446#endif
9447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9449 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009451 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009454 return( ret );
9455 }
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
9459 return( 0 );
9460}
9461
9462/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009463 * Renegotiate current connection on client,
9464 * or request renegotiation on server
9465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009469
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009470 if( ssl == NULL || ssl->conf == NULL )
9471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009474 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009475 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9478 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009481
9482 /* Did we already try/start sending HelloRequest? */
9483 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009485
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009486 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009488#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009491 /*
9492 * On client, either start the renegotiation process or,
9493 * if already in progress, continue the handshake
9494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009499
9500 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009503 return( ret );
9504 }
9505 }
9506 else
9507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009511 return( ret );
9512 }
9513 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009515
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009516 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009517}
9518
9519/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009520 * Check record counters and renegotiate if they're above the limit.
9521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009523{
Andres AG2196c7f2016-12-15 17:01:16 +00009524 size_t ep_len = ssl_ep_len( ssl );
9525 int in_ctr_cmp;
9526 int out_ctr_cmp;
9527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9529 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009530 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009531 {
9532 return( 0 );
9533 }
9534
Andres AG2196c7f2016-12-15 17:01:16 +00009535 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9536 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009537 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009538 ssl->conf->renego_period + ep_len, 8 - ep_len );
9539
9540 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009541 {
9542 return( 0 );
9543 }
9544
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009547}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009549
9550/*
9551 * Receive application data decrypted from the SSL layer
9552 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009554{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009555 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009556 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009557
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009558 if( ssl == NULL || ssl->conf == NULL )
9559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009564 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009567 return( ret );
9568
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009569 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009571 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009572 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009573 return( ret );
9574 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009575 }
9576#endif
9577
Hanno Becker4a810fb2017-05-24 16:27:30 +01009578 /*
9579 * Check if renegotiation is necessary and/or handshake is
9580 * in process. If yes, perform/continue, and fall through
9581 * if an unexpected packet is received while the client
9582 * is waiting for the ServerHello.
9583 *
9584 * (There is no equivalent to the last condition on
9585 * the server-side as it is not treated as within
9586 * a handshake while waiting for the ClientHello
9587 * after a renegotiation request.)
9588 */
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009591 ret = ssl_check_ctr_renegotiate( ssl );
9592 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9593 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009596 return( ret );
9597 }
9598#endif
9599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009603 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9604 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009607 return( ret );
9608 }
9609 }
9610
Hanno Beckere41158b2017-10-23 13:30:32 +01009611 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009612 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009613 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009614 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009615 if( ssl->f_get_timer != NULL &&
9616 ssl->f_get_timer( ssl->p_timer ) == -1 )
9617 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009618 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009619 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009620
Hanno Becker327c93b2018-08-15 13:56:18 +01009621 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009622 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009623 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9624 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009625
Hanno Becker4a810fb2017-05-24 16:27:30 +01009626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9627 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 }
9629
9630 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009632 {
9633 /*
9634 * OpenSSL sends empty messages to randomize the IV
9635 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009636 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009639 return( 0 );
9640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 return( ret );
9643 }
9644 }
9645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009649
Hanno Becker4a810fb2017-05-24 16:27:30 +01009650 /*
9651 * - For client-side, expect SERVER_HELLO_REQUEST.
9652 * - For server-side, expect CLIENT_HELLO.
9653 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9654 */
9655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009657 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009659 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009662
9663 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009665 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009666 {
9667 continue;
9668 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009669 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009670#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009671#if defined(MBEDTLS_SSL_PROTO_TLS)
9672 {
9673 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9674 }
9675#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009676 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009677#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009678
Hanno Becker4a810fb2017-05-24 16:27:30 +01009679#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009684
9685 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009687 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009688 {
9689 continue;
9690 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009691 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009692#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009693#if defined(MBEDTLS_SSL_PROTO_TLS)
9694 {
9695 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9696 }
9697#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009698 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009699#endif /* MBEDTLS_SSL_SRV_C */
9700
Hanno Becker21df7f92017-10-17 11:03:26 +01009701#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009702 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009703 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9704 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9705 ssl->conf->allow_legacy_renegotiation ==
9706 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9707 {
9708 /*
9709 * Accept renegotiation request
9710 */
Paul Bakker48916f92012-09-16 19:57:18 +00009711
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009712 /* DTLS clients need to know renego is server-initiated */
9713#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 Beckerb4ff0aa2017-10-17 11:03:04 +01009715 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9716 {
9717 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9718 }
9719#endif
9720 ret = ssl_start_renegotiation( ssl );
9721 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9722 ret != 0 )
9723 {
9724 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9725 return( ret );
9726 }
9727 }
9728 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009729#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009730 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009731 /*
9732 * Refuse renegotiation
9733 */
9734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737#if defined(MBEDTLS_SSL_PROTO_SSL3)
9738 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009739 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009740 /* SSLv3 does not have a "no_renegotiation" warning, so
9741 we send a fatal alert and abort the connection. */
9742 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9743 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9744 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009745 }
9746 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9748#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9749 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9750 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9753 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9754 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009755 {
9756 return( ret );
9757 }
Paul Bakker48916f92012-09-16 19:57:18 +00009758 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009759 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9761 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9764 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009765 }
Paul Bakker48916f92012-09-16 19:57:18 +00009766 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009767
Hanno Becker90333da2017-10-10 11:27:13 +01009768 /* At this point, we don't know whether the renegotiation has been
9769 * completed or not. The cases to consider are the following:
9770 * 1) The renegotiation is complete. In this case, no new record
9771 * has been read yet.
9772 * 2) The renegotiation is incomplete because the client received
9773 * an application data record while awaiting the ServerHello.
9774 * 3) The renegotiation is incomplete because the client received
9775 * a non-handshake, non-application data message while awaiting
9776 * the ServerHello.
9777 * In each of these case, looping will be the proper action:
9778 * - For 1), the next iteration will read a new record and check
9779 * if it's application data.
9780 * - For 2), the loop condition isn't satisfied as application data
9781 * is present, hence continue is the same as break
9782 * - For 3), the loop condition is satisfied and read_record
9783 * will re-deliver the message that was held back by the client
9784 * when expecting the ServerHello.
9785 */
9786 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009787 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009788#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009789 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009790 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009791 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009792 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009793 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009796 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009798 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009799 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009800 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9804 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009805 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009807 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009808 }
9809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9813 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009814 }
9815
9816 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009817
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009818 /* We're going to return something now, cancel timer,
9819 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009821 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009822
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009824 /* If we requested renego but received AppData, resend HelloRequest.
9825 * Do it now, after setting in_offt, to avoid taking this branch
9826 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009830 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009831 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009834 return( ret );
9835 }
9836 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009838#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009839 }
9840
9841 n = ( len < ssl->in_msglen )
9842 ? len : ssl->in_msglen;
9843
9844 memcpy( buf, ssl->in_offt, n );
9845 ssl->in_msglen -= n;
9846
9847 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009848 {
9849 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009850 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009851 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009853 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009854 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009855 /* more data available */
9856 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009860
Paul Bakker23986e52011-04-24 08:57:21 +00009861 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009862}
9863
9864/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009865 * Send application data to be encrypted by the SSL layer, taking care of max
9866 * fragment length and buffer size.
9867 *
9868 * According to RFC 5246 Section 6.2.1:
9869 *
9870 * Zero-length fragments of Application data MAY be sent as they are
9871 * potentially useful as a traffic analysis countermeasure.
9872 *
9873 * Therefore, it is possible that the input message length is 0 and the
9874 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009875 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009876static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009877 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009878{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009879 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9880 const size_t max_len = (size_t) ret;
9881
9882 if( ret < 0 )
9883 {
9884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9885 return( ret );
9886 }
9887
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009888 if( len > max_len )
9889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009891 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009894 "maximum fragment length: %d > %d",
9895 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009897 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009898 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009899#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009900#if defined(MBEDTLS_SSL_PROTO_TLS)
9901 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009902 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009903 }
9904#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009905 }
Paul Bakker887bd502011-06-08 13:10:54 +00009906
Paul Bakker5121ce52009-01-03 21:22:43 +00009907 if( ssl->out_left != 0 )
9908 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009909 /*
9910 * The user has previously tried to send the data and
9911 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9912 * written. In this case, we expect the high-level write function
9913 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009918 return( ret );
9919 }
9920 }
Paul Bakker887bd502011-06-08 13:10:54 +00009921 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009922 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009923 /*
9924 * The user is trying to send a message the first time, so we need to
9925 * copy the data into the internal buffers and setup the data structure
9926 * to keep track of partial writes
9927 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009928 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009930 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009931
Hanno Becker67bc7c32018-08-06 11:33:50 +01009932 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009935 return( ret );
9936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009937 }
9938
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009939 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009940}
9941
9942/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009943 * Write application data, doing 1/n-1 splitting if necessary.
9944 *
9945 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009946 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009947 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009950static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009951 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009952{
9953 int ret;
9954
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009955 if( ssl->conf->cbc_record_splitting ==
9956 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009957 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9959 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9960 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009961 {
9962 return( ssl_write_real( ssl, buf, len ) );
9963 }
9964
9965 if( ssl->split_done == 0 )
9966 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009967 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009968 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009969 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009970 }
9971
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009972 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9973 return( ret );
9974 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009975
9976 return( ret + 1 );
9977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009979
9980/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009981 * Write application data (public-facing wrapper)
9982 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009983int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009984{
9985 int ret;
9986
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009988
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009989 if( ssl == NULL || ssl->conf == NULL )
9990 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9991
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009992#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009993 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9994 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009995 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009996 return( ret );
9997 }
9998#endif
9999
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010000 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010001 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010002 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010003 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010005 return( ret );
10006 }
10007 }
10008
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010009#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010010 ret = ssl_write_split( ssl, buf, len );
10011#else
10012 ret = ssl_write_real( ssl, buf, len );
10013#endif
10014
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010016
10017 return( ret );
10018}
10019
10020/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010021 * Notify the peer that the connection is being closed
10022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010024{
10025 int ret;
10026
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010027 if( ssl == NULL || ssl->conf == NULL )
10028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010032 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010037 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10038 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10039 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010041 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010042 return( ret );
10043 }
10044 }
10045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010047
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010048 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010049}
10050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010052{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010053 if( transform == NULL )
10054 return;
10055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010057 deflateEnd( &transform->ctx_deflate );
10058 inflateEnd( &transform->ctx_inflate );
10059#endif
10060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10062 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010063
Hanno Becker92231322018-01-03 15:32:51 +000010064#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 mbedtls_md_free( &transform->md_ctx_enc );
10066 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010067#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010068
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010069 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010070}
10071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072#if defined(MBEDTLS_X509_CRT_PARSE_C)
10073static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010076
10077 while( cur != NULL )
10078 {
10079 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010081 cur = next;
10082 }
10083}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010085
Hanno Becker0271f962018-08-16 13:23:47 +010010086#if defined(MBEDTLS_SSL_PROTO_DTLS)
10087
10088static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10089{
10090 unsigned offset;
10091 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10092
10093 if( hs == NULL )
10094 return;
10095
Hanno Becker283f5ef2018-08-24 09:34:47 +010010096 ssl_free_buffered_record( ssl );
10097
Hanno Becker0271f962018-08-16 13:23:47 +010010098 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010099 ssl_buffering_free_slot( ssl, offset );
10100}
10101
10102static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10103 uint8_t slot )
10104{
10105 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10106 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010107
10108 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10109 return;
10110
Hanno Beckere605b192018-08-21 15:59:07 +010010111 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010112 {
Hanno Beckere605b192018-08-21 15:59:07 +010010113 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010114 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010115 mbedtls_free( hs_buf->data );
10116 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010117 }
10118}
10119
10120#endif /* MBEDTLS_SSL_PROTO_DTLS */
10121
Gilles Peskine9b562d52018-04-25 20:32:43 +020010122void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010123{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010124 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10125
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010126 if( handshake == NULL )
10127 return;
10128
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010129#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10130 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10131 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010132 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010133 handshake->async_in_progress = 0;
10134 }
10135#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10136
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010137#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10138 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10139 mbedtls_md5_free( &handshake->fin_md5 );
10140 mbedtls_sha1_free( &handshake->fin_sha1 );
10141#endif
10142#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10143#if defined(MBEDTLS_SHA256_C)
10144 mbedtls_sha256_free( &handshake->fin_sha256 );
10145#endif
10146#if defined(MBEDTLS_SHA512_C)
10147 mbedtls_sha512_free( &handshake->fin_sha512 );
10148#endif
10149#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151#if defined(MBEDTLS_DHM_C)
10152 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154#if defined(MBEDTLS_ECDH_C)
10155 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010156#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010157#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010158 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010159#if defined(MBEDTLS_SSL_CLI_C)
10160 mbedtls_free( handshake->ecjpake_cache );
10161 handshake->ecjpake_cache = NULL;
10162 handshake->ecjpake_cache_len = 0;
10163#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010164#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010165
Janos Follath4ae5c292016-02-10 11:27:43 +000010166#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10167 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010168 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010170#endif
10171
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010172#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10173 if( handshake->psk != NULL )
10174 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010175 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010176 mbedtls_free( handshake->psk );
10177 }
10178#endif
10179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10181 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010182 /*
10183 * Free only the linked list wrapper, not the keys themselves
10184 * since the belong to the SNI callback
10185 */
10186 if( handshake->sni_key_cert != NULL )
10187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010189
10190 while( cur != NULL )
10191 {
10192 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010194 cur = next;
10195 }
10196 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010198
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010199#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010200 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010201#endif
10202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203#if defined(MBEDTLS_SSL_PROTO_DTLS)
10204 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010205 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010206 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010207#endif
10208
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010209 mbedtls_platform_zeroize( handshake,
10210 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010211}
10212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010214{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010215 if( session == NULL )
10216 return;
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010219 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010220#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010221
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010222#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010224#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010225
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010226 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010227}
10228
Paul Bakker5121ce52009-01-03 21:22:43 +000010229/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010230 * Serialize a full SSL context
10231 */
10232int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10233 unsigned char *buf,
10234 size_t buf_len,
10235 size_t *olen )
10236{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010237 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010238 (void) ssl;
10239
10240 if( buf != NULL )
10241 memset( buf, 0, buf_len );
10242
10243 *olen = 0;
10244
10245 return( 0 );
10246}
10247
10248/*
10249 * Deserialize a full SSL context
10250 */
10251int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10252 const unsigned char *buf,
10253 size_t len )
10254{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010255 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010256 (void) ssl;
10257 (void) buf;
10258 (void) len;
10259
10260 return( 0 );
10261}
10262
10263/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010264 * Free an SSL context
10265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010267{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010268 if( ssl == NULL )
10269 return;
10270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010272
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010273 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010274 {
Angus Grattond8213d02016-05-25 20:56:48 +100010275 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010276 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 }
10278
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010279 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010280 {
Angus Grattond8213d02016-05-25 20:56:48 +100010281 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010283 }
10284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010285#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010286 if( ssl->compress_buf != NULL )
10287 {
Angus Grattond8213d02016-05-25 20:56:48 +100010288 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010290 }
10291#endif
10292
Paul Bakker48916f92012-09-16 19:57:18 +000010293 if( ssl->transform )
10294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 mbedtls_ssl_transform_free( ssl->transform );
10296 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010297 }
10298
10299 if( ssl->handshake )
10300 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010301 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10303 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 mbedtls_free( ssl->handshake );
10306 mbedtls_free( ssl->transform_negotiate );
10307 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010308 }
10309
Paul Bakkerc0463502013-02-14 11:19:38 +010010310 if( ssl->session )
10311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312 mbedtls_ssl_session_free( ssl->session );
10313 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010314 }
10315
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010316#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010317 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010318 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010319 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010321 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10325 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10328 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010329 }
10330#endif
10331
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010332#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010333 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010334#endif
10335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010337
Paul Bakker86f04f42013-02-14 11:20:09 +010010338 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010339 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010340}
10341
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010342/*
10343 * Initialze mbedtls_ssl_config
10344 */
10345void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10346{
10347 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010348
10349#if !defined(MBEDTLS_SSL_PROTO_TLS)
10350 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10351#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010352}
10353
Simon Butcherc97b6972015-12-27 23:48:17 +000010354#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010355static int ssl_preset_default_hashes[] = {
10356#if defined(MBEDTLS_SHA512_C)
10357 MBEDTLS_MD_SHA512,
10358 MBEDTLS_MD_SHA384,
10359#endif
10360#if defined(MBEDTLS_SHA256_C)
10361 MBEDTLS_MD_SHA256,
10362 MBEDTLS_MD_SHA224,
10363#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010364#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010365 MBEDTLS_MD_SHA1,
10366#endif
10367 MBEDTLS_MD_NONE
10368};
Simon Butcherc97b6972015-12-27 23:48:17 +000010369#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010370
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010371static int ssl_preset_suiteb_ciphersuites[] = {
10372 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10373 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10374 0
10375};
10376
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010377#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010378static int ssl_preset_suiteb_hashes[] = {
10379 MBEDTLS_MD_SHA256,
10380 MBEDTLS_MD_SHA384,
10381 MBEDTLS_MD_NONE
10382};
10383#endif
10384
10385#if defined(MBEDTLS_ECP_C)
10386static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10387 MBEDTLS_ECP_DP_SECP256R1,
10388 MBEDTLS_ECP_DP_SECP384R1,
10389 MBEDTLS_ECP_DP_NONE
10390};
10391#endif
10392
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010393/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010394 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010395 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010396int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010397 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010398{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010399#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010400 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010401#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010402
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010403 /* Use the functions here so that they are covered in tests,
10404 * but otherwise access member directly for efficiency */
10405 mbedtls_ssl_conf_endpoint( conf, endpoint );
10406 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010407
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010408 /*
10409 * Things that are common to all presets
10410 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010411#if defined(MBEDTLS_SSL_CLI_C)
10412 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10413 {
10414 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10415#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10416 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10417#endif
10418 }
10419#endif
10420
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010421#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010423#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010424
10425#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10426 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10427#endif
10428
10429#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10430 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010431 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010432 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010433#endif
10434
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010435#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10436 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10437#endif
10438
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010439#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010440 conf->f_cookie_write = ssl_cookie_write_dummy;
10441 conf->f_cookie_check = ssl_cookie_check_dummy;
10442#endif
10443
10444#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10445 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10446#endif
10447
Janos Follath088ce432017-04-10 12:42:31 +010010448#if defined(MBEDTLS_SSL_SRV_C)
10449 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10450#endif
10451
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010452#if defined(MBEDTLS_SSL_PROTO_DTLS)
10453 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10454 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10455#endif
10456
10457#if defined(MBEDTLS_SSL_RENEGOTIATION)
10458 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010459 memset( conf->renego_period, 0x00, 2 );
10460 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010461#endif
10462
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010463#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10464 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10465 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010466 const unsigned char dhm_p[] =
10467 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10468 const unsigned char dhm_g[] =
10469 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10470
Hanno Beckera90658f2017-10-04 15:29:08 +010010471 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10472 dhm_p, sizeof( dhm_p ),
10473 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010474 {
10475 return( ret );
10476 }
10477 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010478#endif
10479
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010480 /*
10481 * Preset-specific defaults
10482 */
10483 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010485 /*
10486 * NSA Suite B
10487 */
10488 case MBEDTLS_SSL_PRESET_SUITEB:
10489 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10490 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10491 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10492 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10493
10494 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10495 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10496 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10497 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10498 ssl_preset_suiteb_ciphersuites;
10499
10500#if defined(MBEDTLS_X509_CRT_PARSE_C)
10501 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010502#endif
10503
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010504#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010505 conf->sig_hashes = ssl_preset_suiteb_hashes;
10506#endif
10507
10508#if defined(MBEDTLS_ECP_C)
10509 conf->curve_list = ssl_preset_suiteb_curves;
10510#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010511 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512
10513 /*
10514 * Default
10515 */
10516 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010517 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10518 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10519 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10520 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10521 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10522 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10523 MBEDTLS_SSL_MIN_MINOR_VERSION :
10524 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010525 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10526 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10527
10528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010529 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010530 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10531#endif
10532
10533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10534 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10535 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10536 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10537 mbedtls_ssl_list_ciphersuites();
10538
10539#if defined(MBEDTLS_X509_CRT_PARSE_C)
10540 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10541#endif
10542
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010543#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010544 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010545#endif
10546
10547#if defined(MBEDTLS_ECP_C)
10548 conf->curve_list = mbedtls_ecp_grp_id_list();
10549#endif
10550
10551#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10552 conf->dhm_min_bitlen = 1024;
10553#endif
10554 }
10555
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556 return( 0 );
10557}
10558
10559/*
10560 * Free mbedtls_ssl_config
10561 */
10562void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10563{
10564#if defined(MBEDTLS_DHM_C)
10565 mbedtls_mpi_free( &conf->dhm_P );
10566 mbedtls_mpi_free( &conf->dhm_G );
10567#endif
10568
10569#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10570 if( conf->psk != NULL )
10571 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010572 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010574 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010576 }
10577
10578 if( conf->psk_identity != NULL )
10579 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010580 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010581 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010582 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010583 conf->psk_identity_len = 0;
10584 }
10585#endif
10586
10587#if defined(MBEDTLS_X509_CRT_PARSE_C)
10588 ssl_key_cert_free( conf->key_cert );
10589#endif
10590
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010591 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010592}
10593
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010594#if defined(MBEDTLS_PK_C) && \
10595 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010596/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010601#if defined(MBEDTLS_RSA_C)
10602 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10603 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605#if defined(MBEDTLS_ECDSA_C)
10606 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10607 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010610}
10611
Hanno Becker7e5437a2017-04-28 17:15:26 +010010612unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10613{
10614 switch( type ) {
10615 case MBEDTLS_PK_RSA:
10616 return( MBEDTLS_SSL_SIG_RSA );
10617 case MBEDTLS_PK_ECDSA:
10618 case MBEDTLS_PK_ECKEY:
10619 return( MBEDTLS_SSL_SIG_ECDSA );
10620 default:
10621 return( MBEDTLS_SSL_SIG_ANON );
10622 }
10623}
10624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010625mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010626{
10627 switch( sig )
10628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629#if defined(MBEDTLS_RSA_C)
10630 case MBEDTLS_SSL_SIG_RSA:
10631 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010632#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010633#if defined(MBEDTLS_ECDSA_C)
10634 case MBEDTLS_SSL_SIG_ECDSA:
10635 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010636#endif
10637 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010638 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010639 }
10640}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010641#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010642
Hanno Becker7e5437a2017-04-28 17:15:26 +010010643#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10644 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10645
10646/* Find an entry in a signature-hash set matching a given hash algorithm. */
10647mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10648 mbedtls_pk_type_t sig_alg )
10649{
10650 switch( sig_alg )
10651 {
10652 case MBEDTLS_PK_RSA:
10653 return( set->rsa );
10654 case MBEDTLS_PK_ECDSA:
10655 return( set->ecdsa );
10656 default:
10657 return( MBEDTLS_MD_NONE );
10658 }
10659}
10660
10661/* Add a signature-hash-pair to a signature-hash set */
10662void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10663 mbedtls_pk_type_t sig_alg,
10664 mbedtls_md_type_t md_alg )
10665{
10666 switch( sig_alg )
10667 {
10668 case MBEDTLS_PK_RSA:
10669 if( set->rsa == MBEDTLS_MD_NONE )
10670 set->rsa = md_alg;
10671 break;
10672
10673 case MBEDTLS_PK_ECDSA:
10674 if( set->ecdsa == MBEDTLS_MD_NONE )
10675 set->ecdsa = md_alg;
10676 break;
10677
10678 default:
10679 break;
10680 }
10681}
10682
10683/* Allow exactly one hash algorithm for each signature. */
10684void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10685 mbedtls_md_type_t md_alg )
10686{
10687 set->rsa = md_alg;
10688 set->ecdsa = md_alg;
10689}
10690
10691#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10692 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10693
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010694/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010695 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010698{
10699 switch( hash )
10700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010701#if defined(MBEDTLS_MD5_C)
10702 case MBEDTLS_SSL_HASH_MD5:
10703 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010704#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010705#if defined(MBEDTLS_SHA1_C)
10706 case MBEDTLS_SSL_HASH_SHA1:
10707 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010709#if defined(MBEDTLS_SHA256_C)
10710 case MBEDTLS_SSL_HASH_SHA224:
10711 return( MBEDTLS_MD_SHA224 );
10712 case MBEDTLS_SSL_HASH_SHA256:
10713 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010715#if defined(MBEDTLS_SHA512_C)
10716 case MBEDTLS_SSL_HASH_SHA384:
10717 return( MBEDTLS_MD_SHA384 );
10718 case MBEDTLS_SSL_HASH_SHA512:
10719 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010720#endif
10721 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010723 }
10724}
10725
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010726/*
10727 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10728 */
10729unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10730{
10731 switch( md )
10732 {
10733#if defined(MBEDTLS_MD5_C)
10734 case MBEDTLS_MD_MD5:
10735 return( MBEDTLS_SSL_HASH_MD5 );
10736#endif
10737#if defined(MBEDTLS_SHA1_C)
10738 case MBEDTLS_MD_SHA1:
10739 return( MBEDTLS_SSL_HASH_SHA1 );
10740#endif
10741#if defined(MBEDTLS_SHA256_C)
10742 case MBEDTLS_MD_SHA224:
10743 return( MBEDTLS_SSL_HASH_SHA224 );
10744 case MBEDTLS_MD_SHA256:
10745 return( MBEDTLS_SSL_HASH_SHA256 );
10746#endif
10747#if defined(MBEDTLS_SHA512_C)
10748 case MBEDTLS_MD_SHA384:
10749 return( MBEDTLS_SSL_HASH_SHA384 );
10750 case MBEDTLS_MD_SHA512:
10751 return( MBEDTLS_SSL_HASH_SHA512 );
10752#endif
10753 default:
10754 return( MBEDTLS_SSL_HASH_NONE );
10755 }
10756}
10757
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010758#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010759/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010760 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010761 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010762 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010763int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010765 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010766
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010767 if( ssl->conf->curve_list == NULL )
10768 return( -1 );
10769
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010770 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010771 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010772 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010773
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010774 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010775}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010776#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010777
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010778#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010779/*
10780 * Check if a hash proposed by the peer is in our list.
10781 * Return 0 if we're willing to use it, -1 otherwise.
10782 */
10783int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10784 mbedtls_md_type_t md )
10785{
10786 const int *cur;
10787
10788 if( ssl->conf->sig_hashes == NULL )
10789 return( -1 );
10790
10791 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10792 if( *cur == (int) md )
10793 return( 0 );
10794
10795 return( -1 );
10796}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010797#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010799#if defined(MBEDTLS_X509_CRT_PARSE_C)
10800int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10801 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010802 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010803 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010804{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010805 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010807 int usage = 0;
10808#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010810 const char *ext_oid;
10811 size_t ext_len;
10812#endif
10813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10815 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010816 ((void) cert);
10817 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010818 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010819#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010821#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10822 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010823 {
10824 /* Server part of the key exchange */
10825 switch( ciphersuite->key_exchange )
10826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827 case MBEDTLS_KEY_EXCHANGE_RSA:
10828 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010829 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010830 break;
10831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010832 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10833 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10834 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10835 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010836 break;
10837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10839 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010840 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010841 break;
10842
10843 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844 case MBEDTLS_KEY_EXCHANGE_NONE:
10845 case MBEDTLS_KEY_EXCHANGE_PSK:
10846 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10847 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010848 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010849 usage = 0;
10850 }
10851 }
10852 else
10853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10855 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010856 }
10857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010859 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010860 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010861 ret = -1;
10862 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010863#else
10864 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10868 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10871 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010872 }
10873 else
10874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10876 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010877 }
10878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010880 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010881 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010882 ret = -1;
10883 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010884#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010885
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010886 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010887}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010889
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010890/*
10891 * Convert version numbers to/from wire format
10892 * and, for DTLS, to/from TLS equivalent.
10893 *
10894 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010895 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010896 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10897 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010900 unsigned char ver[2] )
10901{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010902#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10903 ((void) transport);
10904#endif
10905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010907 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010909 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010910 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10911
10912 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10913 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10914 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010915 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010916#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010917#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010918 {
10919 ver[0] = (unsigned char) major;
10920 ver[1] = (unsigned char) minor;
10921 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010922#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010923}
10924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010925void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010926 const unsigned char ver[2] )
10927{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010928#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10929 ((void) transport);
10930#endif
10931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010933 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010934 {
10935 *major = 255 - ver[0] + 2;
10936 *minor = 255 - ver[1] + 1;
10937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010938 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010939 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10940 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010941 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010942#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010943#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010944 {
10945 *major = ver[0];
10946 *minor = ver[1];
10947 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020010948#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010949}
10950
Simon Butcher99000142016-10-13 17:21:01 +010010951int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10952{
10953#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10954 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10955 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10956
10957 switch( md )
10958 {
10959#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10960#if defined(MBEDTLS_MD5_C)
10961 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010962 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010963#endif
10964#if defined(MBEDTLS_SHA1_C)
10965 case MBEDTLS_SSL_HASH_SHA1:
10966 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10967 break;
10968#endif
10969#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10970#if defined(MBEDTLS_SHA512_C)
10971 case MBEDTLS_SSL_HASH_SHA384:
10972 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10973 break;
10974#endif
10975#if defined(MBEDTLS_SHA256_C)
10976 case MBEDTLS_SSL_HASH_SHA256:
10977 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10978 break;
10979#endif
10980 default:
10981 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10982 }
10983
10984 return 0;
10985#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10986 (void) ssl;
10987 (void) md;
10988
10989 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10990#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10991}
10992
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010993#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10994 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10995int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10996 unsigned char *output,
10997 unsigned char *data, size_t data_len )
10998{
10999 int ret = 0;
11000 mbedtls_md5_context mbedtls_md5;
11001 mbedtls_sha1_context mbedtls_sha1;
11002
11003 mbedtls_md5_init( &mbedtls_md5 );
11004 mbedtls_sha1_init( &mbedtls_sha1 );
11005
11006 /*
11007 * digitally-signed struct {
11008 * opaque md5_hash[16];
11009 * opaque sha_hash[20];
11010 * };
11011 *
11012 * md5_hash
11013 * MD5(ClientHello.random + ServerHello.random
11014 * + ServerParams);
11015 * sha_hash
11016 * SHA(ClientHello.random + ServerHello.random
11017 * + ServerParams);
11018 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011021 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011022 goto exit;
11023 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011024 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011025 ssl->handshake->randbytes, 64 ) ) != 0 )
11026 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011028 goto exit;
11029 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011030 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011031 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011032 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011033 goto exit;
11034 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011035 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011036 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011038 goto exit;
11039 }
11040
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011041 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011042 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011044 goto exit;
11045 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011046 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011047 ssl->handshake->randbytes, 64 ) ) != 0 )
11048 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011049 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011050 goto exit;
11051 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011052 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011053 data_len ) ) != 0 )
11054 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011056 goto exit;
11057 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011058 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011059 output + 16 ) ) != 0 )
11060 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011061 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011062 goto exit;
11063 }
11064
11065exit:
11066 mbedtls_md5_free( &mbedtls_md5 );
11067 mbedtls_sha1_free( &mbedtls_sha1 );
11068
11069 if( ret != 0 )
11070 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11071 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11072
11073 return( ret );
11074
11075}
11076#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11077 MBEDTLS_SSL_PROTO_TLS1_1 */
11078
11079#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11080 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11081int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011082 unsigned char *hash, size_t *hashlen,
11083 unsigned char *data, size_t data_len,
11084 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011085{
11086 int ret = 0;
11087 mbedtls_md_context_t ctx;
11088 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011089 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011090
11091 mbedtls_md_init( &ctx );
11092
11093 /*
11094 * digitally-signed struct {
11095 * opaque client_random[32];
11096 * opaque server_random[32];
11097 * ServerDHParams params;
11098 * };
11099 */
11100 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11101 {
11102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11103 goto exit;
11104 }
11105 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11106 {
11107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11108 goto exit;
11109 }
11110 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11111 {
11112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11113 goto exit;
11114 }
11115 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11116 {
11117 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11118 goto exit;
11119 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011120 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011121 {
11122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11123 goto exit;
11124 }
11125
11126exit:
11127 mbedtls_md_free( &ctx );
11128
11129 if( ret != 0 )
11130 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11131 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11132
11133 return( ret );
11134}
11135#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11136 MBEDTLS_SSL_PROTO_TLS1_2 */
11137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011138#endif /* MBEDTLS_SSL_TLS_C */