blob: 7729cbca6c97a86ae40acc86f49e7732fa5c5afd [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 Beckere0200da2019-06-13 09:23:43 +0100120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
121 !defined(MBEDTLS_SSL_CONF_CID_LEN) && \
122 !defined(MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100123int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
124 size_t len,
125 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100126{
127 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
129
Hanno Becker791ec6b2019-05-14 11:45:26 +0100130 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
131 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
132 {
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134 }
135
136 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100137 conf->cid_len = len;
138 return( 0 );
139}
Hanno Beckere0200da2019-06-13 09:23:43 +0100140#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
141 !MBEDTLS_SSL_CONF_CID_LEN &&
142 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
143
144#if MBEDTLS_SSL_CONF_CID_LEN > MBEDTLS_SSL_CID_IN_LEN_MAX
145#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_CID_LEN"
146#endif
147#if MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE && \
148 MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID != MBEDTLS_SSL_UNEXPECTED_CID_FAIL
149#error "Invalid hardcoded value for MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID"
150#endif
151
152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID &&
153 !MBEDTLS_SSL_CONF_CID_LEN &&
154 !MBEDTLS_SSL_CONF_IGNORE_UNEXPECTED_CID */
Hanno Beckereec2be92019-05-03 13:06:44 +0100155
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100156int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
157 int enable,
158 unsigned char const *own_cid,
159 size_t own_cid_len )
160{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200161 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
163
Hanno Becker07489862019-04-25 16:01:49 +0100164 ssl->negotiate_cid = enable;
165 if( enable == MBEDTLS_SSL_CID_DISABLED )
166 {
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
168 return( 0 );
169 }
170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100171 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100172
Hanno Beckere0200da2019-06-13 09:23:43 +0100173 if( own_cid_len != mbedtls_ssl_conf_get_cid_len( ssl->conf ) )
Hanno Becker07489862019-04-25 16:01:49 +0100174 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100175 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
176 (unsigned) own_cid_len,
Hanno Beckere0200da2019-06-13 09:23:43 +0100177 (unsigned) mbedtls_ssl_conf_get_cid_len( ssl->conf ) ) );
Hanno Becker07489862019-04-25 16:01:49 +0100178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
179 }
180
181 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100182 /* Truncation is not an issue here because
183 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
184 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100185
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100186 return( 0 );
187}
188
189int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
190 int *enabled,
191 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
192 size_t *peer_cid_len )
193{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100194 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100195
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200196 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100197 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
198 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100199 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100200 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100201
Hanno Beckercb063f52019-05-03 12:54:52 +0100202 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
203 * were used, but client and server requested the empty CID.
204 * This is indistinguishable from not using the CID extension
205 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100206 if( ssl->transform_in->in_cid_len == 0 &&
207 ssl->transform_in->out_cid_len == 0 )
208 {
209 return( 0 );
210 }
211
Hanno Becker633d6042019-05-22 16:50:35 +0100212 if( peer_cid_len != NULL )
213 {
214 *peer_cid_len = ssl->transform_in->out_cid_len;
215 if( peer_cid != NULL )
216 {
217 memcpy( peer_cid, ssl->transform_in->out_cid,
218 ssl->transform_in->out_cid_len );
219 }
220 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100221
222 *enabled = MBEDTLS_SSL_CID_ENABLED;
223
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100224 return( 0 );
225}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100226#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100227
Hanno Beckerd5847772018-08-28 10:09:23 +0100228/* Forward declarations for functions related to message buffering. */
229static void ssl_buffering_free( mbedtls_ssl_context *ssl );
230static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
231 uint8_t slot );
232static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
233static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
234static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
235static int ssl_buffer_message( mbedtls_ssl_context *ssl );
236static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100237static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100238
Hanno Beckera67dee22018-08-22 10:05:20 +0100239static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100240static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100241{
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100243
244 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100245 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100246
247 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
248}
249
Hanno Becker67bc7c32018-08-06 11:33:50 +0100250static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
251{
Hanno Becker11682cc2018-08-22 14:41:02 +0100252 size_t const bytes_written = ssl->out_left;
253 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100254
255 /* Double-check that the write-index hasn't gone
256 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100257 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100258 {
259 /* Should never happen... */
260 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
261 }
262
263 return( (int) ( mtu - bytes_written ) );
264}
265
266static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
267{
268 int ret;
269 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400270 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100271
272#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
273 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
274
275 if( max_len > mfl )
276 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100277
278 /* By the standard (RFC 6066 Sect. 4), the MFL extension
279 * only limits the maximum record payload size, so in theory
280 * we would be allowed to pack multiple records of payload size
281 * MFL into a single datagram. However, this would mean that there's
282 * no way to explicitly communicate MTU restrictions to the peer.
283 *
284 * The following reduction of max_len makes sure that we never
285 * write datagrams larger than MFL + Record Expansion Overhead.
286 */
287 if( max_len <= ssl->out_left )
288 return( 0 );
289
290 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100291#endif
292
293 ret = ssl_get_remaining_space_in_datagram( ssl );
294 if( ret < 0 )
295 return( ret );
296 remaining = (size_t) ret;
297
298 ret = mbedtls_ssl_get_record_expansion( ssl );
299 if( ret < 0 )
300 return( ret );
301 expansion = (size_t) ret;
302
303 if( remaining <= expansion )
304 return( 0 );
305
306 remaining -= expansion;
307 if( remaining >= max_len )
308 remaining = max_len;
309
310 return( (int) remaining );
311}
312
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313/*
314 * Double the retransmit timeout value, within the allowed range,
315 * returning -1 if the maximum value has already been reached.
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318{
319 uint32_t new_timeout;
320
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200321 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 return( -1 );
323
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200324 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
325 * in the following way: after the initial transmission and a first
326 * retransmission, back off to a temporary estimated MTU of 508 bytes.
327 * This value is guaranteed to be deliverable (if not guaranteed to be
328 * delivered) of any compliant IPv4 (and IPv6) network, and should work
329 * on most non-IP stacks too. */
330 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400331 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200332 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
334 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200335
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200336 new_timeout = 2 * ssl->handshake->retransmit_timeout;
337
338 /* Avoid arithmetic overflow and range overflow */
339 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200340 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200342 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200343 }
344
345 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200347 ssl->handshake->retransmit_timeout ) );
348
349 return( 0 );
350}
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200353{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200354 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200356 ssl->handshake->retransmit_timeout ) );
357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200361/*
362 * Convert max_fragment_length codes to length.
363 * RFC 6066 says:
364 * enum{
365 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
366 * } MaxFragmentLength;
367 * and we add 0 -> extension unused
368 */
Angus Grattond8213d02016-05-25 20:56:48 +1000369static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370{
Angus Grattond8213d02016-05-25 20:56:48 +1000371 switch( mfl )
372 {
373 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
374 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
375 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
376 return 512;
377 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
378 return 1024;
379 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
380 return 2048;
381 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
382 return 4096;
383 default:
384 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
385 }
386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200388
Hanno Becker58fccf22019-02-06 14:30:46 +0000389int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
390 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_ssl_session_free( dst );
393 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000396
397#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398 if( src->peer_cert != NULL )
399 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200400 int ret;
401
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200402 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200403 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200409 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200412 dst->peer_cert = NULL;
413 return( ret );
414 }
415 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100416#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000417 if( src->peer_cert_digest != NULL )
418 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000419 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000420 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000421 if( dst->peer_cert_digest == NULL )
422 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
423
424 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
425 src->peer_cert_digest_len );
426 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000427 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000428 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100429#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200432
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200433#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200434 if( src->ticket != NULL )
435 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200436 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200437 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200438 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200439
440 memcpy( dst->ticket, src->ticket, src->ticket_len );
441 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200442#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200443
444 return( 0 );
445}
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
448int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200449 const unsigned char *key_enc, const unsigned char *key_dec,
450 size_t keylen,
451 const unsigned char *iv_enc, const unsigned char *iv_dec,
452 size_t ivlen,
453 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200454 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
456int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
457int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
458int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
459int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
460#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000461
Paul Bakker5121ce52009-01-03 21:22:43 +0000462/*
463 * Key material generation
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200466static int ssl3_prf( const unsigned char *secret, size_t slen,
467 const char *label,
468 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000469 unsigned char *dstbuf, size_t dlen )
470{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100471 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000472 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200473 mbedtls_md5_context md5;
474 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000475 unsigned char padding[16];
476 unsigned char sha1sum[20];
477 ((void)label);
478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200479 mbedtls_md5_init( &md5 );
480 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200481
Paul Bakker5f70b252012-09-13 14:23:06 +0000482 /*
483 * SSLv3:
484 * block =
485 * MD5( secret + SHA1( 'A' + secret + random ) ) +
486 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
487 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
488 * ...
489 */
490 for( i = 0; i < dlen / 16; i++ )
491 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200492 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000493
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100495 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100498 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100499 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100500 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100501 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000504
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100505 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100506 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100507 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100508 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100509 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100510 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100511 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100512 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000513 }
514
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100515exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200516 mbedtls_md5_free( &md5 );
517 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000518
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500519 mbedtls_platform_zeroize( padding, sizeof( padding ) );
520 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000521
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100522 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200527static int tls1_prf( const unsigned char *secret, size_t slen,
528 const char *label,
529 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000530 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531{
Paul Bakker23986e52011-04-24 08:57:21 +0000532 size_t nb, hs;
533 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200534 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 unsigned char tmp[128];
536 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 const mbedtls_md_info_t *md_info;
538 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539 int ret;
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
546 hs = ( slen + 1 ) / 2;
547 S1 = secret;
548 S2 = secret + slen - hs;
549
550 nb = strlen( label );
551 memcpy( tmp + 20, label, nb );
552 memcpy( tmp + 20 + nb, random, rlen );
553 nb += rlen;
554
555 /*
556 * First compute P_md5(secret,label+random)[0..dlen]
557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100562 return( ret );
563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
565 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
566 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568 for( i = 0; i < dlen; i += 16 )
569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_hmac_reset ( &md_ctx );
571 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
572 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_md_hmac_reset ( &md_ctx );
575 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
576 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
579
580 for( j = 0; j < k; j++ )
581 dstbuf[i + j] = h_i[j];
582 }
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100585
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 /*
587 * XOR out with P_sha1(secret,label+random)[0..dlen]
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
590 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593 return( ret );
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
596 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
597 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 for( i = 0; i < dlen; i += 20 )
600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_md_hmac_reset ( &md_ctx );
602 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
603 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_hmac_reset ( &md_ctx );
606 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
607 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
610
611 for( j = 0; j < k; j++ )
612 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
613 }
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100616
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500617 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
618 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620 return( 0 );
621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
625static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100626 const unsigned char *secret, size_t slen,
627 const char *label,
628 const unsigned char *random, size_t rlen,
629 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630{
631 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000633 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
635 const mbedtls_md_info_t *md_info;
636 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100637 int ret;
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
642 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100645
646 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000648
649 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100650 memcpy( tmp + md_len, label, nb );
651 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652 nb += rlen;
653
654 /*
655 * Compute P_<hash>(secret, label + random)[0..dlen]
656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100658 return( ret );
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
661 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
662 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100663
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100664 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 mbedtls_md_hmac_reset ( &md_ctx );
667 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
668 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_md_hmac_reset ( &md_ctx );
671 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
672 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000673
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100674 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000675
676 for( j = 0; j < k; j++ )
677 dstbuf[i + j] = h_i[j];
678 }
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100681
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500682 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
683 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000684
685 return( 0 );
686}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100689static int tls_prf_sha256( const unsigned char *secret, size_t slen,
690 const char *label,
691 const unsigned char *random, size_t rlen,
692 unsigned char *dstbuf, size_t dlen )
693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100695 label, random, rlen, dstbuf, dlen ) );
696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200700static int tls_prf_sha384( const unsigned char *secret, size_t slen,
701 const char *label,
702 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000703 unsigned char *dstbuf, size_t dlen )
704{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100706 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708#endif /* MBEDTLS_SHA512_C */
709#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
714 defined(MBEDTLS_SSL_PROTO_TLS1_1)
715static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#endif
Paul Bakker380da532012-04-18 16:10:25 +0000717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200721#endif
722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200724static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200726#endif
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
729#if defined(MBEDTLS_SHA256_C)
730static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200731static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200733#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_SHA512_C)
736static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200737static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000741
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742/* Type for the TLS PRF */
743typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
744 const unsigned char *, size_t,
745 unsigned char *, size_t);
746
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200747/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200748 * Populate a transform structure with session keys and all the other
749 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751 * Parameters:
752 * - [in/out]: transform: structure to populate
753 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200754 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755 * - [in] ciphersuite
756 * - [in] master
757 * - [in] encrypt_then_mac
758 * - [in] trunc_hmac
759 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200760 * - [in] tls_prf: pointer to PRF to use for key derivation
761 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200762 * - [in] minor_ver: SSL/TLS minor version
763 * - [in] endpoint: client or server
764 * - [in] ssl: optionally used for:
765 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
766 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
767 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200768 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200770 int ciphersuite,
771 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100772#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200773#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
774 int encrypt_then_mac,
775#endif
776#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
777 int trunc_hmac,
778#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100779#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200780#if defined(MBEDTLS_ZLIB_SUPPORT)
781 int compression,
782#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200783 ssl_tls_prf_t tls_prf,
784 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200785 int minor_ver,
786 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200787 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000788{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200789 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 unsigned char keyblk[256];
791 unsigned char *key1;
792 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100793 unsigned char *mac_enc;
794 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000795 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200796 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000797 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000798 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 const mbedtls_cipher_info_t *cipher_info;
800 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100801
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200802#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
803 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
804 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200805 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200806 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000807#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000808
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200809 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100810#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
811 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200812 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000813#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200814 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200816 /*
817 * Get various info structures
818 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200819 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200820 if( ciphersuite_info == NULL )
821 {
822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200823 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
825 }
826
Hanno Becker8759e162017-12-27 21:34:08 +0000827 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100828 if( cipher_info == NULL )
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000831 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100833 }
834
Hanno Becker8759e162017-12-27 21:34:08 +0000835 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100836 if( md_info == NULL )
837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000839 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100841 }
842
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844 /* Copy own and peer's CID if the use of the CID
845 * extension has been negotiated. */
846 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
847 {
848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100849
Hanno Becker4932f9f2019-05-03 15:23:51 +0100850 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100851 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100852 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100853 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100854
855 transform->out_cid_len = ssl->handshake->peer_cid_len;
856 memcpy( transform->out_cid, ssl->handshake->peer_cid,
857 ssl->handshake->peer_cid_len );
858 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
859 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100860 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100861#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100862
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200864 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000865 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200866 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100867 if( ret != 0 )
868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100870 return( ret );
871 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200874 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200875 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200876 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 /*
880 * Determine the appropriate key, IV and MAC length.
881 */
Paul Bakker68884e32013-01-07 18:20:04 +0100882
Hanno Beckere7f2df02017-12-27 08:17:40 +0000883 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200884
Hanno Beckerf1229442018-01-03 15:32:31 +0000885#if defined(MBEDTLS_GCM_C) || \
886 defined(MBEDTLS_CCM_C) || \
887 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200889 cipher_info->mode == MBEDTLS_MODE_CCM ||
890 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000891 {
Hanno Becker8759e162017-12-27 21:34:08 +0000892 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200893
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200894 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000895 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000896 transform->taglen =
897 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200898
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200899 /* All modes haves 96-bit IVs;
900 * GCM and CCM has 4 implicit and 8 explicit bytes
901 * ChachaPoly has all 12 bytes implicit
902 */
Paul Bakker68884e32013-01-07 18:20:04 +0100903 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200904 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
905 transform->fixed_ivlen = 12;
906 else
907 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200908
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200909 /* Minimum length of encrypted record */
910 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000911 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100912 }
913 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000914#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
915#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
916 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
917 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100918 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200919 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
921 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200924 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100925 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200927 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000928 mac_key_len = mbedtls_md_get_size( md_info );
929 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200932 /*
933 * If HMAC is to be truncated, we shall keep the leftmost bytes,
934 * (rfc 6066 page 13 or rfc 2104 section 4),
935 * so we only need to adjust the length here.
936 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200937 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000940
941#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
942 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000943 * HMAC implementation which also truncates the key
944 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000945 mac_key_len = transform->maclen;
946#endif
947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200949
950 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100951 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200953 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200955 transform->minlen = transform->maclen;
956 else
Paul Bakker68884e32013-01-07 18:20:04 +0100957 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200958 /*
959 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100960 * 1. if EtM is in use: one block plus MAC
961 * otherwise: * first multiple of blocklen greater than maclen
962 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200965 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100966 {
967 transform->minlen = transform->maclen
968 + cipher_info->block_size;
969 }
970 else
971#endif
972 {
973 transform->minlen = transform->maclen
974 + cipher_info->block_size
975 - transform->maclen % cipher_info->block_size;
976 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200979 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
980 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200981 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100982 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200985 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
986 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200987 {
988 transform->minlen += transform->ivlen;
989 }
990 else
991#endif
992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
994 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200995 }
Paul Bakker68884e32013-01-07 18:20:04 +0100996 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000997 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000998 else
999#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1000 {
1001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1002 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1003 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Hanno Beckere7f2df02017-12-27 08:17:40 +00001005 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1006 (unsigned) keylen,
1007 (unsigned) transform->minlen,
1008 (unsigned) transform->ivlen,
1009 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
1011 /*
1012 * Finally setup the cipher contexts, IVs and MAC secrets.
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001015 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001016 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001017 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001018 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001019
Paul Bakker68884e32013-01-07 18:20:04 +01001020 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001021 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001022
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001023 /*
1024 * This is not used in TLS v1.1.
1025 */
Paul Bakker48916f92012-09-16 19:57:18 +00001026 iv_copy_len = ( transform->fixed_ivlen ) ?
1027 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001028 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1029 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001030 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 }
1032 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#endif /* MBEDTLS_SSL_CLI_C */
1034#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001035 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001036 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001037 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001038 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001039
Hanno Becker81c7b182017-11-09 18:39:33 +00001040 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001041 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001043 /*
1044 * This is not used in TLS v1.1.
1045 */
Paul Bakker48916f92012-09-16 19:57:18 +00001046 iv_copy_len = ( transform->fixed_ivlen ) ?
1047 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001048 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1049 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001050 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001052 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Hanno Becker92231322018-01-03 15:32:51 +00001059#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001061 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001062 {
Hanno Becker92231322018-01-03 15:32:51 +00001063 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1066 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001067 }
1068
Hanno Becker81c7b182017-11-09 18:39:33 +00001069 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1070 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001071 }
1072 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1074#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1075 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001076 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001077 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001078 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1079 For AEAD-based ciphersuites, there is nothing to do here. */
1080 if( mac_key_len != 0 )
1081 {
1082 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1083 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1084 }
Paul Bakker68884e32013-01-07 18:20:04 +01001085 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001086 else
1087#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001091 }
Hanno Becker92231322018-01-03 15:32:51 +00001092#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1095 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001096 {
1097 int ret = 0;
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001100
Hanno Beckere7f2df02017-12-27 08:17:40 +00001101 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001102 transform->iv_enc, transform->iv_dec,
1103 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001104 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001105 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1108 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001109 }
1110 }
Hanno Becker92231322018-01-03 15:32:51 +00001111#else
1112 ((void) mac_dec);
1113 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001115
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001116#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1117 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001118 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001119 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001120 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001121 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001122 iv_copy_len );
1123 }
1124#endif
1125
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001126 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 return( ret );
1131 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001132
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001133 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001134 cipher_info ) ) != 0 )
1135 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001137 return( ret );
1138 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001141 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
1146 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001149 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001153 return( ret );
1154 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_CIPHER_MODE_CBC)
1157 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1160 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001164 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1167 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001170 return( ret );
1171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001175 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001176
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001177 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001179 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001182
Paul Bakker48916f92012-09-16 19:57:18 +00001183 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1184 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001185
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001186 if( deflateInit( &transform->ctx_deflate,
1187 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001188 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1191 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001192 }
1193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001195
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 return( 0 );
1197}
1198
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001199/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001200 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001201 *
1202 * Inputs:
1203 * - SSL/TLS minor version
1204 * - hash associated with the ciphersuite (only used by TLS 1.2)
1205 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001206 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001207 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1208 */
1209static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1210 int minor_ver,
1211 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001212{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001213#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1214 (void) hash;
1215#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001216
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001217#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001218 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001219 {
1220 handshake->tls_prf = ssl3_prf;
1221 handshake->calc_verify = ssl_calc_verify_ssl;
1222 handshake->calc_finished = ssl_calc_finished_ssl;
1223 }
1224 else
1225#endif
1226#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001227 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001228 {
1229 handshake->tls_prf = tls1_prf;
1230 handshake->calc_verify = ssl_calc_verify_tls;
1231 handshake->calc_finished = ssl_calc_finished_tls;
1232 }
1233 else
1234#endif
1235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1236#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001237 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1238 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 {
1240 handshake->tls_prf = tls_prf_sha384;
1241 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1242 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1243 }
1244 else
1245#endif
1246#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001247 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001248 {
1249 handshake->tls_prf = tls_prf_sha256;
1250 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1251 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1252 }
1253 else
1254#endif
1255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1256 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1258 }
1259
1260 return( 0 );
1261}
1262
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001263/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001264 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001265 *
1266 * Parameters:
1267 * [in/out] handshake
1268 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1269 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001270 * [out] master
1271 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001272 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001273static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001274 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001275 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276{
1277 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001278
1279#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001280 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001281 (void) ssl;
1282#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001284 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001285 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1287 return( 0 );
1288 }
1289
1290 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1291 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001292
1293#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001294 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1295 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001296 {
1297 unsigned char session_hash[48];
1298 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001299
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001300 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001301
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001302 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1303 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001304
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001305 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001306 "extended master secret",
1307 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001308 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001309 }
1310 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001311#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001312 {
1313 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1314 "master secret",
1315 handshake->randbytes, 64,
1316 master, 48 );
1317 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001318 if( ret != 0 )
1319 {
1320 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1321 return( ret );
1322 }
1323
1324 mbedtls_platform_zeroize( handshake->premaster,
1325 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001326
1327 return( 0 );
1328}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001329
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001330int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1331{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001332 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001333 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1334 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001335
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1337
1338 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001339 ret = ssl_set_handshake_prfs( ssl->handshake,
1340 ssl->minor_ver,
1341 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001342 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001343 {
1344 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001345 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001346 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001347
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001348 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001349 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001350 ssl->session_negotiate->master,
1351 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001352 if( ret != 0 )
1353 {
1354 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1355 return( ret );
1356 }
1357
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001358 /* Swap the client and server random values:
1359 * - MS derivation wanted client+server (RFC 5246 8.1)
1360 * - key derivation wants server+client (RFC 5246 6.3) */
1361 {
1362 unsigned char tmp[64];
1363 memcpy( tmp, ssl->handshake->randbytes, 64 );
1364 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1365 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1366 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1367 }
1368
1369 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001370 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001371 ssl->session_negotiate->ciphersuite,
1372 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001373#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001374#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1375 ssl->session_negotiate->encrypt_then_mac,
1376#endif
1377#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1378 ssl->session_negotiate->trunc_hmac,
1379#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001380#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001381#if defined(MBEDTLS_ZLIB_SUPPORT)
1382 ssl->session_negotiate->compression,
1383#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001384 ssl->handshake->tls_prf,
1385 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001386 ssl->minor_ver,
1387 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001388 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001389 if( ret != 0 )
1390 {
1391 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1392 return( ret );
1393 }
1394
1395 /* We no longer need Server/ClientHello.random values */
1396 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1397 sizeof( ssl->handshake->randbytes ) );
1398
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001399 /* Allocate compression buffer */
1400#if defined(MBEDTLS_ZLIB_SUPPORT)
1401 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1402 ssl->compress_buf == NULL )
1403 {
1404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1405 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1406 if( ssl->compress_buf == NULL )
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001409 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1411 }
1412 }
1413#endif
1414
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1416
1417 return( 0 );
1418}
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001421void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1422 unsigned char hash[36],
1423 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001424{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001425 mbedtls_md5_context md5;
1426 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 unsigned char pad_1[48];
1428 unsigned char pad_2[48];
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001432 mbedtls_md5_init( &md5 );
1433 mbedtls_sha1_init( &sha1 );
1434
1435 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1436 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Paul Bakker380da532012-04-18 16:10:25 +00001438 memset( pad_1, 0x36, 48 );
1439 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001441 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1442 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1443 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_md5_starts_ret( &md5 );
1446 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1447 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1448 mbedtls_md5_update_ret( &md5, hash, 16 );
1449 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001451 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1452 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1453 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001455 mbedtls_sha1_starts_ret( &sha1 );
1456 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1457 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1458 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1459 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001460
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001461 *hlen = 36;
1462
1463 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001465
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001466 mbedtls_md5_free( &md5 );
1467 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001468
Paul Bakker380da532012-04-18 16:10:25 +00001469 return;
1470}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1475 unsigned char hash[36],
1476 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001477{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001478 mbedtls_md5_context md5;
1479 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001482
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001483 mbedtls_md5_init( &md5 );
1484 mbedtls_sha1_init( &sha1 );
1485
1486 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1487 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001488
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001489 mbedtls_md5_finish_ret( &md5, hash );
1490 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001492 *hlen = 36;
1493
1494 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001497 mbedtls_md5_free( &md5 );
1498 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001499
Paul Bakker380da532012-04-18 16:10:25 +00001500 return;
1501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1505#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001506void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1507 unsigned char hash[32],
1508 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001509{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001510 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001512 mbedtls_sha256_init( &sha256 );
1513
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001516 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001517 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001518
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001519 *hlen = 32;
1520
1521 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001523
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001524 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001525
Paul Bakker380da532012-04-18 16:10:25 +00001526 return;
1527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001531void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1532 unsigned char hash[48],
1533 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001534{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001535 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001536
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001537 mbedtls_sha512_init( &sha512 );
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001540
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001541 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001542 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001544 *hlen = 48;
1545
1546 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001548
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001549 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001550
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 return;
1552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_SHA512_C */
1554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1557int 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 +02001558{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559 unsigned char *p = ssl->handshake->premaster;
1560 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001561 const unsigned char *psk = ssl->conf->psk;
1562 size_t psk_len = ssl->conf->psk_len;
1563
1564 /* If the psk callback was called, use its result */
1565 if( ssl->handshake->psk != NULL )
1566 {
1567 psk = ssl->handshake->psk;
1568 psk_len = ssl->handshake->psk_len;
1569 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001570
1571 /*
1572 * PMS = struct {
1573 * opaque other_secret<0..2^16-1>;
1574 * opaque psk<0..2^16-1>;
1575 * };
1576 * with "other_secret" depending on the particular key exchange
1577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1579 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001580 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001581 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001584 *(p++) = (unsigned char)( psk_len >> 8 );
1585 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001586
1587 if( end < p || (size_t)( end - p ) < psk_len )
1588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1589
1590 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001591 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592 }
1593 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1595#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1596 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001597 {
1598 /*
1599 * other_secret already set by the ClientKeyExchange message,
1600 * and is 48 bytes long
1601 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001602 if( end - p < 2 )
1603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1604
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001605 *p++ = 0;
1606 *p++ = 48;
1607 p += 48;
1608 }
1609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1611#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1612 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001614 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001615 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001617 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001619 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001620 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001623 return( ret );
1624 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001625 *(p++) = (unsigned char)( len >> 8 );
1626 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627 p += len;
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001630 }
1631 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1633#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1634 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001636 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001637 size_t zlen;
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001640 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001641 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001644 return( ret );
1645 }
1646
1647 *(p++) = (unsigned char)( zlen >> 8 );
1648 *(p++) = (unsigned char)( zlen );
1649 p += zlen;
1650
Janos Follath3fbdada2018-08-15 10:26:53 +01001651 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1652 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001653 }
1654 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001659 }
1660
1661 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001662 if( end - p < 2 )
1663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001664
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001665 *(p++) = (unsigned char)( psk_len >> 8 );
1666 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001667
1668 if( end < p || (size_t)( end - p ) < psk_len )
1669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1670
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001671 memcpy( p, psk, psk_len );
1672 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001673
1674 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1675
1676 return( 0 );
1677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001681/*
1682 * SSLv3.0 MAC functions
1683 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001684#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001685static void ssl_mac( mbedtls_md_context_t *md_ctx,
1686 const unsigned char *secret,
1687 const unsigned char *buf, size_t len,
1688 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001689 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001690{
1691 unsigned char header[11];
1692 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001693 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1695 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001696
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001697 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001699 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001700 else
Paul Bakker68884e32013-01-07 18:20:04 +01001701 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
1703 memcpy( header, ctr, 8 );
1704 header[ 8] = (unsigned char) type;
1705 header[ 9] = (unsigned char)( len >> 8 );
1706 header[10] = (unsigned char)( len );
1707
Paul Bakker68884e32013-01-07 18:20:04 +01001708 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_md_starts( md_ctx );
1710 mbedtls_md_update( md_ctx, secret, md_size );
1711 mbedtls_md_update( md_ctx, padding, padlen );
1712 mbedtls_md_update( md_ctx, header, 11 );
1713 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001714 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Paul Bakker68884e32013-01-07 18:20:04 +01001716 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_md_starts( md_ctx );
1718 mbedtls_md_update( md_ctx, secret, md_size );
1719 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001720 mbedtls_md_update( md_ctx, out, md_size );
1721 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001724
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001725/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001726 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001727#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001728 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1729 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1730 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1731/* This function makes sure every byte in the memory region is accessed
1732 * (in ascending addresses order) */
1733static void ssl_read_memory( unsigned char *p, size_t len )
1734{
1735 unsigned char acc = 0;
1736 volatile unsigned char force;
1737
1738 for( ; len != 0; p++, len-- )
1739 acc ^= *p;
1740
1741 force = acc;
1742 (void) force;
1743}
1744#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1745
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001746/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001748 */
Hanno Becker3307b532017-12-27 21:37:21 +00001749
Hanno Beckera5a2b082019-05-15 14:03:01 +01001750#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001751/* This functions transforms a DTLS plaintext fragment and a record content
1752 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001753 *
1754 * struct {
1755 * opaque content[DTLSPlaintext.length];
1756 * ContentType real_type;
1757 * uint8 zeros[length_of_padding];
1758 * } DTLSInnerPlaintext;
1759 *
1760 * Input:
1761 * - `content`: The beginning of the buffer holding the
1762 * plaintext to be wrapped.
1763 * - `*content_size`: The length of the plaintext in Bytes.
1764 * - `max_len`: The number of Bytes available starting from
1765 * `content`. This must be `>= *content_size`.
1766 * - `rec_type`: The desired record content type.
1767 *
1768 * Output:
1769 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1770 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1771 *
1772 * Returns:
1773 * - `0` on success.
1774 * - A negative error code if `max_len` didn't offer enough space
1775 * for the expansion.
1776 */
1777static int ssl_cid_build_inner_plaintext( unsigned char *content,
1778 size_t *content_size,
1779 size_t remaining,
1780 uint8_t rec_type )
1781{
1782 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001783 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1784 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1785 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001786
1787 /* Write real content type */
1788 if( remaining == 0 )
1789 return( -1 );
1790 content[ len ] = rec_type;
1791 len++;
1792 remaining--;
1793
1794 if( remaining < pad )
1795 return( -1 );
1796 memset( content + len, 0, pad );
1797 len += pad;
1798 remaining -= pad;
1799
1800 *content_size = len;
1801 return( 0 );
1802}
1803
Hanno Becker7dc25772019-05-20 15:08:01 +01001804/* This function parses a DTLSInnerPlaintext structure.
1805 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001806static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1807 size_t *content_size,
1808 uint8_t *rec_type )
1809{
1810 size_t remaining = *content_size;
1811
1812 /* Determine length of padding by skipping zeroes from the back. */
1813 do
1814 {
1815 if( remaining == 0 )
1816 return( -1 );
1817 remaining--;
1818 } while( content[ remaining ] == 0 );
1819
1820 *content_size = remaining;
1821 *rec_type = content[ remaining ];
1822
1823 return( 0 );
1824}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001825#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001826
Hanno Becker99abf512019-05-20 14:50:53 +01001827/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001828 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001829static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001830 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001831 mbedtls_record *rec )
1832{
Hanno Becker99abf512019-05-20 14:50:53 +01001833 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001834 *
1835 * additional_data = seq_num + TLSCompressed.type +
1836 * TLSCompressed.version + TLSCompressed.length;
1837 *
Hanno Becker99abf512019-05-20 14:50:53 +01001838 * For the CID extension, this is extended as follows
1839 * (quoting draft-ietf-tls-dtls-connection-id-05,
1840 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001841 *
1842 * additional_data = seq_num + DTLSPlaintext.type +
1843 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001844 * cid +
1845 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001846 * length_of_DTLSInnerPlaintext;
1847 */
1848
Hanno Becker3307b532017-12-27 21:37:21 +00001849 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1850 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001851 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001852
Hanno Beckera5a2b082019-05-15 14:03:01 +01001853#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001854 if( rec->cid_len != 0 )
1855 {
1856 memcpy( add_data + 11, rec->cid, rec->cid_len );
1857 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1858 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1859 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1860 *add_data_len = 13 + 1 + rec->cid_len;
1861 }
1862 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001863#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001864 {
1865 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1866 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1867 *add_data_len = 13;
1868 }
Hanno Becker3307b532017-12-27 21:37:21 +00001869}
1870
Hanno Becker611a83b2018-01-03 14:27:32 +00001871int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1872 mbedtls_ssl_transform *transform,
1873 mbedtls_record *rec,
1874 int (*f_rng)(void *, unsigned char *, size_t),
1875 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001876{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001878 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001879 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001880 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001881 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001882 size_t post_avail;
1883
1884 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001885#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001886 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001887 ((void) ssl);
1888#endif
1889
1890 /* The PRNG is used for dynamic IV generation that's used
1891 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1892#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1893 ( defined(MBEDTLS_AES_C) || \
1894 defined(MBEDTLS_ARIA_C) || \
1895 defined(MBEDTLS_CAMELLIA_C) ) && \
1896 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1897 ((void) f_rng);
1898 ((void) p_rng);
1899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001902
Hanno Becker3307b532017-12-27 21:37:21 +00001903 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001904 {
Hanno Becker3307b532017-12-27 21:37:21 +00001905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1907 }
Hanno Becker505089d2019-05-01 09:45:57 +01001908 if( rec == NULL
1909 || rec->buf == NULL
1910 || rec->buf_len < rec->data_offset
1911 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001913 || rec->cid_len != 0
1914#endif
1915 )
Hanno Becker3307b532017-12-27 21:37:21 +00001916 {
1917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001919 }
1920
Hanno Becker3307b532017-12-27 21:37:21 +00001921 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001922 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001924 data, rec->data_len );
1925
1926 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1927
1928 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1929 {
1930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1931 (unsigned) rec->data_len,
1932 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1933 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1934 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001935
Hanno Beckera5a2b082019-05-15 14:03:01 +01001936#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001937 /*
1938 * Add CID information
1939 */
1940 rec->cid_len = transform->out_cid_len;
1941 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1942 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001943
1944 if( rec->cid_len != 0 )
1945 {
1946 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001947 * Wrap plaintext into DTLSInnerPlaintext structure.
1948 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001949 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001950 * Note that this changes `rec->data_len`, and hence
1951 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001952 */
1953 if( ssl_cid_build_inner_plaintext( data,
1954 &rec->data_len,
1955 post_avail,
1956 rec->type ) != 0 )
1957 {
1958 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1959 }
1960
1961 rec->type = MBEDTLS_SSL_MSG_CID;
1962 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001963#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001964
Hanno Becker92c930f2019-04-29 17:31:37 +01001965 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1966
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001968 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001970#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971 if( mode == MBEDTLS_MODE_STREAM ||
1972 ( mode == MBEDTLS_MODE_CBC
1973#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001974 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001975#endif
1976 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001977 {
Hanno Becker3307b532017-12-27 21:37:21 +00001978 if( post_avail < transform->maclen )
1979 {
1980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1981 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1982 }
1983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001985 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001987 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001988 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1989 data, rec->data_len, rec->ctr, rec->type, mac );
1990 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001991 }
1992 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001993#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1995 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001996 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001997 {
Hanno Becker992b6872017-11-09 18:57:39 +00001998 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1999
Hanno Beckere83efe62019-04-29 13:52:53 +01002000 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002001
Hanno Becker3307b532017-12-27 21:37:21 +00002002 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002003 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002004 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2005 data, rec->data_len );
2006 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2007 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2008
2009 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002010 }
2011 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002012#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2015 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002016 }
2017
Hanno Becker3307b532017-12-27 21:37:21 +00002018 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2019 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002020
Hanno Becker3307b532017-12-27 21:37:21 +00002021 rec->data_len += transform->maclen;
2022 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002023 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002024 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002025#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002027 /*
2028 * Encrypt
2029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2031 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002033 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002034 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002036 "including %d bytes of padding",
2037 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002038
Hanno Becker3307b532017-12-27 21:37:21 +00002039 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2040 transform->iv_enc, transform->ivlen,
2041 data, rec->data_len,
2042 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002045 return( ret );
2046 }
2047
Hanno Becker3307b532017-12-27 21:37:21 +00002048 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002052 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 }
Paul Bakker68884e32013-01-07 18:20:04 +01002054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002056
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002057#if defined(MBEDTLS_GCM_C) || \
2058 defined(MBEDTLS_CCM_C) || \
2059 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002061 mode == MBEDTLS_MODE_CCM ||
2062 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002063 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002064 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002066 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002067
Hanno Becker3307b532017-12-27 21:37:21 +00002068 /* Check that there's space for both the authentication tag
2069 * and the explicit IV before and after the record content. */
2070 if( post_avail < transform->taglen ||
2071 rec->data_offset < explicit_iv_len )
2072 {
2073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2074 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2075 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002076
Paul Bakker68884e32013-01-07 18:20:04 +01002077 /*
2078 * Generate IV
2079 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002080 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2081 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002082 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002083 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002084 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2085 explicit_iv_len );
2086 /* Prefix record content with explicit IV. */
2087 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002088 }
2089 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2090 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002091 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 unsigned char i;
2093
2094 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2095
2096 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002097 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 }
2099 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002100 {
2101 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2103 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002104 }
2105
Hanno Beckere83efe62019-04-29 13:52:53 +01002106 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002107
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002108 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2109 iv, transform->ivlen );
2110 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002111 data - explicit_iv_len, explicit_iv_len );
2112 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002113 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002115 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002116 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002117
Paul Bakker68884e32013-01-07 18:20:04 +01002118 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002119 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002120 */
Hanno Becker3307b532017-12-27 21:37:21 +00002121
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002122 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002123 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002124 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002125 data, rec->data_len, /* source */
2126 data, &rec->data_len, /* destination */
2127 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002130 return( ret );
2131 }
2132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2134 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002135
Hanno Becker3307b532017-12-27 21:37:21 +00002136 rec->data_len += transform->taglen + explicit_iv_len;
2137 rec->data_offset -= explicit_iv_len;
2138 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002139 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002140 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2143#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002144 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002147 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002148 size_t padlen, i;
2149 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150
Hanno Becker3307b532017-12-27 21:37:21 +00002151 /* Currently we're always using minimal padding
2152 * (up to 255 bytes would be allowed). */
2153 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2154 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 padlen = 0;
2156
Hanno Becker3307b532017-12-27 21:37:21 +00002157 /* Check there's enough space in the buffer for the padding. */
2158 if( post_avail < padlen + 1 )
2159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2161 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2162 }
2163
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002165 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Hanno Becker3307b532017-12-27 21:37:21 +00002167 rec->data_len += padlen + 1;
2168 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002172 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2173 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174 */
Hanno Becker3307b532017-12-27 21:37:21 +00002175 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002176 {
Hanno Becker3307b532017-12-27 21:37:21 +00002177 if( f_rng == NULL )
2178 {
2179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2181 }
2182
2183 if( rec->data_offset < transform->ivlen )
2184 {
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2186 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2187 }
2188
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002189 /*
2190 * Generate IV
2191 */
Hanno Becker3307b532017-12-27 21:37:21 +00002192 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002193 if( ret != 0 )
2194 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002195
Hanno Becker3307b532017-12-27 21:37:21 +00002196 memcpy( data - transform->ivlen, transform->iv_enc,
2197 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002198
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002199 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002203 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002204 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002205 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
Hanno Becker3307b532017-12-27 21:37:21 +00002207 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2208 transform->iv_enc,
2209 transform->ivlen,
2210 data, rec->data_len,
2211 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002214 return( ret );
2215 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002216
Hanno Becker3307b532017-12-27 21:37:21 +00002217 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002221 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002224 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002225 {
2226 /*
2227 * Save IV in SSL3 and TLS1
2228 */
Hanno Becker3307b532017-12-27 21:37:21 +00002229 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2230 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 }
Hanno Becker3307b532017-12-27 21:37:21 +00002232 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002233#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002234 {
2235 data -= transform->ivlen;
2236 rec->data_offset -= transform->ivlen;
2237 rec->data_len += transform->ivlen;
2238 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002242 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002243 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2244
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002245 /*
2246 * MAC(MAC_write_key, seq_num +
2247 * TLSCipherText.type +
2248 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002249 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250 * IV + // except for TLS 1.0
2251 * ENC(content + padding + padding_length));
2252 */
Hanno Becker3307b532017-12-27 21:37:21 +00002253
2254 if( post_avail < transform->maclen)
2255 {
2256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2257 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2258 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002259
Hanno Beckere83efe62019-04-29 13:52:53 +01002260 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002263 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002264 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265
Hanno Becker3307b532017-12-27 21:37:21 +00002266 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002267 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002268 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2269 data, rec->data_len );
2270 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2271 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002272
Hanno Becker3307b532017-12-27 21:37:21 +00002273 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002274
Hanno Becker3307b532017-12-27 21:37:21 +00002275 rec->data_len += transform->maclen;
2276 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002277 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002279#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002281 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002283 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002288
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002289 /* Make extra sure authentication was performed, exactly once */
2290 if( auth_done != 1 )
2291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002294 }
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
2298 return( 0 );
2299}
2300
Hanno Becker611a83b2018-01-03 14:27:32 +00002301int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2302 mbedtls_ssl_transform *transform,
2303 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002304{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002305 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002307 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002308#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002309 size_t padlen = 0, correct = 1;
2310#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002311 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002312 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002313 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002314
Hanno Becker611a83b2018-01-03 14:27:32 +00002315#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002316 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002317 ((void) ssl);
2318#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002321 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002322 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2324 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2325 }
2326 if( rec == NULL ||
2327 rec->buf == NULL ||
2328 rec->buf_len < rec->data_offset ||
2329 rec->buf_len - rec->data_offset < rec->data_len )
2330 {
2331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002333 }
2334
Hanno Becker4c6876b2017-12-27 21:28:58 +00002335 data = rec->buf + rec->data_offset;
2336 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Hanno Beckera5a2b082019-05-15 14:03:01 +01002338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002339 /*
2340 * Match record's CID with incoming CID.
2341 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002342 if( rec->cid_len != transform->in_cid_len ||
2343 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2344 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002345 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002346 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2350 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002351 {
2352 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002353 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2354 transform->iv_dec,
2355 transform->ivlen,
2356 data, rec->data_len,
2357 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002360 return( ret );
2361 }
2362
Hanno Becker4c6876b2017-12-27 21:28:58 +00002363 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2366 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002367 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002368 }
Paul Bakker68884e32013-01-07 18:20:04 +01002369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371#if defined(MBEDTLS_GCM_C) || \
2372 defined(MBEDTLS_CCM_C) || \
2373 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002375 mode == MBEDTLS_MODE_CCM ||
2376 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002377 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002378 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002379 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002380
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002381 /*
2382 * Compute and update sizes
2383 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002387 "+ taglen (%d)", rec->data_len,
2388 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002390 }
Paul Bakker68884e32013-01-07 18:20:04 +01002391
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002392 /*
2393 * Prepare IV
2394 */
2395 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2396 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002397 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002398 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002399 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002400
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002401 }
2402 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2403 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002404 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 unsigned char i;
2406
2407 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2408
2409 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002410 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002411 }
2412 else
2413 {
2414 /* Reminder if we ever add an AEAD mode with a different size */
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2417 }
2418
Hanno Becker4c6876b2017-12-27 21:28:58 +00002419 data += explicit_iv_len;
2420 rec->data_offset += explicit_iv_len;
2421 rec->data_len -= explicit_iv_len + transform->taglen;
2422
Hanno Beckere83efe62019-04-29 13:52:53 +01002423 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002424 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002425 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002426
2427 memcpy( transform->iv_dec + transform->fixed_ivlen,
2428 data - explicit_iv_len, explicit_iv_len );
2429
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002430 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002431 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002432 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002433
Paul Bakker68884e32013-01-07 18:20:04 +01002434
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002436 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002437 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002438 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2439 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002440 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002441 data, rec->data_len,
2442 data, &olen,
2443 data + rec->data_len,
2444 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2449 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002450
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002451 return( ret );
2452 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002453 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002454
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2458 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002459 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2463#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002464 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002466 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002467 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002468
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 /*
Paul Bakker45829992013-01-03 14:52:21 +01002470 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002472#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002473 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2474 {
2475 /* The ciphertext is prefixed with the CBC IV. */
2476 minlen += transform->ivlen;
2477 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002478#endif
Paul Bakker45829992013-01-03 14:52:21 +01002479
Hanno Becker4c6876b2017-12-27 21:28:58 +00002480 /* Size considerations:
2481 *
2482 * - The CBC cipher text must not be empty and hence
2483 * at least of size transform->ivlen.
2484 *
2485 * Together with the potential IV-prefix, this explains
2486 * the first of the two checks below.
2487 *
2488 * - The record must contain a MAC, either in plain or
2489 * encrypted, depending on whether Encrypt-then-MAC
2490 * is used or not.
2491 * - If it is, the message contains the IV-prefix,
2492 * the CBC ciphertext, and the MAC.
2493 * - If it is not, the padded plaintext, and hence
2494 * the CBC ciphertext, has at least length maclen + 1
2495 * because there is at least the padding length byte.
2496 *
2497 * As the CBC ciphertext is not empty, both cases give the
2498 * lower bound minlen + maclen + 1 on the record size, which
2499 * we test for in the second check below.
2500 */
2501 if( rec->data_len < minlen + transform->ivlen ||
2502 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002505 "+ 1 ) ( + expl IV )", rec->data_len,
2506 transform->ivlen,
2507 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002509 }
2510
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002511 /*
2512 * Authenticate before decrypt if enabled
2513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002515 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002516 {
Hanno Becker992b6872017-11-09 18:57:39 +00002517 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520
Hanno Becker4c6876b2017-12-27 21:28:58 +00002521 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2522 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523
Hanno Beckere83efe62019-04-29 13:52:53 +01002524 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002525
Hanno Beckere83efe62019-04-29 13:52:53 +01002526 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2527 add_data_len );
2528 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2529 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002530 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2531 data, rec->data_len );
2532 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2533 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002534
Hanno Becker4c6876b2017-12-27 21:28:58 +00002535 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2536 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002537 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002538 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002539
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2541 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002545 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002546 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002549
2550 /*
2551 * Check length sanity
2552 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002556 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002558 }
2559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002561 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002562 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002563 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002564 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002565 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002566 /* This is safe because data_len >= minlen + maclen + 1 initially,
2567 * and at this point we have at most subtracted maclen (note that
2568 * minlen == transform->ivlen here). */
2569 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002570
Hanno Becker4c6876b2017-12-27 21:28:58 +00002571 data += transform->ivlen;
2572 rec->data_offset += transform->ivlen;
2573 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002574 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002576
Hanno Becker4c6876b2017-12-27 21:28:58 +00002577 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2578 transform->iv_dec, transform->ivlen,
2579 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002582 return( ret );
2583 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002584
Hanno Becker4c6876b2017-12-27 21:28:58 +00002585 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002589 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002592 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002593 {
2594 /*
2595 * Save IV in SSL3 and TLS1
2596 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2598 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
Hanno Becker4c6876b2017-12-27 21:28:58 +00002602 /* Safe since data_len >= minlen + maclen + 1, so after having
2603 * subtracted at most minlen and maclen up to this point,
2604 * data_len > 0. */
2605 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002606
Hanno Becker4c6876b2017-12-27 21:28:58 +00002607 if( auth_done == 1 )
2608 {
2609 correct *= ( rec->data_len >= padlen + 1 );
2610 padlen *= ( rec->data_len >= padlen + 1 );
2611 }
2612 else
Paul Bakker45829992013-01-03 14:52:21 +01002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002615 if( rec->data_len < transform->maclen + padlen + 1 )
2616 {
2617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2618 rec->data_len,
2619 transform->maclen,
2620 padlen + 1 ) );
2621 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002622#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002623
2624 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2625 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002626 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002627
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 padlen++;
2629
2630 /* Regardless of the validity of the padding,
2631 * we have data_len >= padlen here. */
2632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002636 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638#if defined(MBEDTLS_SSL_DEBUG_ALL)
2639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002640 "should be no more than %d",
2641 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002642#endif
Paul Bakker45829992013-01-03 14:52:21 +01002643 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 }
2645 }
2646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2648#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2649 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002651 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002652 /* The padding check involves a series of up to 256
2653 * consecutive memory reads at the end of the record
2654 * plaintext buffer. In order to hide the length and
2655 * validity of the padding, always perform exactly
2656 * `min(256,plaintext_len)` reads (but take into account
2657 * only the last `padlen` bytes for the padding check). */
2658 size_t pad_count = 0;
2659 size_t real_count = 0;
2660 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002661
Hanno Becker4c6876b2017-12-27 21:28:58 +00002662 /* Index of first padding byte; it has been ensured above
2663 * that the subtraction is safe. */
2664 size_t const padding_idx = rec->data_len - padlen;
2665 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2666 size_t const start_idx = rec->data_len - num_checks;
2667 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002668
Hanno Becker4c6876b2017-12-27 21:28:58 +00002669 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002670 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002671 real_count |= ( idx >= padding_idx );
2672 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002673 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002674 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002677 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002679#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002680 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002682 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2684 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2687 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002688 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002689
Hanno Becker4c6876b2017-12-27 21:28:58 +00002690 /* If the padding was found to be invalid, padlen == 0
2691 * and the subtraction is safe. If the padding was found valid,
2692 * padlen hasn't been changed and the previous assertion
2693 * data_len >= padlen still holds. */
2694 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002696 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002698 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2701 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002703
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002704#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002706 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002707#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002710 * Authenticate if not done yet.
2711 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002713#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002714 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002715 {
Hanno Becker992b6872017-11-09 18:57:39 +00002716 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002717
Hanno Becker4c6876b2017-12-27 21:28:58 +00002718 /* If the initial value of padlen was such that
2719 * data_len < maclen + padlen + 1, then padlen
2720 * got reset to 1, and the initial check
2721 * data_len >= minlen + maclen + 1
2722 * guarantees that at this point we still
2723 * have at least data_len >= maclen.
2724 *
2725 * If the initial value of padlen was such that
2726 * data_len >= maclen + padlen + 1, then we have
2727 * subtracted either padlen + 1 (if the padding was correct)
2728 * or 0 (if the padding was incorrect) since then,
2729 * hence data_len >= maclen in any case.
2730 */
2731 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Hanno Beckere83efe62019-04-29 13:52:53 +01002733 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002736 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002737 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002738 ssl_mac( &transform->md_ctx_dec,
2739 transform->mac_dec,
2740 data, rec->data_len,
2741 rec->ctr, rec->type,
2742 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002743 }
2744 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2746#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2747 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002748 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002749 {
2750 /*
2751 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002752 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002753 *
2754 * Known timing attacks:
2755 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2756 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002757 * To compensate for different timings for the MAC calculation
2758 * depending on how much padding was removed (which is determined
2759 * by padlen), process extra_run more blocks through the hash
2760 * function.
2761 *
2762 * The formula in the paper is
2763 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2764 * where L1 is the size of the header plus the decrypted message
2765 * plus CBC padding and L2 is the size of the header plus the
2766 * decrypted message. This is for an underlying hash function
2767 * with 64-byte blocks.
2768 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2769 * correctly. We round down instead of up, so -56 is the correct
2770 * value for our calculations instead of -55.
2771 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002772 * Repeat the formula rather than defining a block_size variable.
2773 * This avoids requiring division by a variable at runtime
2774 * (which would be marginally less efficient and would require
2775 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002776 */
2777 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002778 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002779
2780 /*
2781 * The next two sizes are the minimum and maximum values of
2782 * in_msglen over all padlen values.
2783 *
2784 * They're independent of padlen, since we previously did
2785 * in_msglen -= padlen.
2786 *
2787 * Note that max_len + maclen is never more than the buffer
2788 * length, as we previously did in_msglen -= maclen too.
2789 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002791 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2792
Hanno Becker4c6876b2017-12-27 21:28:58 +00002793 memset( tmp, 0, sizeof( tmp ) );
2794
2795 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002796 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002797#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2798 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002799 case MBEDTLS_MD_MD5:
2800 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002801 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002802 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002803 extra_run =
2804 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2805 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002806 break;
2807#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002808#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002809 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002810 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002811 extra_run =
2812 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2813 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002814 break;
2815#endif
2816 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2819 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002820
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002821 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002822
Hanno Beckere83efe62019-04-29 13:52:53 +01002823 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2824 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002825 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2826 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002827 /* Make sure we access everything even when padlen > 0. This
2828 * makes the synchronisation requirements for just-in-time
2829 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002830 ssl_read_memory( data + rec->data_len, padlen );
2831 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002832
2833 /* Call mbedtls_md_process at least once due to cache attacks
2834 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002835 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002836 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002837
Hanno Becker4c6876b2017-12-27 21:28:58 +00002838 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002839
2840 /* Make sure we access all the memory that could contain the MAC,
2841 * before we check it in the next code block. This makes the
2842 * synchronisation requirements for just-in-time Prime+Probe
2843 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002844 ssl_read_memory( data + min_len,
2845 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002846 }
2847 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002848#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2849 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2852 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002854
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002855#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002856 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2857 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002858#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Hanno Becker4c6876b2017-12-27 21:28:58 +00002860 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2861 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863#if defined(MBEDTLS_SSL_DEBUG_ALL)
2864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002865#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002866 correct = 0;
2867 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002868 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002869 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002870
2871 /*
2872 * Finally check the correct flag
2873 */
2874 if( correct == 0 )
2875 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002876#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002877
2878 /* Make extra sure authentication was performed, exactly once */
2879 if( auth_done != 1 )
2880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Hanno Beckera5a2b082019-05-15 14:03:01 +01002885#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002886 if( rec->cid_len != 0 )
2887 {
2888 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2889 &rec->type );
2890 if( ret != 0 )
2891 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2892 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002893#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
2897 return( 0 );
2898}
2899
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002900#undef MAC_NONE
2901#undef MAC_PLAINTEXT
2902#undef MAC_CIPHERTEXT
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905/*
2906 * Compression/decompression functions
2907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909{
2910 int ret;
2911 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002912 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002913 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002914 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002918 if( len_pre == 0 )
2919 return( 0 );
2920
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 memcpy( msg_pre, ssl->out_msg, len_pre );
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924 ssl->out_msglen ) );
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927 ssl->out_msg, ssl->out_msglen );
2928
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2930 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2931 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002932 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933
Paul Bakker48916f92012-09-16 19:57:18 +00002934 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002935 if( ret != Z_OK )
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2938 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939 }
2940
Angus Grattond8213d02016-05-25 20:56:48 +10002941 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002942 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945 ssl->out_msglen ) );
2946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002948 ssl->out_msg, ssl->out_msglen );
2949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
2952 return( 0 );
2953}
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956{
2957 int ret;
2958 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002959 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002960 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002961 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002964
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002965 if( len_pre == 0 )
2966 return( 0 );
2967
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968 memcpy( msg_pre, ssl->in_msg, len_pre );
2969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002971 ssl->in_msglen ) );
2972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002974 ssl->in_msg, ssl->in_msglen );
2975
Paul Bakker48916f92012-09-16 19:57:18 +00002976 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2977 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2978 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002979 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002980 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002981
Paul Bakker48916f92012-09-16 19:57:18 +00002982 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002983 if( ret != Z_OK )
2984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2986 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002987 }
2988
Angus Grattond8213d02016-05-25 20:56:48 +10002989 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002990 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002993 ssl->in_msglen ) );
2994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002996 ssl->in_msg, ssl->in_msglen );
2997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002999
3000 return( 0 );
3001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003004#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3005static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003007#if defined(MBEDTLS_SSL_PROTO_DTLS)
3008static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003009{
3010 /* If renegotiation is not enforced, retransmit until we would reach max
3011 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003012 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003013 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003014 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003015 unsigned char doublings = 1;
3016
3017 while( ratio != 0 )
3018 {
3019 ++doublings;
3020 ratio >>= 1;
3021 }
3022
3023 if( ++ssl->renego_records_seen > doublings )
3024 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003026 return( 0 );
3027 }
3028 }
3029
3030 return( ssl_write_hello_request( ssl ) );
3031}
3032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003034
Paul Bakker5121ce52009-01-03 21:22:43 +00003035/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003036 * Fill the input message buffer by appending data to it.
3037 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003038 *
3039 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3040 * available (from this read and/or a previous one). Otherwise, an error code
3041 * is returned (possibly EOF or WANT_READ).
3042 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003043 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3044 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3045 * since we always read a whole datagram at once.
3046 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003047 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003048 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003050int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051{
Paul Bakker23986e52011-04-24 08:57:21 +00003052 int ret;
3053 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003057 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003060 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003062 }
3063
Angus Grattond8213d02016-05-25 20:56:48 +10003064 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003068 }
3069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003071 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003073 uint32_t timeout;
3074
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003075 /* Just to be sure */
3076 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3077 {
3078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3079 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3081 }
3082
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003083 /*
3084 * The point is, we need to always read a full datagram at once, so we
3085 * sometimes read more then requested, and handle the additional data.
3086 * It could be the rest of the current record (while fetching the
3087 * header) and/or some other records in the same datagram.
3088 */
3089
3090 /*
3091 * Move to the next record in the already read datagram if applicable
3092 */
3093 if( ssl->next_record_offset != 0 )
3094 {
3095 if( ssl->in_left < ssl->next_record_offset )
3096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003099 }
3100
3101 ssl->in_left -= ssl->next_record_offset;
3102
3103 if( ssl->in_left != 0 )
3104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003106 ssl->next_record_offset ) );
3107 memmove( ssl->in_hdr,
3108 ssl->in_hdr + ssl->next_record_offset,
3109 ssl->in_left );
3110 }
3111
3112 ssl->next_record_offset = 0;
3113 }
3114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003117
3118 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003119 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003120 */
3121 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003124 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003125 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003126
3127 /*
3128 * A record can't be split accross datagrams. If we need to read but
3129 * are not at the beginning of a new record, the caller did something
3130 * wrong.
3131 */
3132 if( ssl->in_left != 0 )
3133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3135 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003136 }
3137
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003138 /*
3139 * Don't even try to read if time's out already.
3140 * This avoids by-passing the timer when repeatedly receiving messages
3141 * that will end up being dropped.
3142 */
3143 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003144 {
3145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003146 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003147 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003148 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003149 {
Angus Grattond8213d02016-05-25 20:56:48 +10003150 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003153 timeout = ssl->handshake->retransmit_timeout;
3154 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003155 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003158
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003159 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003160 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3161 timeout );
3162 else
3163 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003166
3167 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003169 }
3170
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003171 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003174 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003177 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003178 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003181 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003182 }
3183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003187 return( ret );
3188 }
3189
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003190 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003191 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003193 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003195 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003196 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003199 return( ret );
3200 }
3201
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003202 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003205 }
3206
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 if( ret < 0 )
3208 return( ret );
3209
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003210 ssl->in_left = ret;
3211 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003212 MBEDTLS_SSL_TRANSPORT_ELSE
3213#endif /* MBEDTLS_SSL_PROTO_DTLS */
3214#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003217 ssl->in_left, nb_want ) );
3218
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003219 while( ssl->in_left < nb_want )
3220 {
3221 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003222
3223 if( ssl_check_timer( ssl ) != 0 )
3224 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3225 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003226 {
3227 if( ssl->f_recv_timeout != NULL )
3228 {
3229 ret = ssl->f_recv_timeout( ssl->p_bio,
3230 ssl->in_hdr + ssl->in_left, len,
3231 ssl->conf->read_timeout );
3232 }
3233 else
3234 {
3235 ret = ssl->f_recv( ssl->p_bio,
3236 ssl->in_hdr + ssl->in_left, len );
3237 }
3238 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003241 ssl->in_left, nb_want ) );
3242 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003243
3244 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003246
3247 if( ret < 0 )
3248 return( ret );
3249
mohammad160352aecb92018-03-28 23:41:40 -07003250 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003251 {
Darryl Green11999bb2018-03-13 15:22:58 +00003252 MBEDTLS_SSL_DEBUG_MSG( 1,
3253 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003254 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3256 }
3257
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003258 ssl->in_left += ret;
3259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003261#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 return( 0 );
3266}
3267
3268/*
3269 * Flush any data not yet written
3270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003272{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003273 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003274 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003278 if( ssl->f_send == NULL )
3279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003281 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003283 }
3284
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003285 /* Avoid incrementing counter if data is flushed */
3286 if( ssl->out_left == 0 )
3287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003289 return( 0 );
3290 }
3291
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 while( ssl->out_left > 0 )
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003295 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Hanno Becker2b1e3542018-08-06 11:19:13 +01003297 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003298 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
3302 if( ret <= 0 )
3303 return( ret );
3304
mohammad160352aecb92018-03-28 23:41:40 -07003305 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003306 {
Darryl Green11999bb2018-03-13 15:22:58 +00003307 MBEDTLS_SSL_DEBUG_MSG( 1,
3308 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003309 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003310 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3311 }
3312
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 ssl->out_left -= ret;
3314 }
3315
Hanno Becker2b1e3542018-08-06 11:19:13 +01003316#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003317 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003318 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003319 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003320 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003321 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003322#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003323#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003324 {
3325 ssl->out_hdr = ssl->out_buf + 8;
3326 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003327#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003328 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
3332 return( 0 );
3333}
3334
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003335/*
3336 * Functions to handle the DTLS retransmission state machine
3337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003339/*
3340 * Append current handshake message to current outgoing flight
3341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003343{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3346 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3347 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003348
3349 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003350 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003351 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003354 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003355 }
3356
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003357 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003361 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003362 }
3363
3364 /* Copy current handshake message with headers */
3365 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3366 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003367 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368 msg->next = NULL;
3369
3370 /* Append to the current flight */
3371 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003373 else
3374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003376 while( cur->next != NULL )
3377 cur = cur->next;
3378 cur->next = msg;
3379 }
3380
Hanno Becker3b235902018-08-06 09:54:53 +01003381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003382 return( 0 );
3383}
3384
3385/*
3386 * Free the current flight of handshake messages
3387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 mbedtls_ssl_flight_item *cur = flight;
3391 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003392
3393 while( cur != NULL )
3394 {
3395 next = cur->next;
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 mbedtls_free( cur->p );
3398 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003399
3400 cur = next;
3401 }
3402}
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3405static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003406#endif
3407
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003408/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003409 * Swap transform_out and out_ctr with the alternative ones
3410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003414 unsigned char tmp_out_ctr[8];
3415
3416 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003419 return;
3420 }
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003423
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003424 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003425 tmp_transform = ssl->transform_out;
3426 ssl->transform_out = ssl->handshake->alt_transform_out;
3427 ssl->handshake->alt_transform_out = tmp_transform;
3428
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003429 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003430 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3431 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003432 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003433
3434 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003435 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3438 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3443 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003444 }
3445 }
3446#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003447}
3448
3449/*
3450 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003451 */
3452int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3453{
3454 int ret = 0;
3455
3456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3457
3458 ret = mbedtls_ssl_flight_transmit( ssl );
3459
3460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3461
3462 return( ret );
3463}
3464
3465/*
3466 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003467 *
3468 * Need to remember the current message in case flush_output returns
3469 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003470 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003471 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003472int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003473{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003478 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003480
3481 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003482 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003483 ssl_swap_epochs( ssl );
3484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003486 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003487
3488 while( ssl->handshake->cur_msg != NULL )
3489 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003490 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003491 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003492
Hanno Beckere1dcb032018-08-17 16:47:58 +01003493 int const is_finished =
3494 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3495 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3496
Hanno Becker04da1892018-08-14 13:22:10 +01003497 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3498 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3499
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003500 /* Swap epochs before sending Finished: we can't do it after
3501 * sending ChangeCipherSpec, in case write returns WANT_READ.
3502 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003503 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003504 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003506 ssl_swap_epochs( ssl );
3507 }
3508
Hanno Becker67bc7c32018-08-06 11:33:50 +01003509 ret = ssl_get_remaining_payload_in_datagram( ssl );
3510 if( ret < 0 )
3511 return( ret );
3512 max_frag_len = (size_t) ret;
3513
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003514 /* CCS is copied as is, while HS messages may need fragmentation */
3515 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3516 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003517 if( max_frag_len == 0 )
3518 {
3519 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3520 return( ret );
3521
3522 continue;
3523 }
3524
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003525 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003526 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003528
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003529 /* Update position inside current message */
3530 ssl->handshake->cur_msg_p += cur->len;
3531 }
3532 else
3533 {
3534 const unsigned char * const p = ssl->handshake->cur_msg_p;
3535 const size_t hs_len = cur->len - 12;
3536 const size_t frag_off = p - ( cur->p + 12 );
3537 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003538 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003539
Hanno Beckere1dcb032018-08-17 16:47:58 +01003540 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003541 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003542 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003543 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003544
Hanno Becker67bc7c32018-08-06 11:33:50 +01003545 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3546 return( ret );
3547
3548 continue;
3549 }
3550 max_hs_frag_len = max_frag_len - 12;
3551
3552 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3553 max_hs_frag_len : rem_len;
3554
3555 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003556 {
3557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003558 (unsigned) cur_hs_frag_len,
3559 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003560 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003561
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003562 /* Messages are stored with handshake headers as if not fragmented,
3563 * copy beginning of headers then fill fragmentation fields.
3564 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3565 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003566
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003567 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3568 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3569 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3570
Hanno Becker67bc7c32018-08-06 11:33:50 +01003571 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3572 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3573 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003574
3575 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3576
Hanno Becker3f7b9732018-08-28 09:53:25 +01003577 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003578 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3579 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003580 ssl->out_msgtype = cur->type;
3581
3582 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003583 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003584 }
3585
3586 /* If done with the current message move to the next one if any */
3587 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3588 {
3589 if( cur->next != NULL )
3590 {
3591 ssl->handshake->cur_msg = cur->next;
3592 ssl->handshake->cur_msg_p = cur->next->p + 12;
3593 }
3594 else
3595 {
3596 ssl->handshake->cur_msg = NULL;
3597 ssl->handshake->cur_msg_p = NULL;
3598 }
3599 }
3600
3601 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003602 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 return( ret );
3606 }
3607 }
3608
Hanno Becker67bc7c32018-08-06 11:33:50 +01003609 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3610 return( ret );
3611
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003612 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3614 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003615 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003618 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3619 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003620
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003622
3623 return( 0 );
3624}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003625
3626/*
3627 * To be called when the last message of an incoming flight is received.
3628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003629void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003630{
3631 /* We won't need to resend that one any more */
3632 ssl_flight_free( ssl->handshake->flight );
3633 ssl->handshake->flight = NULL;
3634 ssl->handshake->cur_msg = NULL;
3635
3636 /* The next incoming flight will start with this msg_seq */
3637 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3638
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003639 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003640 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003641
Hanno Becker0271f962018-08-16 13:23:47 +01003642 /* Clear future message buffering structure. */
3643 ssl_buffering_free( ssl );
3644
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003645 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003646 ssl_set_timer( ssl, 0 );
3647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3649 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003652 }
3653 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003656
3657/*
3658 * To be called when the last message of an outgoing flight is send.
3659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003661{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003662 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003663 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3666 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003669 }
3670 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003674
Paul Bakker5121ce52009-01-03 21:22:43 +00003675/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003676 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003678
3679/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003680 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003681 *
3682 * - fill in handshake headers
3683 * - update handshake checksum
3684 * - DTLS: save message for resending
3685 * - then pass to the record layer
3686 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003687 * DTLS: except for HelloRequest, messages are only queued, and will only be
3688 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003689 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003690 * Inputs:
3691 * - ssl->out_msglen: 4 + actual handshake message len
3692 * (4 is the size of handshake headers for TLS)
3693 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3694 * - ssl->out_msg + 4: the handshake message body
3695 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003696 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003697 * - ssl->out_msglen: the length of the record contents
3698 * (including handshake headers but excluding record headers)
3699 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003700 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003702{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003703 int ret;
3704 const size_t hs_len = ssl->out_msglen - 4;
3705 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003706
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3708
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003709 /*
3710 * Sanity checks
3711 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003712 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003713 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3714 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003715 /* In SSLv3, the client might send a NoCertificate alert. */
3716#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3717 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3718 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3719 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3720#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3721 {
3722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3724 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003727 /* Whenever we send anything different from a
3728 * HelloRequest we should be in a handshake - double check. */
3729 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3730 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003731 ssl->handshake == NULL )
3732 {
3733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003738 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003739 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003741 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003744 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003745#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003746
Hanno Beckerb50a2532018-08-06 11:52:54 +01003747 /* Double-check that we did not exceed the bounds
3748 * of the outgoing record buffer.
3749 * This should never fail as the various message
3750 * writing functions must obey the bounds of the
3751 * outgoing record buffer, but better be safe.
3752 *
3753 * Note: We deliberately do not check for the MTU or MFL here.
3754 */
3755 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3756 {
3757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3758 "size %u, maximum %u",
3759 (unsigned) ssl->out_msglen,
3760 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3761 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3762 }
3763
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003764 /*
3765 * Fill handshake headers
3766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003767 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3770 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3771 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003773 /*
3774 * DTLS has additional fields in the Handshake layer,
3775 * between the length field and the actual payload:
3776 * uint16 message_seq;
3777 * uint24 fragment_offset;
3778 * uint24 fragment_length;
3779 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003780#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003781 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003782 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003783 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003784 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003785 {
3786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3787 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003788 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003789 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003790 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3791 }
3792
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003794 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003795
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003796 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003797 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003798 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003799 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3800 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3801 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003802 }
3803 else
3804 {
3805 ssl->out_msg[4] = 0;
3806 ssl->out_msg[5] = 0;
3807 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003808
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003809 /* Handshake hashes are computed without fragmentation,
3810 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003811 memset( ssl->out_msg + 6, 0x00, 3 );
3812 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003813 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003815
Hanno Becker0207e532018-08-28 10:28:28 +01003816 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003817 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3818 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 }
3820
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003823 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003824 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3825 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826 {
3827 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830 return( ret );
3831 }
3832 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003833 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003834#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003835 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003836 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003837 {
3838 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3839 return( ret );
3840 }
3841 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003842
3843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3844
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003845 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003846}
3847
3848/*
3849 * Record layer functions
3850 */
3851
3852/*
3853 * Write current record.
3854 *
3855 * Uses:
3856 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3857 * - ssl->out_msglen: length of the record content (excl headers)
3858 * - ssl->out_msg: record content
3859 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003860int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003861{
3862 int ret, done = 0;
3863 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003864 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003865
3866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003869 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003871 {
3872 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003875 return( ret );
3876 }
3877
3878 len = ssl->out_msglen;
3879 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3883 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 ret = mbedtls_ssl_hw_record_write( ssl );
3888 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3891 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003892 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003893
3894 if( ret == 0 )
3895 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003896 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003898 if( !done )
3899 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003900 unsigned i;
3901 size_t protected_record_size;
3902
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003903 /* Skip writing the record content type to after the encryption,
3904 * as it may change when using the CID extension. */
3905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003907 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003908
Hanno Becker19859472018-08-06 09:40:20 +01003909 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003910 ssl->out_len[0] = (unsigned char)( len >> 8 );
3911 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003912
Paul Bakker48916f92012-09-16 19:57:18 +00003913 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003914 {
Hanno Becker3307b532017-12-27 21:37:21 +00003915 mbedtls_record rec;
3916
3917 rec.buf = ssl->out_iv;
3918 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3919 ( ssl->out_iv - ssl->out_buf );
3920 rec.data_len = ssl->out_msglen;
3921 rec.data_offset = ssl->out_msg - rec.buf;
3922
3923 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3924 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3925 ssl->conf->transport, rec.ver );
3926 rec.type = ssl->out_msgtype;
3927
Hanno Beckera5a2b082019-05-15 14:03:01 +01003928#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003929 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003930 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003931#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003932
Hanno Becker611a83b2018-01-03 14:27:32 +00003933 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003934 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003937 return( ret );
3938 }
3939
Hanno Becker3307b532017-12-27 21:37:21 +00003940 if( rec.data_offset != 0 )
3941 {
3942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3943 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3944 }
3945
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003946 /* Update the record content type and CID. */
3947 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003948#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003949 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003950#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003951 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003952 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3953 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003954 }
3955
Hanno Becker43395762019-05-03 14:46:38 +01003956 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003957
3958#if defined(MBEDTLS_SSL_PROTO_DTLS)
3959 /* In case of DTLS, double-check that we don't exceed
3960 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003961 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003962 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003963 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003964 if( ret < 0 )
3965 return( ret );
3966
3967 if( protected_record_size > (size_t) ret )
3968 {
3969 /* Should never happen */
3970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3971 }
3972 }
3973#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003974
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003975 /* Now write the potentially updated record content type. */
3976 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003979 "version = [%d:%d], msglen = %d",
3980 ssl->out_hdr[0], ssl->out_hdr[1],
3981 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003984 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003985
3986 ssl->out_left += protected_record_size;
3987 ssl->out_hdr += protected_record_size;
3988 ssl_update_out_pointers( ssl, ssl->transform_out );
3989
Hanno Becker04484622018-08-06 09:49:38 +01003990 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3991 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3992 break;
3993
3994 /* The loop goes to its end iff the counter is wrapping */
3995 if( i == ssl_ep_len( ssl ) )
3996 {
3997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3998 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3999 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004000 }
4001
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004003 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004004 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004005 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004006 size_t remaining;
4007 ret = ssl_get_remaining_payload_in_datagram( ssl );
4008 if( ret < 0 )
4009 {
4010 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4011 ret );
4012 return( ret );
4013 }
4014
4015 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004016 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004017 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004018 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004019 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004020 else
4021 {
Hanno Becker513815a2018-08-20 11:56:09 +01004022 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004023 }
4024 }
4025#endif /* MBEDTLS_SSL_PROTO_DTLS */
4026
4027 if( ( flush == SSL_FORCE_FLUSH ) &&
4028 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031 return( ret );
4032 }
4033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035
4036 return( 0 );
4037}
4038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004040
4041static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4042{
4043 if( ssl->in_msglen < ssl->in_hslen ||
4044 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4045 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4046 {
4047 return( 1 );
4048 }
4049 return( 0 );
4050}
Hanno Becker44650b72018-08-16 12:51:11 +01004051
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004052static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004053{
4054 return( ( ssl->in_msg[9] << 16 ) |
4055 ( ssl->in_msg[10] << 8 ) |
4056 ssl->in_msg[11] );
4057}
4058
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004059static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004060{
4061 return( ( ssl->in_msg[6] << 16 ) |
4062 ( ssl->in_msg[7] << 8 ) |
4063 ssl->in_msg[8] );
4064}
4065
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004066static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004067{
4068 uint32_t msg_len, frag_off, frag_len;
4069
4070 msg_len = ssl_get_hs_total_len( ssl );
4071 frag_off = ssl_get_hs_frag_off( ssl );
4072 frag_len = ssl_get_hs_frag_len( ssl );
4073
4074 if( frag_off > msg_len )
4075 return( -1 );
4076
4077 if( frag_len > msg_len - frag_off )
4078 return( -1 );
4079
4080 if( frag_len + 12 > ssl->in_msglen )
4081 return( -1 );
4082
4083 return( 0 );
4084}
4085
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004086/*
4087 * Mark bits in bitmask (used for DTLS HS reassembly)
4088 */
4089static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4090{
4091 unsigned int start_bits, end_bits;
4092
4093 start_bits = 8 - ( offset % 8 );
4094 if( start_bits != 8 )
4095 {
4096 size_t first_byte_idx = offset / 8;
4097
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004098 /* Special case */
4099 if( len <= start_bits )
4100 {
4101 for( ; len != 0; len-- )
4102 mask[first_byte_idx] |= 1 << ( start_bits - len );
4103
4104 /* Avoid potential issues with offset or len becoming invalid */
4105 return;
4106 }
4107
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004108 offset += start_bits; /* Now offset % 8 == 0 */
4109 len -= start_bits;
4110
4111 for( ; start_bits != 0; start_bits-- )
4112 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4113 }
4114
4115 end_bits = len % 8;
4116 if( end_bits != 0 )
4117 {
4118 size_t last_byte_idx = ( offset + len ) / 8;
4119
4120 len -= end_bits; /* Now len % 8 == 0 */
4121
4122 for( ; end_bits != 0; end_bits-- )
4123 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4124 }
4125
4126 memset( mask + offset / 8, 0xFF, len / 8 );
4127}
4128
4129/*
4130 * Check that bitmask is full
4131 */
4132static int ssl_bitmask_check( unsigned char *mask, size_t len )
4133{
4134 size_t i;
4135
4136 for( i = 0; i < len / 8; i++ )
4137 if( mask[i] != 0xFF )
4138 return( -1 );
4139
4140 for( i = 0; i < len % 8; i++ )
4141 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4142 return( -1 );
4143
4144 return( 0 );
4145}
4146
Hanno Becker56e205e2018-08-16 09:06:12 +01004147/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004148static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004149 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004150{
Hanno Becker56e205e2018-08-16 09:06:12 +01004151 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004152
Hanno Becker56e205e2018-08-16 09:06:12 +01004153 alloc_len = 12; /* Handshake header */
4154 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004155
Hanno Beckerd07df862018-08-16 09:14:58 +01004156 if( add_bitmap )
4157 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004158
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004159 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004160}
Hanno Becker56e205e2018-08-16 09:06:12 +01004161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004163
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004164static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004165{
4166 return( ( ssl->in_msg[1] << 16 ) |
4167 ( ssl->in_msg[2] << 8 ) |
4168 ssl->in_msg[3] );
4169}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004170
Simon Butcher99000142016-10-13 17:21:01 +01004171int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004172{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004176 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004177 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004178 }
4179
Hanno Becker12555c62018-08-16 12:47:53 +01004180 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004183 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004184 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004187 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004188 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004189 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004190 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004191
Hanno Becker44650b72018-08-16 12:51:11 +01004192 if( ssl_check_hs_header( ssl ) != 0 )
4193 {
4194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4195 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4196 }
4197
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004198 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004199 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4200 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4201 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4202 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004203 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004204 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4205 {
4206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4207 recv_msg_seq,
4208 ssl->handshake->in_msg_seq ) );
4209 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4210 }
4211
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004212 /* Retransmit only on last message from previous flight, to avoid
4213 * too many retransmissions.
4214 * Besides, No sane server ever retransmits HelloVerifyRequest */
4215 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004219 "message_seq = %d, start_of_flight = %d",
4220 recv_msg_seq,
4221 ssl->handshake->in_flight_start_seq ) );
4222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004226 return( ret );
4227 }
4228 }
4229 else
4230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004232 "message_seq = %d, expected = %d",
4233 recv_msg_seq,
4234 ssl->handshake->in_msg_seq ) );
4235 }
4236
Hanno Becker90333da2017-10-10 11:27:13 +01004237 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004238 }
4239 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004240
Hanno Becker6d97ef52018-08-16 13:09:04 +01004241 /* Message reassembly is handled alongside buffering of future
4242 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004243 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004244 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004245 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004247 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004248 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004249 }
4250 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004251 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004252#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004253#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004254 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004255 /* With TLS we don't handle fragmentation (for now) */
4256 if( ssl->in_msglen < ssl->in_hslen )
4257 {
4258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4259 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4260 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004261 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004262#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004263
Simon Butcher99000142016-10-13 17:21:01 +01004264 return( 0 );
4265}
4266
4267void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4268{
Hanno Becker0271f962018-08-16 13:23:47 +01004269 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004270
Hanno Becker0271f962018-08-16 13:23:47 +01004271 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004272 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004273 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004274 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004275
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004276 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004278 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004279 ssl->handshake != NULL )
4280 {
Hanno Becker0271f962018-08-16 13:23:47 +01004281 unsigned offset;
4282 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004283
Hanno Becker0271f962018-08-16 13:23:47 +01004284 /* Increment handshake sequence number */
4285 hs->in_msg_seq++;
4286
4287 /*
4288 * Clear up handshake buffering and reassembly structure.
4289 */
4290
4291 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004292 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004293
4294 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004295 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4296 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004297 offset++, hs_buf++ )
4298 {
4299 *hs_buf = *(hs_buf + 1);
4300 }
4301
4302 /* Create a fresh last entry */
4303 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004304 }
4305#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004306}
4307
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004308/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004309 * DTLS anti-replay: RFC 6347 4.1.2.6
4310 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004311 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4312 * Bit n is set iff record number in_window_top - n has been seen.
4313 *
4314 * Usually, in_window_top is the last record number seen and the lsb of
4315 * in_window is set. The only exception is the initial state (record number 0
4316 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004318#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4319static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004320{
4321 ssl->in_window_top = 0;
4322 ssl->in_window = 0;
4323}
4324
4325static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4326{
4327 return( ( (uint64_t) buf[0] << 40 ) |
4328 ( (uint64_t) buf[1] << 32 ) |
4329 ( (uint64_t) buf[2] << 24 ) |
4330 ( (uint64_t) buf[3] << 16 ) |
4331 ( (uint64_t) buf[4] << 8 ) |
4332 ( (uint64_t) buf[5] ) );
4333}
4334
4335/*
4336 * Return 0 if sequence number is acceptable, -1 otherwise
4337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339{
4340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4341 uint64_t bit;
4342
Hanno Becker7f376f42019-06-12 16:20:48 +01004343 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4344 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4345 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004346 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004347 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004348
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004349 if( rec_seqnum > ssl->in_window_top )
4350 return( 0 );
4351
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004352 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004353
4354 if( bit >= 64 )
4355 return( -1 );
4356
4357 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4358 return( -1 );
4359
4360 return( 0 );
4361}
4362
4363/*
4364 * Update replay window on new validated record
4365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004367{
4368 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4369
Hanno Becker7f376f42019-06-12 16:20:48 +01004370 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4371 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4372 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004373 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004374 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004375
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004376 if( rec_seqnum > ssl->in_window_top )
4377 {
4378 /* Update window_top and the contents of the window */
4379 uint64_t shift = rec_seqnum - ssl->in_window_top;
4380
4381 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004382 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004383 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004384 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004385 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004386 ssl->in_window |= 1;
4387 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004388
4389 ssl->in_window_top = rec_seqnum;
4390 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004391 else
4392 {
4393 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004394 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004395
4396 if( bit < 64 ) /* Always true, but be extra sure */
4397 ssl->in_window |= (uint64_t) 1 << bit;
4398 }
4399}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004401
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004402#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004403/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004404static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4405
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004406/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004407 * Without any SSL context, check if a datagram looks like a ClientHello with
4408 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004409 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004410 *
4411 * - if cookie is valid, return 0
4412 * - if ClientHello looks superficially valid but cookie is not,
4413 * fill obuf and set olen, then
4414 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4415 * - otherwise return a specific error code
4416 */
4417static int ssl_check_dtls_clihlo_cookie(
4418 mbedtls_ssl_cookie_write_t *f_cookie_write,
4419 mbedtls_ssl_cookie_check_t *f_cookie_check,
4420 void *p_cookie,
4421 const unsigned char *cli_id, size_t cli_id_len,
4422 const unsigned char *in, size_t in_len,
4423 unsigned char *obuf, size_t buf_len, size_t *olen )
4424{
4425 size_t sid_len, cookie_len;
4426 unsigned char *p;
4427
4428 if( f_cookie_write == NULL || f_cookie_check == NULL )
4429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4430
4431 /*
4432 * Structure of ClientHello with record and handshake headers,
4433 * and expected values. We don't need to check a lot, more checks will be
4434 * done when actually parsing the ClientHello - skipping those checks
4435 * avoids code duplication and does not make cookie forging any easier.
4436 *
4437 * 0-0 ContentType type; copied, must be handshake
4438 * 1-2 ProtocolVersion version; copied
4439 * 3-4 uint16 epoch; copied, must be 0
4440 * 5-10 uint48 sequence_number; copied
4441 * 11-12 uint16 length; (ignored)
4442 *
4443 * 13-13 HandshakeType msg_type; (ignored)
4444 * 14-16 uint24 length; (ignored)
4445 * 17-18 uint16 message_seq; copied
4446 * 19-21 uint24 fragment_offset; copied, must be 0
4447 * 22-24 uint24 fragment_length; (ignored)
4448 *
4449 * 25-26 ProtocolVersion client_version; (ignored)
4450 * 27-58 Random random; (ignored)
4451 * 59-xx SessionID session_id; 1 byte len + sid_len content
4452 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4453 * ...
4454 *
4455 * Minimum length is 61 bytes.
4456 */
4457 if( in_len < 61 ||
4458 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4459 in[3] != 0 || in[4] != 0 ||
4460 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4461 {
4462 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4463 }
4464
4465 sid_len = in[59];
4466 if( sid_len > in_len - 61 )
4467 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4468
4469 cookie_len = in[60 + sid_len];
4470 if( cookie_len > in_len - 60 )
4471 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4472
4473 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4474 cli_id, cli_id_len ) == 0 )
4475 {
4476 /* Valid cookie */
4477 return( 0 );
4478 }
4479
4480 /*
4481 * If we get here, we've got an invalid cookie, let's prepare HVR.
4482 *
4483 * 0-0 ContentType type; copied
4484 * 1-2 ProtocolVersion version; copied
4485 * 3-4 uint16 epoch; copied
4486 * 5-10 uint48 sequence_number; copied
4487 * 11-12 uint16 length; olen - 13
4488 *
4489 * 13-13 HandshakeType msg_type; hello_verify_request
4490 * 14-16 uint24 length; olen - 25
4491 * 17-18 uint16 message_seq; copied
4492 * 19-21 uint24 fragment_offset; copied
4493 * 22-24 uint24 fragment_length; olen - 25
4494 *
4495 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4496 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4497 *
4498 * Minimum length is 28.
4499 */
4500 if( buf_len < 28 )
4501 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4502
4503 /* Copy most fields and adapt others */
4504 memcpy( obuf, in, 25 );
4505 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4506 obuf[25] = 0xfe;
4507 obuf[26] = 0xff;
4508
4509 /* Generate and write actual cookie */
4510 p = obuf + 28;
4511 if( f_cookie_write( p_cookie,
4512 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4513 {
4514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4515 }
4516
4517 *olen = p - obuf;
4518
4519 /* Go back and fill length fields */
4520 obuf[27] = (unsigned char)( *olen - 28 );
4521
4522 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4523 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4524 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4525
4526 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4527 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4528
4529 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4530}
4531
4532/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533 * Handle possible client reconnect with the same UDP quadruplet
4534 * (RFC 6347 Section 4.2.8).
4535 *
4536 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4537 * that looks like a ClientHello.
4538 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539 * - if the input looks like a ClientHello without cookies,
4540 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004541 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 * - if the input looks like a ClientHello with a valid cookie,
4543 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004544 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004546 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004548 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4549 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004550 */
4551static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4552{
4553 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004555
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004556 ret = ssl_check_dtls_clihlo_cookie(
4557 ssl->conf->f_cookie_write,
4558 ssl->conf->f_cookie_check,
4559 ssl->conf->p_cookie,
4560 ssl->cli_id, ssl->cli_id_len,
4561 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004562 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004563
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004564 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4565
4566 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004567 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004568 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004569 * If the error is permanent we'll catch it later,
4570 * if it's not, then hopefully it'll work next time. */
4571 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4572
4573 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004574 }
4575
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004576 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004577 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004578 /* Got a valid cookie, partially reset context */
4579 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4580 {
4581 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4582 return( ret );
4583 }
4584
4585 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004586 }
4587
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004588 return( ret );
4589}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004590#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004591
Hanno Becker46483f12019-05-03 13:25:54 +01004592static int ssl_check_record_type( uint8_t record_type )
4593{
4594 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4595 record_type != MBEDTLS_SSL_MSG_ALERT &&
4596 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4597 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4598 {
4599 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4600 }
4601
4602 return( 0 );
4603}
4604
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004605/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004606 * ContentType type;
4607 * ProtocolVersion version;
4608 * uint16 epoch; // DTLS only
4609 * uint48 sequence_number; // DTLS only
4610 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004611 *
4612 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004613 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004614 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4615 *
4616 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004617 * 1. proceed with the record if this function returns 0
4618 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4619 * 3. return CLIENT_RECONNECT if this function return that value
4620 * 4. drop the whole datagram if this function returns anything else.
4621 * Point 2 is needed when the peer is resending, and we have already received
4622 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004626 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004627 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
Hanno Becker8b09b732019-05-08 12:03:28 +01004629 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004630
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004632 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004634 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004635#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004636 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004637 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01004638 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 )
Hanno Becker8b09b732019-05-08 12:03:28 +01004639 {
4640 /* Shift pointers to account for record header including CID
4641 * struct {
4642 * ContentType special_type = tls12_cid;
4643 * ProtocolVersion version;
4644 * uint16 epoch;
4645 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004646 * opaque cid[cid_length]; // Additional field compared to
4647 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004648 * uint16 length;
4649 * opaque enc_content[DTLSCiphertext.length];
4650 * } DTLSCiphertext;
4651 */
4652
4653 /* So far, we only support static CID lengths
4654 * fixed in the configuration. */
Hanno Beckere0200da2019-06-13 09:23:43 +01004655 ssl->in_len = ssl->in_cid + mbedtls_ssl_conf_get_cid_len( ssl->conf );
Hanno Becker8b09b732019-05-08 12:03:28 +01004656 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4657 }
4658 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004659#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004660 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004663
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004664#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004665 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004666 * Section 4.1.2.7, that is, send alert only with TLS */
4667 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4668 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004669 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4670 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004671 }
4672#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004675 }
4676
4677 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004678 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4681 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004682 }
4683
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004684 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 }
4689
Hanno Becker8b09b732019-05-08 12:03:28 +01004690 /* Now that the total length of the record header is known, ensure
4691 * that the current datagram is large enough to hold it.
4692 * This would fail, for example, if we received a datagram of
4693 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4694 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4695 if( ret != 0 )
4696 {
4697 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4698 return( ret );
4699 }
4700 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4701
4702 /* Parse and validate record length
4703 * This must happen after the CID parsing because
4704 * its position in the record header depends on
4705 * the presence of a CID. */
4706
4707 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004708 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004709 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4712 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004713 }
4714
Hanno Becker8b09b732019-05-08 12:03:28 +01004715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004716 "version = [%d:%d], msglen = %d",
4717 ssl->in_msgtype,
4718 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004719
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004720 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004721 * DTLS-related tests.
4722 * Check epoch before checking length constraint because
4723 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4724 * message gets duplicated before the corresponding Finished message,
4725 * the second ChangeCipherSpec should be discarded because it belongs
4726 * to an old epoch, but not because its length is shorter than
4727 * the minimum record length for packets using the new record transform.
4728 * Note that these two kinds of failures are handled differently,
4729 * as an unexpected record is silently skipped but an invalid
4730 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004731 */
4732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004733 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004734 {
4735 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4736
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004737 /* Check epoch (and sequence number) with DTLS */
4738 if( rec_epoch != ssl->in_epoch )
4739 {
4740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4741 "expected %d, received %d",
4742 ssl->in_epoch, rec_epoch ) );
4743
4744#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4745 /*
4746 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4747 * access the first byte of record content (handshake type), as we
4748 * have an active transform (possibly iv_len != 0), so use the
4749 * fact that the record header len is 13 instead.
4750 */
4751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4752 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4753 rec_epoch == 0 &&
4754 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4755 ssl->in_left > 13 &&
4756 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4757 {
4758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4759 "from the same port" ) );
4760 return( ssl_handle_possible_reconnect( ssl ) );
4761 }
4762 else
4763#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004764 {
4765 /* Consider buffering the record. */
4766 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4767 {
4768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4769 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4770 }
4771
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004772 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004773 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004774 }
4775
4776#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4777 /* Replay detection only works for the current epoch */
4778 if( rec_epoch == ssl->in_epoch &&
4779 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4782 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4783 }
4784#endif
4785 }
4786#endif /* MBEDTLS_SSL_PROTO_DTLS */
4787
Hanno Becker52c6dc62017-05-26 16:07:36 +01004788
4789 /* Check length against bounds of the current transform and version */
4790 if( ssl->transform_in == NULL )
4791 {
4792 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004793 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004794 {
4795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4796 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4797 }
4798 }
4799 else
4800 {
4801 if( ssl->in_msglen < ssl->transform_in->minlen )
4802 {
4803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4804 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4805 }
4806
4807#if defined(MBEDTLS_SSL_PROTO_SSL3)
4808 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004809 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004810 {
4811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4812 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4813 }
4814#endif
4815#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4816 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4817 /*
4818 * TLS encrypted messages can have up to 256 bytes of padding
4819 */
4820 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4821 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004822 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004823 {
4824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4825 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4826 }
4827#endif
4828 }
4829
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004830 return( 0 );
4831}
Paul Bakker5121ce52009-01-03 21:22:43 +00004832
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833/*
4834 * If applicable, decrypt (and decompress) record content
4835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004836static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004837{
4838 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004841 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4844 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848 ret = mbedtls_ssl_hw_record_read( ssl );
4849 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4852 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004853 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004854
4855 if( ret == 0 )
4856 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004857 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004859 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004861 mbedtls_record rec;
4862
4863 rec.buf = ssl->in_iv;
4864 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4865 - ( ssl->in_iv - ssl->in_buf );
4866 rec.data_len = ssl->in_msglen;
4867 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004869 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004870 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004871#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004872
4873 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4874 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4875 ssl->conf->transport, rec.ver );
4876 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004877 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4878 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004880 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004881
Hanno Beckera5a2b082019-05-15 14:03:01 +01004882#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004883 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01004884 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
4885 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004886 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004888 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004889 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004891
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 return( ret );
4893 }
4894
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004895 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004896 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004897 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4898 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004900
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004901 /* The record content type may change during decryption,
4902 * so re-read it. */
4903 ssl->in_msgtype = rec.type;
4904 /* Also update the input buffer, because unfortunately
4905 * the server-side ssl_parse_client_hello() reparses the
4906 * record header when receiving a ClientHello initiating
4907 * a renegotiation. */
4908 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004909 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004910 ssl->in_msglen = rec.data_len;
4911 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4912 ssl->in_len[1] = (unsigned char)( rec.data_len );
4913
Paul Bakker5121ce52009-01-03 21:22:43 +00004914 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4915 ssl->in_msg, ssl->in_msglen );
4916
Hanno Beckera5a2b082019-05-15 14:03:01 +01004917#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004918 /* We have already checked the record content type
4919 * in ssl_parse_record_header(), failing or silently
4920 * dropping the record in the case of an unknown type.
4921 *
4922 * Since with the use of CIDs, the record content type
4923 * might change during decryption, re-check the record
4924 * content type, but treat a failure as fatal this time. */
4925 if( ssl_check_record_type( ssl->in_msgtype ) )
4926 {
4927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4928 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4929 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004930#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004931
Angus Grattond8213d02016-05-25 20:56:48 +10004932 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4935 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004936 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004937 else if( ssl->in_msglen == 0 )
4938 {
4939#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4940 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4941 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4942 {
4943 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4946 }
4947#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4948
4949 ssl->nb_zero++;
4950
4951 /*
4952 * Three or more empty messages may be a DoS attack
4953 * (excessive CPU consumption).
4954 */
4955 if( ssl->nb_zero > 3 )
4956 {
4957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004958 "messages, possible DoS attack" ) );
4959 /* Treat the records as if they were not properly authenticated,
4960 * thereby failing the connection if we see more than allowed
4961 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004962 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4963 }
4964 }
4965 else
4966 ssl->nb_zero = 0;
4967
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004968 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4969#if defined(MBEDTLS_SSL_PROTO_TLS)
4970 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004971 {
4972 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004973 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004974 if( ++ssl->in_ctr[i - 1] != 0 )
4975 break;
4976
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004977 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004978 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4981 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4982 }
4983 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004984#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004985
Paul Bakker5121ce52009-01-03 21:22:43 +00004986 }
4987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004989 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004991 {
4992 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004995 return( ret );
4996 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004997 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004998#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005001 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005004 }
5005#endif
5006
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005007 return( 0 );
5008}
5009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005010static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005011
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005012/*
5013 * Read a record.
5014 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005015 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5016 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5017 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005018 */
Hanno Becker1097b342018-08-15 14:09:41 +01005019
5020/* Helper functions for mbedtls_ssl_read_record(). */
5021static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005022static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5023static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005024
Hanno Becker327c93b2018-08-15 13:56:18 +01005025int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005026 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005027{
5028 int ret;
5029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005031
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005032 if( ssl->keep_current_message == 0 )
5033 {
5034 do {
Simon Butcher99000142016-10-13 17:21:01 +01005035
Hanno Becker26994592018-08-15 14:14:59 +01005036 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005037 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005038 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005039
Hanno Beckere74d5562018-08-15 14:26:08 +01005040 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005041 {
Hanno Becker40f50842018-08-15 14:48:01 +01005042#if defined(MBEDTLS_SSL_PROTO_DTLS)
5043 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005044
Hanno Becker40f50842018-08-15 14:48:01 +01005045 /* We only check for buffered messages if the
5046 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005047 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005048 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005049 {
Hanno Becker40f50842018-08-15 14:48:01 +01005050 if( ssl_load_buffered_message( ssl ) == 0 )
5051 have_buffered = 1;
5052 }
5053
5054 if( have_buffered == 0 )
5055#endif /* MBEDTLS_SSL_PROTO_DTLS */
5056 {
5057 ret = ssl_get_next_record( ssl );
5058 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5059 continue;
5060
5061 if( ret != 0 )
5062 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005063 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005064 return( ret );
5065 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005066 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005067 }
5068
5069 ret = mbedtls_ssl_handle_message_type( ssl );
5070
Hanno Becker40f50842018-08-15 14:48:01 +01005071#if defined(MBEDTLS_SSL_PROTO_DTLS)
5072 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5073 {
5074 /* Buffer future message */
5075 ret = ssl_buffer_message( ssl );
5076 if( ret != 0 )
5077 return( ret );
5078
5079 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5080 }
5081#endif /* MBEDTLS_SSL_PROTO_DTLS */
5082
Hanno Becker90333da2017-10-10 11:27:13 +01005083 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5084 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005085
5086 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005087 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005088 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005089 return( ret );
5090 }
5091
Hanno Becker327c93b2018-08-15 13:56:18 +01005092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005093 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005094 {
5095 mbedtls_ssl_update_handshake_status( ssl );
5096 }
Simon Butcher99000142016-10-13 17:21:01 +01005097 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005098 else
Simon Butcher99000142016-10-13 17:21:01 +01005099 {
Hanno Becker02f59072018-08-15 14:00:24 +01005100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005101 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005102 }
5103
5104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5105
5106 return( 0 );
5107}
5108
Hanno Becker40f50842018-08-15 14:48:01 +01005109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005110static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005111{
Hanno Becker40f50842018-08-15 14:48:01 +01005112 if( ssl->in_left > ssl->next_record_offset )
5113 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005114
Hanno Becker40f50842018-08-15 14:48:01 +01005115 return( 0 );
5116}
5117
5118static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5119{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005120 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005121 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005122 int ret = 0;
5123
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005124 if( hs == NULL )
5125 return( -1 );
5126
Hanno Beckere00ae372018-08-20 09:39:42 +01005127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5128
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005129 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5130 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5131 {
5132 /* Check if we have seen a ChangeCipherSpec before.
5133 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005134 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005135 {
5136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5137 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005138 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005139 }
5140
Hanno Becker39b8bc92018-08-28 17:17:13 +01005141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005142 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5143 ssl->in_msglen = 1;
5144 ssl->in_msg[0] = 1;
5145
5146 /* As long as they are equal, the exact value doesn't matter. */
5147 ssl->in_left = 0;
5148 ssl->next_record_offset = 0;
5149
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005150 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005151 goto exit;
5152 }
Hanno Becker37f95322018-08-16 13:55:32 +01005153
Hanno Beckerb8f50142018-08-28 10:01:34 +01005154#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005155 /* Debug only */
5156 {
5157 unsigned offset;
5158 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5159 {
5160 hs_buf = &hs->buffering.hs[offset];
5161 if( hs_buf->is_valid == 1 )
5162 {
5163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5164 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005165 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005166 }
5167 }
5168 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005169#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005170
5171 /* Check if we have buffered and/or fully reassembled the
5172 * next handshake message. */
5173 hs_buf = &hs->buffering.hs[0];
5174 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5175 {
5176 /* Synthesize a record containing the buffered HS message. */
5177 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5178 ( hs_buf->data[2] << 8 ) |
5179 hs_buf->data[3];
5180
5181 /* Double-check that we haven't accidentally buffered
5182 * a message that doesn't fit into the input buffer. */
5183 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5184 {
5185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5187 }
5188
5189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5190 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5191 hs_buf->data, msg_len + 12 );
5192
5193 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5194 ssl->in_hslen = msg_len + 12;
5195 ssl->in_msglen = msg_len + 12;
5196 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5197
5198 ret = 0;
5199 goto exit;
5200 }
5201 else
5202 {
5203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5204 hs->in_msg_seq ) );
5205 }
5206
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005207 ret = -1;
5208
5209exit:
5210
5211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5212 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005213}
5214
Hanno Beckera02b0b42018-08-21 17:20:27 +01005215static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5216 size_t desired )
5217{
5218 int offset;
5219 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5221 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005222
Hanno Becker01315ea2018-08-21 17:22:17 +01005223 /* Get rid of future records epoch first, if such exist. */
5224 ssl_free_buffered_record( ssl );
5225
5226 /* Check if we have enough space available now. */
5227 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5228 hs->buffering.total_bytes_buffered ) )
5229 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005231 return( 0 );
5232 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005233
Hanno Becker4f432ad2018-08-28 10:02:32 +01005234 /* We don't have enough space to buffer the next expected handshake
5235 * message. Remove buffers used for future messages to gain space,
5236 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005237 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5238 offset >= 0; offset-- )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5241 offset ) );
5242
Hanno Beckerb309b922018-08-23 13:18:05 +01005243 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005244
5245 /* Check if we have enough space available now. */
5246 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5247 hs->buffering.total_bytes_buffered ) )
5248 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005250 return( 0 );
5251 }
5252 }
5253
5254 return( -1 );
5255}
5256
Hanno Becker40f50842018-08-15 14:48:01 +01005257static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5258{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005259 int ret = 0;
5260 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5261
5262 if( hs == NULL )
5263 return( 0 );
5264
5265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5266
5267 switch( ssl->in_msgtype )
5268 {
5269 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005271
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005272 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005273 break;
5274
5275 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005276 {
5277 unsigned recv_msg_seq_offset;
5278 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5279 mbedtls_ssl_hs_buffer *hs_buf;
5280 size_t msg_len = ssl->in_hslen - 12;
5281
5282 /* We should never receive an old handshake
5283 * message - double-check nonetheless. */
5284 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5285 {
5286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5288 }
5289
5290 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5291 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5292 {
5293 /* Silently ignore -- message too far in the future */
5294 MBEDTLS_SSL_DEBUG_MSG( 2,
5295 ( "Ignore future HS message with sequence number %u, "
5296 "buffering window %u - %u",
5297 recv_msg_seq, ssl->handshake->in_msg_seq,
5298 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5299
5300 goto exit;
5301 }
5302
5303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5304 recv_msg_seq, recv_msg_seq_offset ) );
5305
5306 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5307
5308 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005309 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005310 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005311 size_t reassembly_buf_sz;
5312
Hanno Becker37f95322018-08-16 13:55:32 +01005313 hs_buf->is_fragmented =
5314 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5315
5316 /* We copy the message back into the input buffer
5317 * after reassembly, so check that it's not too large.
5318 * This is an implementation-specific limitation
5319 * and not one from the standard, hence it is not
5320 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005321 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005322 {
5323 /* Ignore message */
5324 goto exit;
5325 }
5326
Hanno Beckere0b150f2018-08-21 15:51:03 +01005327 /* Check if we have enough space to buffer the message. */
5328 if( hs->buffering.total_bytes_buffered >
5329 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5330 {
5331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5333 }
5334
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005335 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5336 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005337
5338 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5339 hs->buffering.total_bytes_buffered ) )
5340 {
5341 if( recv_msg_seq_offset > 0 )
5342 {
5343 /* If we can't buffer a future message because
5344 * of space limitations -- ignore. */
5345 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",
5346 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5347 (unsigned) hs->buffering.total_bytes_buffered ) );
5348 goto exit;
5349 }
Hanno Beckere1801392018-08-21 16:51:05 +01005350 else
5351 {
5352 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",
5353 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5354 (unsigned) hs->buffering.total_bytes_buffered ) );
5355 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005356
Hanno Beckera02b0b42018-08-21 17:20:27 +01005357 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005358 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005359 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",
5360 (unsigned) msg_len,
5361 (unsigned) reassembly_buf_sz,
5362 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005363 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005364 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5365 goto exit;
5366 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005367 }
5368
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5370 msg_len ) );
5371
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005372 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5373 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005374 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005375 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005376 goto exit;
5377 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005378 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005379
5380 /* Prepare final header: copy msg_type, length and message_seq,
5381 * then add standardised fragment_offset and fragment_length */
5382 memcpy( hs_buf->data, ssl->in_msg, 6 );
5383 memset( hs_buf->data + 6, 0, 3 );
5384 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5385
5386 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005387
5388 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005389 }
5390 else
5391 {
5392 /* Make sure msg_type and length are consistent */
5393 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5394 {
5395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5396 /* Ignore */
5397 goto exit;
5398 }
5399 }
5400
Hanno Becker4422bbb2018-08-20 09:40:19 +01005401 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005402 {
5403 size_t frag_len, frag_off;
5404 unsigned char * const msg = hs_buf->data + 12;
5405
5406 /*
5407 * Check and copy current fragment
5408 */
5409
5410 /* Validation of header fields already done in
5411 * mbedtls_ssl_prepare_handshake_record(). */
5412 frag_off = ssl_get_hs_frag_off( ssl );
5413 frag_len = ssl_get_hs_frag_len( ssl );
5414
5415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5416 frag_off, frag_len ) );
5417 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5418
5419 if( hs_buf->is_fragmented )
5420 {
5421 unsigned char * const bitmask = msg + msg_len;
5422 ssl_bitmask_set( bitmask, frag_off, frag_len );
5423 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5424 msg_len ) == 0 );
5425 }
5426 else
5427 {
5428 hs_buf->is_complete = 1;
5429 }
5430
5431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5432 hs_buf->is_complete ? "" : "not yet " ) );
5433 }
5434
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005435 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005436 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005437
5438 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005439 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005440 break;
5441 }
5442
5443exit:
5444
5445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5446 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005447}
5448#endif /* MBEDTLS_SSL_PROTO_DTLS */
5449
Hanno Becker1097b342018-08-15 14:09:41 +01005450static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005451{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005452 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005453 * Consume last content-layer message and potentially
5454 * update in_msglen which keeps track of the contents'
5455 * consumption state.
5456 *
5457 * (1) Handshake messages:
5458 * Remove last handshake message, move content
5459 * and adapt in_msglen.
5460 *
5461 * (2) Alert messages:
5462 * Consume whole record content, in_msglen = 0.
5463 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005464 * (3) Change cipher spec:
5465 * Consume whole record content, in_msglen = 0.
5466 *
5467 * (4) Application data:
5468 * Don't do anything - the record layer provides
5469 * the application data as a stream transport
5470 * and consumes through mbedtls_ssl_read only.
5471 *
5472 */
5473
5474 /* Case (1): Handshake messages */
5475 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005476 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005477 /* Hard assertion to be sure that no application data
5478 * is in flight, as corrupting ssl->in_msglen during
5479 * ssl->in_offt != NULL is fatal. */
5480 if( ssl->in_offt != NULL )
5481 {
5482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5484 }
5485
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005486 /*
5487 * Get next Handshake message in the current record
5488 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005489
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005491 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005492 * current handshake content: If DTLS handshake
5493 * fragmentation is used, that's the fragment
5494 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005495 * size here is faulty and should be changed at
5496 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005497 * (2) While it doesn't seem to cause problems, one
5498 * has to be very careful not to assume that in_hslen
5499 * is always <= in_msglen in a sensible communication.
5500 * Again, it's wrong for DTLS handshake fragmentation.
5501 * The following check is therefore mandatory, and
5502 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005503 * Additionally, ssl->in_hslen might be arbitrarily out of
5504 * bounds after handling a DTLS message with an unexpected
5505 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005506 */
5507 if( ssl->in_hslen < ssl->in_msglen )
5508 {
5509 ssl->in_msglen -= ssl->in_hslen;
5510 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5511 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005512
Hanno Becker4a810fb2017-05-24 16:27:30 +01005513 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5514 ssl->in_msg, ssl->in_msglen );
5515 }
5516 else
5517 {
5518 ssl->in_msglen = 0;
5519 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005520
Hanno Becker4a810fb2017-05-24 16:27:30 +01005521 ssl->in_hslen = 0;
5522 }
5523 /* Case (4): Application data */
5524 else if( ssl->in_offt != NULL )
5525 {
5526 return( 0 );
5527 }
5528 /* Everything else (CCS & Alerts) */
5529 else
5530 {
5531 ssl->in_msglen = 0;
5532 }
5533
Hanno Becker1097b342018-08-15 14:09:41 +01005534 return( 0 );
5535}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005536
Hanno Beckere74d5562018-08-15 14:26:08 +01005537static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5538{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005539 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005540 return( 1 );
5541
5542 return( 0 );
5543}
5544
Hanno Becker5f066e72018-08-16 14:56:31 +01005545#if defined(MBEDTLS_SSL_PROTO_DTLS)
5546
5547static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5548{
5549 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5550 if( hs == NULL )
5551 return;
5552
Hanno Becker01315ea2018-08-21 17:22:17 +01005553 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005554 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005555 hs->buffering.total_bytes_buffered -=
5556 hs->buffering.future_record.len;
5557
5558 mbedtls_free( hs->buffering.future_record.data );
5559 hs->buffering.future_record.data = NULL;
5560 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005561}
5562
5563static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5564{
5565 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5566 unsigned char * rec;
5567 size_t rec_len;
5568 unsigned rec_epoch;
5569
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005570 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005571 return( 0 );
5572
5573 if( hs == NULL )
5574 return( 0 );
5575
Hanno Becker5f066e72018-08-16 14:56:31 +01005576 rec = hs->buffering.future_record.data;
5577 rec_len = hs->buffering.future_record.len;
5578 rec_epoch = hs->buffering.future_record.epoch;
5579
5580 if( rec == NULL )
5581 return( 0 );
5582
Hanno Becker4cb782d2018-08-20 11:19:05 +01005583 /* Only consider loading future records if the
5584 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005585 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005586 return( 0 );
5587
Hanno Becker5f066e72018-08-16 14:56:31 +01005588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5589
5590 if( rec_epoch != ssl->in_epoch )
5591 {
5592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5593 goto exit;
5594 }
5595
5596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5597
5598 /* Double-check that the record is not too large */
5599 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5600 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5601 {
5602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5603 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5604 }
5605
5606 memcpy( ssl->in_hdr, rec, rec_len );
5607 ssl->in_left = rec_len;
5608 ssl->next_record_offset = 0;
5609
5610 ssl_free_buffered_record( ssl );
5611
5612exit:
5613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5614 return( 0 );
5615}
5616
5617static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5618{
5619 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5620 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005621 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005622
5623 /* Don't buffer future records outside handshakes. */
5624 if( hs == NULL )
5625 return( 0 );
5626
5627 /* Only buffer handshake records (we are only interested
5628 * in Finished messages). */
5629 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5630 return( 0 );
5631
5632 /* Don't buffer more than one future epoch record. */
5633 if( hs->buffering.future_record.data != NULL )
5634 return( 0 );
5635
Hanno Becker01315ea2018-08-21 17:22:17 +01005636 /* Don't buffer record if there's not enough buffering space remaining. */
5637 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5638 hs->buffering.total_bytes_buffered ) )
5639 {
5640 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",
5641 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5642 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005643 return( 0 );
5644 }
5645
Hanno Becker5f066e72018-08-16 14:56:31 +01005646 /* Buffer record */
5647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5648 ssl->in_epoch + 1 ) );
5649 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5650 rec_hdr_len + ssl->in_msglen );
5651
5652 /* ssl_parse_record_header() only considers records
5653 * of the next epoch as candidates for buffering. */
5654 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005655 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005656
5657 hs->buffering.future_record.data =
5658 mbedtls_calloc( 1, hs->buffering.future_record.len );
5659 if( hs->buffering.future_record.data == NULL )
5660 {
5661 /* If we run out of RAM trying to buffer a
5662 * record from the next epoch, just ignore. */
5663 return( 0 );
5664 }
5665
Hanno Becker01315ea2018-08-21 17:22:17 +01005666 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005667
Hanno Becker01315ea2018-08-21 17:22:17 +01005668 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005669 return( 0 );
5670}
5671
5672#endif /* MBEDTLS_SSL_PROTO_DTLS */
5673
Hanno Beckere74d5562018-08-15 14:26:08 +01005674static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005675{
5676 int ret;
5677
Hanno Becker5f066e72018-08-16 14:56:31 +01005678#if defined(MBEDTLS_SSL_PROTO_DTLS)
5679 /* We might have buffered a future record; if so,
5680 * and if the epoch matches now, load it.
5681 * On success, this call will set ssl->in_left to
5682 * the length of the buffered record, so that
5683 * the calls to ssl_fetch_input() below will
5684 * essentially be no-ops. */
5685 ret = ssl_load_buffered_record( ssl );
5686 if( ret != 0 )
5687 return( ret );
5688#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005689
Hanno Becker8b09b732019-05-08 12:03:28 +01005690 /* Reset in pointers to default state for TLS/DTLS records,
5691 * assuming no CID and no offset between record content and
5692 * record plaintext. */
5693 ssl_update_in_pointers( ssl );
5694
5695 /* Ensure that we have enough space available for the default form
5696 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5697 * with no space for CIDs counted in). */
5698 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5699 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005702 return( ret );
5703 }
5704
5705 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005708 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005709 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005710 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005711 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5712 {
5713 ret = ssl_buffer_future_record( ssl );
5714 if( ret != 0 )
5715 return( ret );
5716
5717 /* Fall through to handling of unexpected records */
5718 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5719 }
5720
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005721 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5722 {
5723 /* Skip unexpected record (but not whole datagram) */
5724 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005725 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005726
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005727 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5728 "(header)" ) );
5729 }
5730 else
5731 {
5732 /* Skip invalid record and the rest of the datagram */
5733 ssl->next_record_offset = 0;
5734 ssl->in_left = 0;
5735
5736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5737 "(header)" ) );
5738 }
5739
5740 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005741 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005742 }
5743#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005745 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746
5747 /*
5748 * Read and optionally decrypt the message contents
5749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005751 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005754 return( ret );
5755 }
5756
5757 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005759 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005760 {
Hanno Becker43395762019-05-03 14:46:38 +01005761 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005762 if( ssl->next_record_offset < ssl->in_left )
5763 {
5764 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5765 }
5766 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005767 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005768#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005769#if defined(MBEDTLS_SSL_PROTO_TLS)
5770 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005771 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005772 }
5773#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005774
5775 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005778 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005779 {
5780 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005781 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005782 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005783 /* Except when waiting for Finished as a bad mac here
5784 * probably means something went wrong in the handshake
5785 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5786 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5787 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5788 {
5789#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5790 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5791 {
5792 mbedtls_ssl_send_alert_message( ssl,
5793 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5794 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5795 }
5796#endif
5797 return( ret );
5798 }
5799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01005801 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
5802 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5805 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005806 }
5807#endif
5808
Hanno Becker4a810fb2017-05-24 16:27:30 +01005809 /* As above, invalid records cause
5810 * dismissal of the whole datagram. */
5811
5812 ssl->next_record_offset = 0;
5813 ssl->in_left = 0;
5814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005816 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005817 }
5818
5819 return( ret );
5820 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005821 MBEDTLS_SSL_TRANSPORT_ELSE
5822#endif /* MBEDTLS_SSL_PROTO_DTLS */
5823#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005824 {
5825 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005826#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5827 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005829 mbedtls_ssl_send_alert_message( ssl,
5830 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5831 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005832 }
5833#endif
5834 return( ret );
5835 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005836#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005837 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005838
Simon Butcher99000142016-10-13 17:21:01 +01005839 return( 0 );
5840}
5841
5842int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5843{
5844 int ret;
5845
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005846 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005847 * Handle particular types of records
5848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005849 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005850 {
Simon Butcher99000142016-10-13 17:21:01 +01005851 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5852 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005853 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005855 }
5856
Hanno Beckere678eaa2018-08-21 14:57:46 +01005857 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005858 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005859 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005860 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5862 ssl->in_msglen ) );
5863 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 }
5865
Hanno Beckere678eaa2018-08-21 14:57:46 +01005866 if( ssl->in_msg[0] != 1 )
5867 {
5868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5869 ssl->in_msg[0] ) );
5870 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5871 }
5872
5873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005874 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005875 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5876 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5877 {
5878 if( ssl->handshake == NULL )
5879 {
5880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5881 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5882 }
5883
5884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5885 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5886 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005887#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005888 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005892 if( ssl->in_msglen != 2 )
5893 {
5894 /* Note: Standard allows for more than one 2 byte alert
5895 to be packed in a single message, but Mbed TLS doesn't
5896 currently support this. */
5897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5898 ssl->in_msglen ) );
5899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5900 }
5901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 ssl->in_msg[0], ssl->in_msg[1] ) );
5904
5905 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005906 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005911 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005912 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 }
5914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005915 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5916 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5919 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005920 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005921
5922#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5923 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5924 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5925 {
Hanno Becker90333da2017-10-10 11:27:13 +01005926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005927 /* Will be handled when trying to parse ServerHello */
5928 return( 0 );
5929 }
5930#endif
5931
5932#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5933 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5934 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5935 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5936 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5937 {
5938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5939 /* Will be handled in mbedtls_ssl_parse_certificate() */
5940 return( 0 );
5941 }
5942#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5943
5944 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005945 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 }
5947
Hanno Beckerc76c6192017-06-06 10:03:17 +01005948#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005949 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005950 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005951 /* Drop unexpected ApplicationData records,
5952 * except at the beginning of renegotiations */
5953 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5954 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5955#if defined(MBEDTLS_SSL_RENEGOTIATION)
5956 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5957 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005958#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005959 )
5960 {
5961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5962 return( MBEDTLS_ERR_SSL_NON_FATAL );
5963 }
5964
5965 if( ssl->handshake != NULL &&
5966 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5967 {
5968 ssl_handshake_wrapup_free_hs_transform( ssl );
5969 }
5970 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005971#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005972
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 return( 0 );
5974}
5975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005977{
5978 int ret;
5979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5981 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5982 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005983 {
5984 return( ret );
5985 }
5986
5987 return( 0 );
5988}
5989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005990int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005991 unsigned char level,
5992 unsigned char message )
5993{
5994 int ret;
5995
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005996 if( ssl == NULL || ssl->conf == NULL )
5997 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006003 ssl->out_msglen = 2;
6004 ssl->out_msg[0] = level;
6005 ssl->out_msg[1] = message;
6006
Hanno Becker67bc7c32018-08-06 11:33:50 +01006007 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006010 return( ret );
6011 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006013
6014 return( 0 );
6015}
6016
Hanno Becker17572472019-02-08 07:19:04 +00006017#if defined(MBEDTLS_X509_CRT_PARSE_C)
6018static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6019{
6020#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6021 if( session->peer_cert != NULL )
6022 {
6023 mbedtls_x509_crt_free( session->peer_cert );
6024 mbedtls_free( session->peer_cert );
6025 session->peer_cert = NULL;
6026 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006027#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006028 if( session->peer_cert_digest != NULL )
6029 {
6030 /* Zeroization is not necessary. */
6031 mbedtls_free( session->peer_cert_digest );
6032 session->peer_cert_digest = NULL;
6033 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6034 session->peer_cert_digest_len = 0;
6035 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006036#else
6037 ((void) session);
6038#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006039}
6040#endif /* MBEDTLS_X509_CRT_PARSE_C */
6041
Paul Bakker5121ce52009-01-03 21:22:43 +00006042/*
6043 * Handshake functions
6044 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006045#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006046/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006048{
Hanno Becker8759e162017-12-27 21:34:08 +00006049 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006052
Hanno Becker5097cba2019-02-05 13:36:46 +00006053 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006056 ssl->state++;
6057 return( 0 );
6058 }
6059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062}
6063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006065{
Hanno Becker8759e162017-12-27 21:34:08 +00006066 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006069
Hanno Becker5097cba2019-02-05 13:36:46 +00006070 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006073 ssl->state++;
6074 return( 0 );
6075 }
6076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6078 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006079}
Gilles Peskinef9828522017-05-03 12:28:43 +02006080
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006081#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006082/* Some certificate support -> implement write and parse */
6083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006084int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006085{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006087 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006088 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006089 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006092
Hanno Becker5097cba2019-02-05 13:36:46 +00006093 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006096 ssl->state++;
6097 return( 0 );
6098 }
6099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006101 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006102 {
6103 if( ssl->client_auth == 0 )
6104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006106 ssl->state++;
6107 return( 0 );
6108 }
6109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 /*
6112 * If using SSLv3 and got no cert, send an Alert message
6113 * (otherwise an empty Certificate message will be sent).
6114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6116 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 {
6118 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6120 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6121 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 goto write_msg;
6125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128#endif /* MBEDTLS_SSL_CLI_C */
6129#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006130 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6135 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 }
6137 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141
6142 /*
6143 * 0 . 0 handshake type
6144 * 1 . 3 handshake length
6145 * 4 . 6 length of all certs
6146 * 7 . 9 length of cert. 1
6147 * 10 . n-1 peer certificate
6148 * n . n+2 length of cert. 2
6149 * n+3 . ... upper level cert, etc.
6150 */
6151 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006152 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006153
Paul Bakker29087132010-03-21 21:03:34 +00006154 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
6156 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006157 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006160 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006161 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 }
6163
6164 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6165 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6166 ssl->out_msg[i + 2] = (unsigned char)( n );
6167
6168 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6169 i += n; crt = crt->next;
6170 }
6171
6172 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6173 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6174 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6175
6176 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6178 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006179
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006180#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006181write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006183
6184 ssl->state++;
6185
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006186 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 return( ret );
6190 }
6191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006193
Paul Bakkered27a042013-04-18 22:46:23 +02006194 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195}
6196
Hanno Becker285ff0c2019-01-31 07:44:03 +00006197#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006198
6199#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006200static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6201 unsigned char *crt_buf,
6202 size_t crt_buf_len )
6203{
6204 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6205
6206 if( peer_crt == NULL )
6207 return( -1 );
6208
6209 if( peer_crt->raw.len != crt_buf_len )
6210 return( -1 );
6211
Hanno Becker68b856d2019-02-08 14:00:04 +00006212 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006213}
Hanno Becker5882dd02019-06-06 16:25:57 +01006214#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006215static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6216 unsigned char *crt_buf,
6217 size_t crt_buf_len )
6218{
6219 int ret;
6220 unsigned char const * const peer_cert_digest =
6221 ssl->session->peer_cert_digest;
6222 mbedtls_md_type_t const peer_cert_digest_type =
6223 ssl->session->peer_cert_digest_type;
6224 mbedtls_md_info_t const * const digest_info =
6225 mbedtls_md_info_from_type( peer_cert_digest_type );
6226 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6227 size_t digest_len;
6228
6229 if( peer_cert_digest == NULL || digest_info == NULL )
6230 return( -1 );
6231
6232 digest_len = mbedtls_md_get_size( digest_info );
6233 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6234 return( -1 );
6235
6236 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6237 if( ret != 0 )
6238 return( -1 );
6239
6240 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6241}
Hanno Becker5882dd02019-06-06 16:25:57 +01006242#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006243#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006244
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006245/*
6246 * Once the certificate message is read, parse it into a cert chain and
6247 * perform basic checks, but leave actual verification to the caller
6248 */
Hanno Becker35e41772019-02-05 15:37:23 +00006249static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6250 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006251{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006252 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006253#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6254 int crt_cnt=0;
6255#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006256 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006257 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006259 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006262 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6263 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006265 }
6266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6268 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006271 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6272 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006274 }
6275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006277
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006281 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006282
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006283 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006287 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6288 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006290 }
6291
Hanno Becker33c3dc82019-01-30 14:46:46 +00006292 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6293 i += 3;
6294
Hanno Becker33c3dc82019-01-30 14:46:46 +00006295 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 while( i < ssl->in_hslen )
6297 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006298 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006299 if ( i + 3 > ssl->in_hslen ) {
6300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006301 mbedtls_ssl_send_alert_message( ssl,
6302 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6303 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006304 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6305 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6307 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006308 if( ssl->in_msg[i] != 0 )
6309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006311 mbedtls_ssl_send_alert_message( ssl,
6312 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6313 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006315 }
6316
Hanno Becker33c3dc82019-01-30 14:46:46 +00006317 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006318 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6319 | (unsigned int) ssl->in_msg[i + 2];
6320 i += 3;
6321
6322 if( n < 128 || i + n > ssl->in_hslen )
6323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006325 mbedtls_ssl_send_alert_message( ssl,
6326 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6327 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 }
6330
Hanno Becker33c3dc82019-01-30 14:46:46 +00006331 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006332#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6333 if( crt_cnt++ == 0 &&
6334 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6335 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006336 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006337 /* During client-side renegotiation, check that the server's
6338 * end-CRTs hasn't changed compared to the initial handshake,
6339 * mitigating the triple handshake attack. On success, reuse
6340 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006341 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6342 if( ssl_check_peer_crt_unchanged( ssl,
6343 &ssl->in_msg[i],
6344 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006345 {
Hanno Becker35e41772019-02-05 15:37:23 +00006346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6347 mbedtls_ssl_send_alert_message( ssl,
6348 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6349 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6350 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006351 }
Hanno Becker35e41772019-02-05 15:37:23 +00006352
6353 /* Now we can safely free the original chain. */
6354 ssl_clear_peer_cert( ssl->session );
6355 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006356#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6357
Hanno Becker33c3dc82019-01-30 14:46:46 +00006358 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006359#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006360 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006361#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006362 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006363 * it in-place from the input buffer instead of making a copy. */
6364 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6365#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006366 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006367 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006368 case 0: /*ok*/
6369 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6370 /* Ignore certificate with an unknown algorithm: maybe a
6371 prior certificate was already trusted. */
6372 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006373
Hanno Becker33c3dc82019-01-30 14:46:46 +00006374 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6375 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6376 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006377
Hanno Becker33c3dc82019-01-30 14:46:46 +00006378 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6379 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6380 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006381
Hanno Becker33c3dc82019-01-30 14:46:46 +00006382 default:
6383 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6384 crt_parse_der_failed:
6385 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6386 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6387 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 }
6389
6390 i += n;
6391 }
6392
Hanno Becker35e41772019-02-05 15:37:23 +00006393 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006394 return( 0 );
6395}
6396
Hanno Beckerb8a08572019-02-05 12:49:06 +00006397#if defined(MBEDTLS_SSL_SRV_C)
6398static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6399{
6400 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6401 return( -1 );
6402
6403#if defined(MBEDTLS_SSL_PROTO_SSL3)
6404 /*
6405 * Check if the client sent an empty certificate
6406 */
6407 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6408 {
6409 if( ssl->in_msglen == 2 &&
6410 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6411 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6412 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6413 {
6414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6415 return( 0 );
6416 }
6417
6418 return( -1 );
6419 }
6420#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6421
6422#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6423 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6424 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6425 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6426 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6427 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6428 {
6429 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6430 return( 0 );
6431 }
6432
Hanno Beckerb8a08572019-02-05 12:49:06 +00006433#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6434 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006435
6436 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006437}
6438#endif /* MBEDTLS_SSL_SRV_C */
6439
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006440/* Check if a certificate message is expected.
6441 * Return either
6442 * - SSL_CERTIFICATE_EXPECTED, or
6443 * - SSL_CERTIFICATE_SKIP
6444 * indicating whether a Certificate message is expected or not.
6445 */
6446#define SSL_CERTIFICATE_EXPECTED 0
6447#define SSL_CERTIFICATE_SKIP 1
6448static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6449 int authmode )
6450{
6451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6452 ssl->handshake->ciphersuite_info;
6453
6454 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6455 return( SSL_CERTIFICATE_SKIP );
6456
6457#if defined(MBEDTLS_SSL_SRV_C)
6458 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6459 {
6460 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6461 return( SSL_CERTIFICATE_SKIP );
6462
6463 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6464 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006465 ssl->session_negotiate->verify_result =
6466 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6467 return( SSL_CERTIFICATE_SKIP );
6468 }
6469 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006470#else
6471 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006472#endif /* MBEDTLS_SSL_SRV_C */
6473
6474 return( SSL_CERTIFICATE_EXPECTED );
6475}
6476
Hanno Becker3cf50612019-02-05 14:36:34 +00006477static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6478 int authmode,
6479 mbedtls_x509_crt *chain,
6480 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006481{
Hanno Becker613d4902019-02-05 13:11:17 +00006482 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006483 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6484 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006485 mbedtls_x509_crt *ca_chain;
6486 mbedtls_x509_crl *ca_crl;
6487
6488 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6489 return( 0 );
6490
6491#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6492 if( ssl->handshake->sni_ca_chain != NULL )
6493 {
6494 ca_chain = ssl->handshake->sni_ca_chain;
6495 ca_crl = ssl->handshake->sni_ca_crl;
6496 }
6497 else
6498#endif
6499 {
6500 ca_chain = ssl->conf->ca_chain;
6501 ca_crl = ssl->conf->ca_crl;
6502 }
6503
6504 /*
6505 * Main check: verify certificate
6506 */
6507 ret = mbedtls_x509_crt_verify_restartable(
6508 chain,
6509 ca_chain, ca_crl,
6510 ssl->conf->cert_profile,
6511 ssl->hostname,
6512 &ssl->session_negotiate->verify_result,
6513 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6514
6515 if( ret != 0 )
6516 {
6517 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6518 }
6519
6520#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6521 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6522 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6523#endif
6524
6525 /*
6526 * Secondary checks: always done, but change 'ret' only if it was 0
6527 */
6528
6529#if defined(MBEDTLS_ECP_C)
6530 {
6531 const mbedtls_pk_context *pk = &chain->pk;
6532
6533 /* If certificate uses an EC key, make sure the curve is OK */
6534 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6535 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6536 {
6537 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6538
6539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6540 if( ret == 0 )
6541 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6542 }
6543 }
6544#endif /* MBEDTLS_ECP_C */
6545
6546 if( mbedtls_ssl_check_cert_usage( chain,
6547 ciphersuite_info,
6548 ! ssl->conf->endpoint,
6549 &ssl->session_negotiate->verify_result ) != 0 )
6550 {
6551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6552 if( ret == 0 )
6553 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6554 }
6555
6556 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6557 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6558 * with details encoded in the verification flags. All other kinds
6559 * of error codes, including those from the user provided f_vrfy
6560 * functions, are treated as fatal and lead to a failure of
6561 * ssl_parse_certificate even if verification was optional. */
6562 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6563 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6564 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6565 {
6566 ret = 0;
6567 }
6568
6569 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6570 {
6571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6572 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6573 }
6574
6575 if( ret != 0 )
6576 {
6577 uint8_t alert;
6578
6579 /* The certificate may have been rejected for several reasons.
6580 Pick one and send the corresponding alert. Which alert to send
6581 may be a subject of debate in some cases. */
6582 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6583 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6584 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6585 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6586 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6587 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6588 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6589 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6590 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6591 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6592 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6593 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6594 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6595 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6596 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6597 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6598 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6599 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6600 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6601 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6602 else
6603 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6604 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6605 alert );
6606 }
6607
6608#if defined(MBEDTLS_DEBUG_C)
6609 if( ssl->session_negotiate->verify_result != 0 )
6610 {
6611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6612 ssl->session_negotiate->verify_result ) );
6613 }
6614 else
6615 {
6616 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6617 }
6618#endif /* MBEDTLS_DEBUG_C */
6619
6620 return( ret );
6621}
6622
Hanno Becker34106f62019-02-08 14:59:05 +00006623#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006624
6625#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006626static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6627 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006628{
6629 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006630 /* Remember digest of the peer's end-CRT. */
6631 ssl->session_negotiate->peer_cert_digest =
6632 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6633 if( ssl->session_negotiate->peer_cert_digest == NULL )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6636 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6637 mbedtls_ssl_send_alert_message( ssl,
6638 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6639 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6640
6641 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6642 }
6643
6644 ret = mbedtls_md( mbedtls_md_info_from_type(
6645 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6646 start, len,
6647 ssl->session_negotiate->peer_cert_digest );
6648
6649 ssl->session_negotiate->peer_cert_digest_type =
6650 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6651 ssl->session_negotiate->peer_cert_digest_len =
6652 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6653
6654 return( ret );
6655}
Hanno Becker5882dd02019-06-06 16:25:57 +01006656#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006657
6658static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6659 unsigned char *start, size_t len )
6660{
6661 unsigned char *end = start + len;
6662 int ret;
6663
6664 /* Make a copy of the peer's raw public key. */
6665 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6666 ret = mbedtls_pk_parse_subpubkey( &start, end,
6667 &ssl->handshake->peer_pubkey );
6668 if( ret != 0 )
6669 {
6670 /* We should have parsed the public key before. */
6671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6672 }
6673
6674 return( 0 );
6675}
6676#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6677
Hanno Becker3cf50612019-02-05 14:36:34 +00006678int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6679{
6680 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006681 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006682#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6683 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6684 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006685 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006686#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006687 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006688#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006689 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006690 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006691
6692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6693
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006694 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6695 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006696 {
6697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006698 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006699 }
6700
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006701#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6702 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006703 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006704 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006705 chain = ssl->handshake->ecrs_peer_cert;
6706 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006707 goto crt_verify;
6708 }
6709#endif
6710
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006711 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006712 {
6713 /* mbedtls_ssl_read_record may have sent an alert already. We
6714 let it decide whether to alert. */
6715 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006716 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006717 }
6718
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006719#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006720 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6721 {
6722 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006723
Hanno Beckerb8a08572019-02-05 12:49:06 +00006724 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006725 ret = 0;
6726 else
6727 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006728
Hanno Becker613d4902019-02-05 13:11:17 +00006729 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006730 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006731#endif /* MBEDTLS_SSL_SRV_C */
6732
Hanno Becker35e41772019-02-05 15:37:23 +00006733 /* Clear existing peer CRT structure in case we tried to
6734 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006735 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006736
6737 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6738 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006739 {
6740 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6741 sizeof( mbedtls_x509_crt ) ) );
6742 mbedtls_ssl_send_alert_message( ssl,
6743 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6744 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006745
Hanno Beckere4aeb762019-02-05 17:19:52 +00006746 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6747 goto exit;
6748 }
6749 mbedtls_x509_crt_init( chain );
6750
6751 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006752 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006753 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006754
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006755#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6756 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006757 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006758
6759crt_verify:
6760 if( ssl->handshake->ecrs_enabled)
6761 rs_ctx = &ssl->handshake->ecrs_ctx;
6762#endif
6763
Hanno Becker3cf50612019-02-05 14:36:34 +00006764 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006765 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006766 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006767 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006768
Hanno Becker3008d282019-02-05 17:02:28 +00006769#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006770 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006771 size_t pk_len;
6772 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006773
Hanno Becker34106f62019-02-08 14:59:05 +00006774 /* We parse the CRT chain without copying, so
6775 * these pointers point into the input buffer,
6776 * and are hence still valid after freeing the
6777 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006778
Hanno Becker5882dd02019-06-06 16:25:57 +01006779#if defined(MBEDTLS_SSL_RENEGOTIATION)
6780 unsigned char *crt_start;
6781 size_t crt_len;
6782
Hanno Becker34106f62019-02-08 14:59:05 +00006783 crt_start = chain->raw.p;
6784 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006785#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006786
Hanno Becker34106f62019-02-08 14:59:05 +00006787 pk_start = chain->pk_raw.p;
6788 pk_len = chain->pk_raw.len;
6789
6790 /* Free the CRT structures before computing
6791 * digest and copying the peer's public key. */
6792 mbedtls_x509_crt_free( chain );
6793 mbedtls_free( chain );
6794 chain = NULL;
6795
Hanno Becker5882dd02019-06-06 16:25:57 +01006796#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006797 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006799 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006800#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006801
Hanno Becker34106f62019-02-08 14:59:05 +00006802 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006804 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
Hanno Becker34106f62019-02-08 14:59:05 +00006806#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6807 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006808 ssl->session_negotiate->peer_cert = chain;
6809 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006810#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
Hanno Becker613d4902019-02-05 13:11:17 +00006814exit:
6815
Hanno Beckere4aeb762019-02-05 17:19:52 +00006816 if( ret == 0 )
6817 ssl->state++;
6818
6819#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6820 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6821 {
6822 ssl->handshake->ecrs_peer_cert = chain;
6823 chain = NULL;
6824 }
6825#endif
6826
6827 if( chain != NULL )
6828 {
6829 mbedtls_x509_crt_free( chain );
6830 mbedtls_free( chain );
6831 }
6832
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 return( ret );
6834}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006835#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006838{
6839 int ret;
6840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 ssl->out_msglen = 1;
6845 ssl->out_msg[0] = 1;
6846
Paul Bakker5121ce52009-01-03 21:22:43 +00006847 ssl->state++;
6848
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006849 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006851 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 return( ret );
6853 }
6854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006856
6857 return( 0 );
6858}
6859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006860int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006861{
6862 int ret;
6863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
Hanno Becker327c93b2018-08-15 13:56:18 +01006866 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006868 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006869 return( ret );
6870 }
6871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006872 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006875 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6876 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006878 }
6879
Hanno Beckere678eaa2018-08-21 14:57:46 +01006880 /* CCS records are only accepted if they have length 1 and content '1',
6881 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006882
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006883 /*
6884 * Switch to our negotiated transform and session parameters for inbound
6885 * data.
6886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006887 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006888 ssl->transform_in = ssl->transform_negotiate;
6889 ssl->session_in = ssl->session_negotiate;
6890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006891#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006892 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006895 ssl_dtls_replay_reset( ssl );
6896#endif
6897
6898 /* Increment epoch */
6899 if( ++ssl->in_epoch == 0 )
6900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006902 /* This is highly unlikely to happen for legitimate reasons, so
6903 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006904 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006905 }
6906 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006907 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006909#if defined(MBEDTLS_SSL_PROTO_TLS)
6910 {
6911 memset( ssl->in_ctr, 0, 8 );
6912 }
6913#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006914
Hanno Beckerf5970a02019-05-08 09:38:41 +01006915 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6918 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006920 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006923 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6924 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006926 }
6927 }
6928#endif
6929
Paul Bakker5121ce52009-01-03 21:22:43 +00006930 ssl->state++;
6931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006933
6934 return( 0 );
6935}
6936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6938 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006939{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006940 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6943 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6944 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006945 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006946 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6949#if defined(MBEDTLS_SHA512_C)
6950 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006951 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6952 else
6953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SHA256_C)
6955 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006956 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006957 else
6958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006962 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006963 }
Paul Bakker380da532012-04-18 16:10:25 +00006964}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006966void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006967{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6969 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006970 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6971 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006972#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006973#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6974#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006975 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006978 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006979#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006981}
6982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006984 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6987 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006988 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6989 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006990#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006991#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6992#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006993 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006994#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006995#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006996 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006999}
7000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7002 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7003static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007004 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007005{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007006 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7007 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007009#endif
Paul Bakker380da532012-04-18 16:10:25 +00007010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7012#if defined(MBEDTLS_SHA256_C)
7013static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007014 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007015{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007016 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007017}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007018#endif
Paul Bakker380da532012-04-18 16:10:25 +00007019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007020#if defined(MBEDTLS_SHA512_C)
7021static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007022 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007023{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007024 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007025}
Paul Bakker769075d2012-11-24 11:26:46 +01007026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007030static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007031 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007032{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007033 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007034 mbedtls_md5_context md5;
7035 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007036
Paul Bakker5121ce52009-01-03 21:22:43 +00007037 unsigned char padbuf[48];
7038 unsigned char md5sum[16];
7039 unsigned char sha1sum[20];
7040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007041 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007042 if( !session )
7043 session = ssl->session;
7044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007046
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007047 mbedtls_md5_init( &md5 );
7048 mbedtls_sha1_init( &sha1 );
7049
7050 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7051 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
7053 /*
7054 * SSLv3:
7055 * hash =
7056 * MD5( master + pad2 +
7057 * MD5( handshake + sender + master + pad1 ) )
7058 * + SHA1( master + pad2 +
7059 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007060 */
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007063 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7064 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007065#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007068 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7069 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007073 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007074
Paul Bakker1ef83d62012-04-11 12:09:53 +00007075 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007077 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7078 mbedtls_md5_update_ret( &md5, session->master, 48 );
7079 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7080 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007082 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7083 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7084 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7085 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Paul Bakker1ef83d62012-04-11 12:09:53 +00007087 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007089 mbedtls_md5_starts_ret( &md5 );
7090 mbedtls_md5_update_ret( &md5, session->master, 48 );
7091 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7092 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7093 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007095 mbedtls_sha1_starts_ret( &sha1 );
7096 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7097 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7098 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7099 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007101 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007102
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007103 mbedtls_md5_free( &md5 );
7104 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007106 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7107 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7108 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007115static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007117{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007118 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007119 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007120 mbedtls_md5_context md5;
7121 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007122 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007125 if( !session )
7126 session = ssl->session;
7127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007130 mbedtls_md5_init( &md5 );
7131 mbedtls_sha1_init( &sha1 );
7132
7133 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7134 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007135
Paul Bakker1ef83d62012-04-11 12:09:53 +00007136 /*
7137 * TLSv1:
7138 * hash = PRF( master, finished_label,
7139 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7140 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007143 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7144 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007145#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007148 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7149 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007150#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007153 ? "client finished"
7154 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007156 mbedtls_md5_finish_ret( &md5, padbuf );
7157 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007158
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007159 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007160 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007163
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007164 mbedtls_md5_free( &md5 );
7165 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007166
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007167 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7174#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007175static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177{
7178 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007179 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007180 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007181 unsigned char padbuf[32];
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007184 if( !session )
7185 session = ssl->session;
7186
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007187 mbedtls_sha256_init( &sha256 );
7188
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007191 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
7193 /*
7194 * TLSv1.2:
7195 * hash = PRF( master, finished_label,
7196 * Hash( handshake ) )[0.11]
7197 */
7198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if !defined(MBEDTLS_SHA256_ALT)
7200 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007201 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007202#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007205 ? "client finished"
7206 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007207
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007208 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007209
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007210 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007211 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007214
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007215 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007216
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007217 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007224static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007226{
7227 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007228 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007229 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007230 unsigned char padbuf[48];
7231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007233 if( !session )
7234 session = ssl->session;
7235
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007236 mbedtls_sha512_init( &sha512 );
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007240 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007241
7242 /*
7243 * TLSv1.2:
7244 * hash = PRF( master, finished_label,
7245 * Hash( handshake ) )[0.11]
7246 */
7247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007249 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7250 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007251#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007254 ? "client finished"
7255 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007256
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007257 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007258
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007259 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007260 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007263
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007264 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007265
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007266 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007269}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270#endif /* MBEDTLS_SHA512_C */
7271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007273static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007276
7277 /*
7278 * Free our handshake params
7279 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007280 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007282 ssl->handshake = NULL;
7283
7284 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007285 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007286 */
7287 if( ssl->transform )
7288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289 mbedtls_ssl_transform_free( ssl->transform );
7290 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007291 }
7292 ssl->transform = ssl->transform_negotiate;
7293 ssl->transform_negotiate = NULL;
7294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007296}
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007299{
7300 int resume = ssl->handshake->resume;
7301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SSL_RENEGOTIATION)
7305 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007308 ssl->renego_records_seen = 0;
7309 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007310#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007311
7312 /*
7313 * Free the previous session and switch in the current one
7314 */
Paul Bakker0a597072012-09-25 21:55:46 +00007315 if( ssl->session )
7316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007318 /* RFC 7366 3.1: keep the EtM state */
7319 ssl->session_negotiate->encrypt_then_mac =
7320 ssl->session->encrypt_then_mac;
7321#endif
7322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323 mbedtls_ssl_session_free( ssl->session );
7324 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007325 }
7326 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007327 ssl->session_negotiate = NULL;
7328
Paul Bakker0a597072012-09-25 21:55:46 +00007329 /*
7330 * Add cache entry
7331 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007332 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007333 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007334 resume == 0 )
7335 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007336 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007338 }
Paul Bakker0a597072012-09-25 21:55:46 +00007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007341 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007342 ssl->handshake->flight != NULL )
7343 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007344 /* Cancel handshake timer */
7345 ssl_set_timer( ssl, 0 );
7346
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007347 /* Keep last flight around in case we need to resend it:
7348 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007349 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007350 }
7351 else
7352#endif
7353 ssl_handshake_wrapup_free_hs_transform( ssl );
7354
Paul Bakker48916f92012-09-16 19:57:18 +00007355 ssl->state++;
7356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007358}
7359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007361{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007362 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007365
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007366 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007367
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007368 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007369
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007370 /*
7371 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7372 * may define some other value. Currently (early 2016), no defined
7373 * ciphersuite does this (and this is unlikely to change as activity has
7374 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007378#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007379 ssl->verify_data_len = hash_len;
7380 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007381#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007382
Paul Bakker5121ce52009-01-03 21:22:43 +00007383 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7385 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
7387 /*
7388 * In case of session resuming, invert the client and server
7389 * ChangeCipherSpec messages order.
7390 */
Paul Bakker0a597072012-09-25 21:55:46 +00007391 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007394 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007396#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007400#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007401 }
7402 else
7403 ssl->state++;
7404
Paul Bakker48916f92012-09-16 19:57:18 +00007405 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007406 * Switch to our negotiated transform and session parameters for outbound
7407 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007412 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007413 {
7414 unsigned char i;
7415
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007416 /* Remember current epoch settings for resending */
7417 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007418 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007419
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007420 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007421 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007422
7423 /* Increment epoch */
7424 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007425 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007426 break;
7427
7428 /* The loop goes to its end iff the counter is wrapping */
7429 if( i == 0 )
7430 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7432 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007433 }
7434 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007435 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007437#if defined(MBEDTLS_SSL_PROTO_TLS)
7438 {
7439 memset( ssl->cur_out_ctr, 0, 8 );
7440 }
7441#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007442
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007443 ssl->transform_out = ssl->transform_negotiate;
7444 ssl->session_out = ssl->session_negotiate;
7445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7447 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7452 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007453 }
7454 }
7455#endif
7456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007458 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007460#endif
7461
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007462 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007463 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007465 return( ret );
7466 }
7467
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007469 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007470 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7471 {
7472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7473 return( ret );
7474 }
7475#endif
7476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007478
7479 return( 0 );
7480}
7481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007483#define SSL_MAX_HASH_LEN 36
7484#else
7485#define SSL_MAX_HASH_LEN 12
7486#endif
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007489{
Paul Bakker23986e52011-04-24 08:57:21 +00007490 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007491 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007492 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007495
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007496 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
Hanno Becker327c93b2018-08-15 13:56:18 +01007498 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 return( ret );
7502 }
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7508 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 }
7511
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007512 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513#if defined(MBEDTLS_SSL_PROTO_SSL3)
7514 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007515 hash_len = 36;
7516 else
7517#endif
7518 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7521 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007524 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007527 }
7528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007530 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007533 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7534 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007536 }
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007539 ssl->verify_data_len = hash_len;
7540 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007541#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007542
Paul Bakker0a597072012-09-25 21:55:46 +00007543 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007548#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007553 }
7554 else
7555 ssl->state++;
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007558 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007560#endif
7561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007563
7564 return( 0 );
7565}
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007568{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7572 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7573 mbedtls_md5_init( &handshake->fin_md5 );
7574 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007575 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7576 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7579#if defined(MBEDTLS_SHA256_C)
7580 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007581 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SHA512_C)
7584 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007585 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007588
7589 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007590
7591#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7592 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7593 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7594#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if defined(MBEDTLS_DHM_C)
7597 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#if defined(MBEDTLS_ECDH_C)
7600 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007601#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007602#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007603 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007604#if defined(MBEDTLS_SSL_CLI_C)
7605 handshake->ecjpake_cache = NULL;
7606 handshake->ecjpake_cache_len = 0;
7607#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007608#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007609
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007610#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007611 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007612#endif
7613
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007614#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7615 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7616#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007617
7618#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7619 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7620 mbedtls_pk_init( &handshake->peer_pubkey );
7621#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007622}
7623
Hanno Becker611a83b2018-01-03 14:27:32 +00007624void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7629 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007630
Hanno Becker92231322018-01-03 15:32:51 +00007631#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 mbedtls_md_init( &transform->md_ctx_enc );
7633 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007634#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007635}
7636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007637void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007638{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007640}
7641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007643{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007644 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007645 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007647 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007649 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007650 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007651
7652 /*
7653 * Either the pointers are now NULL or cleared properly and can be freed.
7654 * Now allocate missing structures.
7655 */
7656 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007657 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007658 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007659 }
Paul Bakker48916f92012-09-16 19:57:18 +00007660
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007661 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007662 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007663 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007664 }
Paul Bakker48916f92012-09-16 19:57:18 +00007665
Paul Bakker82788fb2014-10-20 13:59:19 +02007666 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007667 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007668 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007669 }
Paul Bakker48916f92012-09-16 19:57:18 +00007670
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007671 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007672 if( ssl->handshake == NULL ||
7673 ssl->transform_negotiate == NULL ||
7674 ssl->session_negotiate == NULL )
7675 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678 mbedtls_free( ssl->handshake );
7679 mbedtls_free( ssl->transform_negotiate );
7680 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007681
7682 ssl->handshake = NULL;
7683 ssl->transform_negotiate = NULL;
7684 ssl->session_negotiate = NULL;
7685
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007686 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007687 }
7688
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007689 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007691 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007692 ssl_handshake_params_init( ssl->handshake );
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007695 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007696 {
7697 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007698
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007699 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7700 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7701 else
7702 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007703
7704 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007705 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007706#endif
7707
Paul Bakker48916f92012-09-16 19:57:18 +00007708 return( 0 );
7709}
7710
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007711#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007712/* Dummy cookie callbacks for defaults */
7713static int ssl_cookie_write_dummy( void *ctx,
7714 unsigned char **p, unsigned char *end,
7715 const unsigned char *cli_id, size_t cli_id_len )
7716{
7717 ((void) ctx);
7718 ((void) p);
7719 ((void) end);
7720 ((void) cli_id);
7721 ((void) cli_id_len);
7722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007724}
7725
7726static int ssl_cookie_check_dummy( void *ctx,
7727 const unsigned char *cookie, size_t cookie_len,
7728 const unsigned char *cli_id, size_t cli_id_len )
7729{
7730 ((void) ctx);
7731 ((void) cookie);
7732 ((void) cookie_len);
7733 ((void) cli_id);
7734 ((void) cli_id_len);
7735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007737}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007738#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007739
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007740/* Once ssl->out_hdr as the address of the beginning of the
7741 * next outgoing record is set, deduce the other pointers.
7742 *
7743 * Note: For TLS, we save the implicit record sequence number
7744 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7745 * and the caller has to make sure there's space for this.
7746 */
7747
7748static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7749 mbedtls_ssl_transform *transform )
7750{
7751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007752 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007753 {
7754 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007755#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007756 ssl->out_cid = ssl->out_ctr + 8;
7757 ssl->out_len = ssl->out_cid;
7758 if( transform != NULL )
7759 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007760#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007761 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007762#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007763 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007764 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007765 MBEDTLS_SSL_TRANSPORT_ELSE
7766#endif /* MBEDTLS_SSL_PROTO_DTLS */
7767#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007768 {
7769 ssl->out_ctr = ssl->out_hdr - 8;
7770 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007771#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007772 ssl->out_cid = ssl->out_len;
7773#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007774 ssl->out_iv = ssl->out_hdr + 5;
7775 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007776#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007777
7778 /* Adjust out_msg to make space for explicit IV, if used. */
7779 if( transform != NULL &&
7780 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7781 {
7782 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7783 }
7784 else
7785 ssl->out_msg = ssl->out_iv;
7786}
7787
7788/* Once ssl->in_hdr as the address of the beginning of the
7789 * next incoming record is set, deduce the other pointers.
7790 *
7791 * Note: For TLS, we save the implicit record sequence number
7792 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7793 * and the caller has to make sure there's space for this.
7794 */
7795
Hanno Beckerf5970a02019-05-08 09:38:41 +01007796static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007797{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007798 /* This function sets the pointers to match the case
7799 * of unprotected TLS/DTLS records, with both ssl->in_iv
7800 * and ssl->in_msg pointing to the beginning of the record
7801 * content.
7802 *
7803 * When decrypting a protected record, ssl->in_msg
7804 * will be shifted to point to the beginning of the
7805 * record plaintext.
7806 */
7807
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007809 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007810 {
Hanno Becker70e79282019-05-03 14:34:53 +01007811 /* This sets the header pointers to match records
7812 * without CID. When we receive a record containing
7813 * a CID, the fields are shifted accordingly in
7814 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007815 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007816#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007817 ssl->in_cid = ssl->in_ctr + 8;
7818 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007819#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007820 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007821#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007822 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007823 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007824 MBEDTLS_SSL_TRANSPORT_ELSE
7825#endif /* MBEDTLS_SSL_PROTO_DTLS */
7826#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007827 {
7828 ssl->in_ctr = ssl->in_hdr - 8;
7829 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007830#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007831 ssl->in_cid = ssl->in_len;
7832#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007833 ssl->in_iv = ssl->in_hdr + 5;
7834 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007835#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007836
Hanno Beckerf5970a02019-05-08 09:38:41 +01007837 /* This will be adjusted at record decryption time. */
7838 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007839}
7840
Paul Bakker5121ce52009-01-03 21:22:43 +00007841/*
7842 * Initialize an SSL context
7843 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007844void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7845{
7846 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7847}
7848
7849/*
7850 * Setup an SSL context
7851 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007852
7853static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7854{
7855 /* Set the incoming and outgoing record pointers. */
7856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007857 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007858 {
7859 ssl->out_hdr = ssl->out_buf;
7860 ssl->in_hdr = ssl->in_buf;
7861 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007862 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007863#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007864#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007865 {
7866 ssl->out_hdr = ssl->out_buf + 8;
7867 ssl->in_hdr = ssl->in_buf + 8;
7868 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007869#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007870
7871 /* Derive other internal pointers. */
7872 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007873 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007874}
7875
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007876int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007877 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007878{
Paul Bakker48916f92012-09-16 19:57:18 +00007879 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007881 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007882
7883 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007884 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007885 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007886
7887 /* Set to NULL in case of an error condition */
7888 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007889
Angus Grattond8213d02016-05-25 20:56:48 +10007890 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7891 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007892 {
Angus Grattond8213d02016-05-25 20:56:48 +10007893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007894 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007895 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007896 }
7897
7898 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7899 if( ssl->out_buf == NULL )
7900 {
7901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007902 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007903 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007904 }
7905
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007906 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007907
Paul Bakker48916f92012-09-16 19:57:18 +00007908 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007909 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007910
7911 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007912
7913error:
7914 mbedtls_free( ssl->in_buf );
7915 mbedtls_free( ssl->out_buf );
7916
7917 ssl->conf = NULL;
7918
7919 ssl->in_buf = NULL;
7920 ssl->out_buf = NULL;
7921
7922 ssl->in_hdr = NULL;
7923 ssl->in_ctr = NULL;
7924 ssl->in_len = NULL;
7925 ssl->in_iv = NULL;
7926 ssl->in_msg = NULL;
7927
7928 ssl->out_hdr = NULL;
7929 ssl->out_ctr = NULL;
7930 ssl->out_len = NULL;
7931 ssl->out_iv = NULL;
7932 ssl->out_msg = NULL;
7933
k-stachowiak9f7798e2018-07-31 16:52:32 +02007934 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007935}
7936
7937/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007938 * Reset an initialized and used SSL context for re-use while retaining
7939 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007940 *
7941 * If partial is non-zero, keep data in the input buffer and client ID.
7942 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007943 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007944static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007945{
Paul Bakker48916f92012-09-16 19:57:18 +00007946 int ret;
7947
Hanno Becker7e772132018-08-10 12:38:21 +01007948#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7949 !defined(MBEDTLS_SSL_SRV_C)
7950 ((void) partial);
7951#endif
7952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007954
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007955 /* Cancel any possibly running timer */
7956 ssl_set_timer( ssl, 0 );
7957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958#if defined(MBEDTLS_SSL_RENEGOTIATION)
7959 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007960 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007961
7962 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7964 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007967
Paul Bakker7eb013f2011-10-06 12:37:39 +00007968 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007969 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007970
7971 ssl->in_msgtype = 0;
7972 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007974 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007975 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007976#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007978 ssl_dtls_replay_reset( ssl );
7979#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007980
7981 ssl->in_hslen = 0;
7982 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007983
7984 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007985
7986 ssl->out_msgtype = 0;
7987 ssl->out_msglen = 0;
7988 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7990 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007991 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007992#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007993
Hanno Becker19859472018-08-06 09:40:20 +01007994 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7995
Paul Bakker48916f92012-09-16 19:57:18 +00007996 ssl->transform_in = NULL;
7997 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007998
Hanno Becker78640902018-08-13 16:35:15 +01007999 ssl->session_in = NULL;
8000 ssl->session_out = NULL;
8001
Angus Grattond8213d02016-05-25 20:56:48 +10008002 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008003
8004#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008005 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008006#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8007 {
8008 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008009 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008010 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8013 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8016 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8019 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008020 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008021 }
8022#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008023
Paul Bakker48916f92012-09-16 19:57:18 +00008024 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 mbedtls_ssl_transform_free( ssl->transform );
8027 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008028 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008029 }
Paul Bakker48916f92012-09-16 19:57:18 +00008030
Paul Bakkerc0463502013-02-14 11:19:38 +01008031 if( ssl->session )
8032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008033 mbedtls_ssl_session_free( ssl->session );
8034 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008035 ssl->session = NULL;
8036 }
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008039 ssl->alpn_chosen = NULL;
8040#endif
8041
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008042#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008043#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008044 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008045#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008046 {
8047 mbedtls_free( ssl->cli_id );
8048 ssl->cli_id = NULL;
8049 ssl->cli_id_len = 0;
8050 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008051#endif
8052
Paul Bakker48916f92012-09-16 19:57:18 +00008053 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8054 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008055
8056 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008057}
8058
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008059/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008060 * Reset an initialized and used SSL context for re-use while retaining
8061 * all application-set variables, function pointers and data.
8062 */
8063int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8064{
8065 return( ssl_session_reset_int( ssl, 0 ) );
8066}
8067
8068/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008069 * SSL set accessors
8070 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008071void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008073 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008074}
8075
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008076void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008078 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008079}
8080
Hanno Becker7f376f42019-06-12 16:20:48 +01008081#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8082 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008083void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008084{
Hanno Becker7f376f42019-06-12 16:20:48 +01008085 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008086}
Hanno Becker7f376f42019-06-12 16:20:48 +01008087#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008088
Hanno Beckerde671542019-06-12 16:30:46 +01008089#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8090 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8091void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8092 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008094 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008095}
Hanno Beckerde671542019-06-12 16:30:46 +01008096#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008099
Hanno Becker1841b0a2018-08-24 11:13:57 +01008100void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8101 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008102{
8103 ssl->disable_datagram_packing = !allow_packing;
8104}
8105
8106void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8107 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008109 conf->hs_timeout_min = min;
8110 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008111}
8112#endif
8113
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008114void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008116#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8117 conf->authmode = authmode;
8118#else
8119 ((void) conf);
8120 ((void) authmode);
8121#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008122}
8123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008124#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008125void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008126 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008127 void *p_vrfy )
8128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008129 conf->f_vrfy = f_vrfy;
8130 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008134void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008135 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008136 void *p_rng )
8137{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008138 conf->f_rng = f_rng;
8139 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008140}
8141
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008142void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008143 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008144 void *p_dbg )
8145{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008146 conf->f_dbg = f_dbg;
8147 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008148}
8149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008151 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008152 mbedtls_ssl_send_t *f_send,
8153 mbedtls_ssl_recv_t *f_recv,
8154 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008155{
8156 ssl->p_bio = p_bio;
8157 ssl->f_send = f_send;
8158 ssl->f_recv = f_recv;
8159 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008160}
8161
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008162#if defined(MBEDTLS_SSL_PROTO_DTLS)
8163void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8164{
8165 ssl->mtu = mtu;
8166}
8167#endif
8168
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008169void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008170{
8171 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008172}
8173
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008174void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8175 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008176 mbedtls_ssl_set_timer_t *f_set_timer,
8177 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008178{
8179 ssl->p_timer = p_timer;
8180 ssl->f_set_timer = f_set_timer;
8181 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008182
8183 /* Make sure we start with no timer running */
8184 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008185}
8186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008188void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008189 void *p_cache,
8190 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8191 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008192{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008193 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008194 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008195 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199#if defined(MBEDTLS_SSL_CLI_C)
8200int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008201{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008202 int ret;
8203
8204 if( ssl == NULL ||
8205 session == NULL ||
8206 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008207 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008210 }
8211
Hanno Becker58fccf22019-02-06 14:30:46 +00008212 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8213 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008214 return( ret );
8215
Paul Bakker0a597072012-09-25 21:55:46 +00008216 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008217
8218 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008220#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008221
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008222void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008223 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008224{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008225 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8226 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8227 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8228 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008229}
8230
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008231void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008232 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008233 int major, int minor )
8234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008236 return;
8237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008238 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008239 return;
8240
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008241 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008242}
8243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008245void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008246 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008247{
8248 conf->cert_profile = profile;
8249}
8250
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008251/* Append a new keycert entry to a (possibly empty) list */
8252static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8253 mbedtls_x509_crt *cert,
8254 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008255{
niisato8ee24222018-06-25 19:05:48 +09008256 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008257
niisato8ee24222018-06-25 19:05:48 +09008258 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8259 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008260 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008261
niisato8ee24222018-06-25 19:05:48 +09008262 new_cert->cert = cert;
8263 new_cert->key = key;
8264 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008265
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008266 /* Update head is the list was null, else add to the end */
8267 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008268 {
niisato8ee24222018-06-25 19:05:48 +09008269 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008270 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008271 else
8272 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008273 mbedtls_ssl_key_cert *cur = *head;
8274 while( cur->next != NULL )
8275 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008276 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008277 }
8278
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008279 return( 0 );
8280}
8281
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008282int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008283 mbedtls_x509_crt *own_cert,
8284 mbedtls_pk_context *pk_key )
8285{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008286 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008287}
8288
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008289void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008290 mbedtls_x509_crt *ca_chain,
8291 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008292{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008293 conf->ca_chain = ca_chain;
8294 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008295}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008297
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008298#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8299int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8300 mbedtls_x509_crt *own_cert,
8301 mbedtls_pk_context *pk_key )
8302{
8303 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8304 own_cert, pk_key ) );
8305}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008306
8307void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8308 mbedtls_x509_crt *ca_chain,
8309 mbedtls_x509_crl *ca_crl )
8310{
8311 ssl->handshake->sni_ca_chain = ca_chain;
8312 ssl->handshake->sni_ca_crl = ca_crl;
8313}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008314
8315void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8316 int authmode )
8317{
8318 ssl->handshake->sni_authmode = authmode;
8319}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008320#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8321
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008322#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008323/*
8324 * Set EC J-PAKE password for current handshake
8325 */
8326int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8327 const unsigned char *pw,
8328 size_t pw_len )
8329{
8330 mbedtls_ecjpake_role role;
8331
Janos Follath8eb64132016-06-03 15:40:57 +01008332 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8334
8335 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8336 role = MBEDTLS_ECJPAKE_SERVER;
8337 else
8338 role = MBEDTLS_ECJPAKE_CLIENT;
8339
8340 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8341 role,
8342 MBEDTLS_MD_SHA256,
8343 MBEDTLS_ECP_DP_SECP256R1,
8344 pw, pw_len ) );
8345}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008346#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008349int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008350 const unsigned char *psk, size_t psk_len,
8351 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008352{
Paul Bakker6db455e2013-09-18 17:29:31 +02008353 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008356 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8357 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008358
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008359 /* Identity len will be encoded on two bytes */
8360 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008361 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008362 {
8363 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8364 }
8365
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008366 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008367 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008368 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008369
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008370 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008371 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008372 conf->psk_len = 0;
8373 }
8374 if( conf->psk_identity != NULL )
8375 {
8376 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008377 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008378 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008379 }
8380
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008381 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8382 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008383 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008384 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008385 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008386 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008387 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008388 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008389 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008390
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008391 conf->psk_len = psk_len;
8392 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008393
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008394 memcpy( conf->psk, psk, conf->psk_len );
8395 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008396
8397 return( 0 );
8398}
8399
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008400int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8401 const unsigned char *psk, size_t psk_len )
8402{
8403 if( psk == NULL || ssl->handshake == NULL )
8404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8405
8406 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8408
8409 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008410 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008411 mbedtls_platform_zeroize( ssl->handshake->psk,
8412 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008413 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008414 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008415 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008416
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008417 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008418 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008419
8420 ssl->handshake->psk_len = psk_len;
8421 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8422
8423 return( 0 );
8424}
8425
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008426void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008427 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008428 size_t),
8429 void *p_psk )
8430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008431 conf->f_psk = f_psk;
8432 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008435
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008436#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008437
8438#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008440{
8441 int ret;
8442
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008443 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8444 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8445 {
8446 mbedtls_mpi_free( &conf->dhm_P );
8447 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008448 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008449 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008450
8451 return( 0 );
8452}
Hanno Becker470a8c42017-10-04 15:28:46 +01008453#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008454
Hanno Beckera90658f2017-10-04 15:29:08 +01008455int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8456 const unsigned char *dhm_P, size_t P_len,
8457 const unsigned char *dhm_G, size_t G_len )
8458{
8459 int ret;
8460
8461 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8462 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8463 {
8464 mbedtls_mpi_free( &conf->dhm_P );
8465 mbedtls_mpi_free( &conf->dhm_G );
8466 return( ret );
8467 }
8468
8469 return( 0 );
8470}
Paul Bakker5121ce52009-01-03 21:22:43 +00008471
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008472int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008473{
8474 int ret;
8475
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008476 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8477 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8478 {
8479 mbedtls_mpi_free( &conf->dhm_P );
8480 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008481 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008482 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008483
8484 return( 0 );
8485}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008486#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008487
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008488#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8489/*
8490 * Set the minimum length for Diffie-Hellman parameters
8491 */
8492void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8493 unsigned int bitlen )
8494{
8495 conf->dhm_min_bitlen = bitlen;
8496}
8497#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8498
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008499#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008500/*
8501 * Set allowed/preferred hashes for handshake signatures
8502 */
8503void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8504 const int *hashes )
8505{
8506 conf->sig_hashes = hashes;
8507}
Hanno Becker947194e2017-04-07 13:25:49 +01008508#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008509
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008510#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008511/*
8512 * Set the allowed elliptic curves
8513 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008514void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008516{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008517 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008518}
Hanno Becker947194e2017-04-07 13:25:49 +01008519#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008520
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008521#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008522int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008523{
Hanno Becker947194e2017-04-07 13:25:49 +01008524 /* Initialize to suppress unnecessary compiler warning */
8525 size_t hostname_len = 0;
8526
8527 /* Check if new hostname is valid before
8528 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008529 if( hostname != NULL )
8530 {
8531 hostname_len = strlen( hostname );
8532
8533 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8535 }
8536
8537 /* Now it's clear that we will overwrite the old hostname,
8538 * so we can free it safely */
8539
8540 if( ssl->hostname != NULL )
8541 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008542 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008543 mbedtls_free( ssl->hostname );
8544 }
8545
8546 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008547
Paul Bakker5121ce52009-01-03 21:22:43 +00008548 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008549 {
8550 ssl->hostname = NULL;
8551 }
8552 else
8553 {
8554 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008555 if( ssl->hostname == NULL )
8556 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008557
Hanno Becker947194e2017-04-07 13:25:49 +01008558 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008559
Hanno Becker947194e2017-04-07 13:25:49 +01008560 ssl->hostname[hostname_len] = '\0';
8561 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008562
8563 return( 0 );
8564}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008565#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008566
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008567#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008568void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008570 const unsigned char *, size_t),
8571 void *p_sni )
8572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008573 conf->f_sni = f_sni;
8574 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008580{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008581 size_t cur_len, tot_len;
8582 const char **p;
8583
8584 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008585 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8586 * MUST NOT be truncated."
8587 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008588 */
8589 tot_len = 0;
8590 for( p = protos; *p != NULL; p++ )
8591 {
8592 cur_len = strlen( *p );
8593 tot_len += cur_len;
8594
8595 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008597 }
8598
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008599 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008600
8601 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008602}
8603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008604const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008605{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008606 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008609
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008610void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008611{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008612 conf->max_major_ver = major;
8613 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008614}
8615
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008616void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008618 conf->min_major_ver = major;
8619 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008620}
8621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008622#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008623void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008624{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008625 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008626}
8627#endif
8628
Janos Follath088ce432017-04-10 12:42:31 +01008629#if defined(MBEDTLS_SSL_SRV_C)
8630void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8631 char cert_req_ca_list )
8632{
8633 conf->cert_req_ca_list = cert_req_ca_list;
8634}
8635#endif
8636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008638void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008640 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008641}
8642#endif
8643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008645#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008646void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008648 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008649}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008650#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8651#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008652void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008653 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008654{
8655 conf->enforce_extended_master_secret = ems_enf;
8656}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008657#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008658#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008659
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008660#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008661void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008663 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008664}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008665#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008667#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008671 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008674 }
8675
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008676 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008677
8678 return( 0 );
8679}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008682#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008683void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008685 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008690void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008691{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008692 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008693}
8694#endif
8695
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008696#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008697void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008698{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008699 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008700}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008701#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008703#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008704void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008705{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008706 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008707}
8708
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008709void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008711 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008712}
8713
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008714void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008715 const unsigned char period[8] )
8716{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008717 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008718}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008719#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008721#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008722#if defined(MBEDTLS_SSL_CLI_C)
8723void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008724{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008725 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008726}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008727#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008728
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008729#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008730void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8731 mbedtls_ssl_ticket_write_t *f_ticket_write,
8732 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8733 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008734{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008735 conf->f_ticket_write = f_ticket_write;
8736 conf->f_ticket_parse = f_ticket_parse;
8737 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008738}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008739#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008740#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008741
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008742#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8743void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8744 mbedtls_ssl_export_keys_t *f_export_keys,
8745 void *p_export_keys )
8746{
8747 conf->f_export_keys = f_export_keys;
8748 conf->p_export_keys = p_export_keys;
8749}
8750#endif
8751
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008752#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008753void mbedtls_ssl_conf_async_private_cb(
8754 mbedtls_ssl_config *conf,
8755 mbedtls_ssl_async_sign_t *f_async_sign,
8756 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8757 mbedtls_ssl_async_resume_t *f_async_resume,
8758 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008759 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008760{
8761 conf->f_async_sign_start = f_async_sign;
8762 conf->f_async_decrypt_start = f_async_decrypt;
8763 conf->f_async_resume = f_async_resume;
8764 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008765 conf->p_async_config_data = async_config_data;
8766}
8767
Gilles Peskine8f97af72018-04-26 11:46:10 +02008768void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8769{
8770 return( conf->p_async_config_data );
8771}
8772
Gilles Peskine1febfef2018-04-30 11:54:39 +02008773void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008774{
8775 if( ssl->handshake == NULL )
8776 return( NULL );
8777 else
8778 return( ssl->handshake->user_async_ctx );
8779}
8780
Gilles Peskine1febfef2018-04-30 11:54:39 +02008781void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008782 void *ctx )
8783{
8784 if( ssl->handshake != NULL )
8785 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008786}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008787#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008788
Paul Bakker5121ce52009-01-03 21:22:43 +00008789/*
8790 * SSL get accessors
8791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008792size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008793{
8794 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8795}
8796
Hanno Becker8b170a02017-10-10 11:51:19 +01008797int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8798{
8799 /*
8800 * Case A: We're currently holding back
8801 * a message for further processing.
8802 */
8803
8804 if( ssl->keep_current_message == 1 )
8805 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008806 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008807 return( 1 );
8808 }
8809
8810 /*
8811 * Case B: Further records are pending in the current datagram.
8812 */
8813
8814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008815 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008816 ssl->in_left > ssl->next_record_offset )
8817 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008818 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008819 return( 1 );
8820 }
8821#endif /* MBEDTLS_SSL_PROTO_DTLS */
8822
8823 /*
8824 * Case C: A handshake message is being processed.
8825 */
8826
Hanno Becker8b170a02017-10-10 11:51:19 +01008827 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8828 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008829 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008830 return( 1 );
8831 }
8832
8833 /*
8834 * Case D: An application data message is being processed
8835 */
8836 if( ssl->in_offt != NULL )
8837 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008838 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008839 return( 1 );
8840 }
8841
8842 /*
8843 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008844 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008845 * we implement support for multiple alerts in single records.
8846 */
8847
8848 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8849 return( 0 );
8850}
8851
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008852uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008853{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008854 if( ssl->session != NULL )
8855 return( ssl->session->verify_result );
8856
8857 if( ssl->session_negotiate != NULL )
8858 return( ssl->session_negotiate->verify_result );
8859
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008860 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008861}
8862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008863const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008864{
Paul Bakker926c8e42013-03-06 10:23:34 +01008865 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008866 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008869}
8870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008871const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008872{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008874 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008875 {
8876 switch( ssl->minor_ver )
8877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008879 return( "DTLSv1.0" );
8880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008881 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008882 return( "DTLSv1.2" );
8883
8884 default:
8885 return( "unknown (DTLS)" );
8886 }
8887 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008888 MBEDTLS_SSL_TRANSPORT_ELSE
8889#endif /* MBEDTLS_SSL_PROTO_DTLS */
8890#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008891 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008892 switch( ssl->minor_ver )
8893 {
8894 case MBEDTLS_SSL_MINOR_VERSION_0:
8895 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008896
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008897 case MBEDTLS_SSL_MINOR_VERSION_1:
8898 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008899
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008900 case MBEDTLS_SSL_MINOR_VERSION_2:
8901 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008902
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008903 case MBEDTLS_SSL_MINOR_VERSION_3:
8904 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008905
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008906 default:
8907 return( "unknown" );
8908 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008909 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008910#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008911}
8912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008913int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008914{
Hanno Becker3136ede2018-08-17 15:28:19 +01008915 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008916 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008917 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008918
Hanno Becker43395762019-05-03 14:46:38 +01008919 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8920
Hanno Becker78640902018-08-13 16:35:15 +01008921 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008922 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008924#if defined(MBEDTLS_ZLIB_SUPPORT)
8925 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8926 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008927#endif
8928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008929 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008931 case MBEDTLS_MODE_GCM:
8932 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008933 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008935 transform_expansion = transform->minlen;
8936 break;
8937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008939
8940 block_size = mbedtls_cipher_get_block_size(
8941 &transform->cipher_ctx_enc );
8942
Hanno Becker3136ede2018-08-17 15:28:19 +01008943 /* Expansion due to the addition of the MAC. */
8944 transform_expansion += transform->maclen;
8945
8946 /* Expansion due to the addition of CBC padding;
8947 * Theoretically up to 256 bytes, but we never use
8948 * more than the block size of the underlying cipher. */
8949 transform_expansion += block_size;
8950
8951 /* For TLS 1.1 or higher, an explicit IV is added
8952 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008953#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8954 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008955 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008956#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008957
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008958 break;
8959
8960 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008963 }
8964
Hanno Beckera5a2b082019-05-15 14:03:01 +01008965#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008966 if( transform->out_cid_len != 0 )
8967 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008968#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008969
Hanno Becker43395762019-05-03 14:46:38 +01008970 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008971}
8972
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008973#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8974size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8975{
8976 size_t max_len;
8977
8978 /*
8979 * Assume mfl_code is correct since it was checked when set
8980 */
Angus Grattond8213d02016-05-25 20:56:48 +10008981 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008982
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008983 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008984 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008985 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008986 {
Angus Grattond8213d02016-05-25 20:56:48 +10008987 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008988 }
8989
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008990 /* During a handshake, use the value being negotiated */
8991 if( ssl->session_negotiate != NULL &&
8992 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8993 {
8994 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8995 }
8996
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008997 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008998}
8999#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9000
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009001#if defined(MBEDTLS_SSL_PROTO_DTLS)
9002static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9003{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009004 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9005 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9006 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9007 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9008 return ( 0 );
9009
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009010 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9011 return( ssl->mtu );
9012
9013 if( ssl->mtu == 0 )
9014 return( ssl->handshake->mtu );
9015
9016 return( ssl->mtu < ssl->handshake->mtu ?
9017 ssl->mtu : ssl->handshake->mtu );
9018}
9019#endif /* MBEDTLS_SSL_PROTO_DTLS */
9020
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009021int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9022{
9023 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9024
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009025#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9026 !defined(MBEDTLS_SSL_PROTO_DTLS)
9027 (void) ssl;
9028#endif
9029
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009030#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9031 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9032
9033 if( max_len > mfl )
9034 max_len = mfl;
9035#endif
9036
9037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009038 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009039 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009040 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009041 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9042 const size_t overhead = (size_t) ret;
9043
9044 if( ret < 0 )
9045 return( ret );
9046
9047 if( mtu <= overhead )
9048 {
9049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9050 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9051 }
9052
9053 if( max_len > mtu - overhead )
9054 max_len = mtu - overhead;
9055 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009056#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009057
Hanno Becker0defedb2018-08-10 12:35:02 +01009058#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9059 !defined(MBEDTLS_SSL_PROTO_DTLS)
9060 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009061#endif
9062
9063 return( (int) max_len );
9064}
9065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009066#if defined(MBEDTLS_X509_CRT_PARSE_C)
9067const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009068{
9069 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009070 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009071
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009072#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009073 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009074#else
9075 return( NULL );
9076#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009077}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009081int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9082 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009083{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009084 if( ssl == NULL ||
9085 dst == NULL ||
9086 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009087 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009090 }
9091
Hanno Becker58fccf22019-02-06 14:30:46 +00009092 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009095
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009096const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9097{
9098 if( ssl == NULL )
9099 return( NULL );
9100
9101 return( ssl->session );
9102}
9103
Paul Bakker5121ce52009-01-03 21:22:43 +00009104/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009105 * Define ticket header determining Mbed TLS version
9106 * and structure of the ticket.
9107 */
9108
Hanno Becker41527622019-05-16 12:50:45 +01009109/*
Hanno Becker26829e92019-05-28 14:30:45 +01009110 * Define bitflag determining compile-time settings influencing
9111 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009112 */
9113
Hanno Becker26829e92019-05-28 14:30:45 +01009114#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009115#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009116#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009117#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009118#endif /* MBEDTLS_HAVE_TIME */
9119
9120#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009121#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009122#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009123#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009124#endif /* MBEDTLS_X509_CRT_PARSE_C */
9125
9126#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009127#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009128#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009129#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009130#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9131
9132#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009133#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009134#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009135#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009136#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9137
9138#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009139#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009140#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009141#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009142#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9143
9144#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009145#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009146#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009147#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009148#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9149
Hanno Becker41527622019-05-16 12:50:45 +01009150#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9151#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9152#else
9153#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9154#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9155
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009156#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9157#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9158#else
9159#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9160#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9161
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009162#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9163#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9164#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9165#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9166#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9167#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9168#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009169#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009170
Hanno Becker26829e92019-05-28 14:30:45 +01009171#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009172 ( (uint16_t) ( \
9173 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9174 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9175 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9176 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9177 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9178 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009179 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9180 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009181
Hanno Becker557fe9f2019-05-16 12:41:07 +01009182static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009183 MBEDTLS_VERSION_MAJOR,
9184 MBEDTLS_VERSION_MINOR,
9185 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009186 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9187 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009188};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009189
9190/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009191 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009192 * (in the presentation language of TLS, RFC 8446 section 3)
9193 *
Hanno Becker26829e92019-05-28 14:30:45 +01009194 * opaque mbedtls_version[3]; // major, minor, patch
9195 * opaque session_format[2]; // version-specific 16-bit field determining
9196 * // the format of the remaining
9197 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009198 *
9199 * Note: When updating the format, remember to keep
9200 * these version+format bytes.
9201 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009202 * // In this version, `session_format` determines
9203 * // the setting of those compile-time
9204 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009205 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009206 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009207 * uint8 ciphersuite[2]; // defined by the standard
9208 * uint8 compression; // 0 or 1
9209 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009210 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009211 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009212 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009213 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9214 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9215 * case disabled: uint8_t peer_cert_digest_type;
9216 * opaque peer_cert_digest<0..2^8-1>;
9217 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009218 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009219 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009220 * uint8 mfl_code; // up to 255 according to standard
9221 * uint8 trunc_hmac; // 0 or 1
9222 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009223 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009224 * The order is the same as in the definition of the structure, except
9225 * verify_result is put before peer_cert so that all mandatory fields come
9226 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009227 */
9228int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9229 unsigned char *buf,
9230 size_t buf_len,
9231 size_t *olen )
9232{
9233 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009234 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009235#if defined(MBEDTLS_HAVE_TIME)
9236 uint64_t start;
9237#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009238#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009239#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009240 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009241#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009242#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009243
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009244 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009245 * Add version identifier
9246 */
9247
9248 used += sizeof( ssl_serialized_session_header );
9249
9250 if( used <= buf_len )
9251 {
9252 memcpy( p, ssl_serialized_session_header,
9253 sizeof( ssl_serialized_session_header ) );
9254 p += sizeof( ssl_serialized_session_header );
9255 }
9256
9257 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009258 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009259 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009260#if defined(MBEDTLS_HAVE_TIME)
9261 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009262
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009263 if( used <= buf_len )
9264 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009265 start = (uint64_t) session->start;
9266
9267 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9268 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9269 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9270 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9271 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9272 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9273 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9274 *p++ = (unsigned char)( ( start ) & 0xFF );
9275 }
9276#endif /* MBEDTLS_HAVE_TIME */
9277
9278 /*
9279 * Basic mandatory fields
9280 */
9281 used += 2 /* ciphersuite */
9282 + 1 /* compression */
9283 + 1 /* id_len */
9284 + sizeof( session->id )
9285 + sizeof( session->master )
9286 + 4; /* verify_result */
9287
9288 if( used <= buf_len )
9289 {
9290 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9291 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9292
9293 *p++ = (unsigned char)( session->compression & 0xFF );
9294
9295 *p++ = (unsigned char)( session->id_len & 0xFF );
9296 memcpy( p, session->id, 32 );
9297 p += 32;
9298
9299 memcpy( p, session->master, 48 );
9300 p += 48;
9301
9302 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9303 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9304 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9305 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009306 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009307
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009308 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009309 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009310 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009311#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009312#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009313 if( session->peer_cert == NULL )
9314 cert_len = 0;
9315 else
9316 cert_len = session->peer_cert->raw.len;
9317
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009318 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009319
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009320 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009321 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009322 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9323 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9324 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9325
9326 if( session->peer_cert != NULL )
9327 {
9328 memcpy( p, session->peer_cert->raw.p, cert_len );
9329 p += cert_len;
9330 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009331 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009332
Hanno Becker5882dd02019-06-06 16:25:57 +01009333#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009334 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009335 if( session->peer_cert_digest != NULL )
9336 {
9337 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9338 if( used <= buf_len )
9339 {
9340 *p++ = (unsigned char) session->peer_cert_digest_type;
9341 *p++ = (unsigned char) session->peer_cert_digest_len;
9342 memcpy( p, session->peer_cert_digest,
9343 session->peer_cert_digest_len );
9344 p += session->peer_cert_digest_len;
9345 }
9346 }
9347 else
9348 {
9349 used += 2;
9350 if( used <= buf_len )
9351 {
9352 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9353 *p++ = 0;
9354 }
9355 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009356#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009357#endif /* MBEDTLS_X509_CRT_PARSE_C */
9358
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009359 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009360 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009361 */
9362#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009363 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009364
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009365 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009366 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009367 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9368 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9369 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9370
9371 if( session->ticket != NULL )
9372 {
9373 memcpy( p, session->ticket, session->ticket_len );
9374 p += session->ticket_len;
9375 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009376
9377 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9378 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9379 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9380 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009381 }
9382#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9383
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009384 /*
9385 * Misc extension-related info
9386 */
9387#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9388 used += 1;
9389
9390 if( used <= buf_len )
9391 *p++ = session->mfl_code;
9392#endif
9393
9394#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9395 used += 1;
9396
9397 if( used <= buf_len )
9398 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9399#endif
9400
9401#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9402 used += 1;
9403
9404 if( used <= buf_len )
9405 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9406#endif
9407
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009408 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009409 *olen = used;
9410
9411 if( used > buf_len )
9412 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009413
9414 return( 0 );
9415}
9416
9417/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009418 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009419 *
9420 * This internal version is wrapped by a public function that cleans up in
9421 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009422 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009423static int ssl_session_load( mbedtls_ssl_session *session,
9424 const unsigned char *buf,
9425 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009426{
9427 const unsigned char *p = buf;
9428 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009429#if defined(MBEDTLS_HAVE_TIME)
9430 uint64_t start;
9431#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009432#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009433#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009434 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009435#endif
9436#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009437
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009438 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009439 * Check version identifier
9440 */
9441
9442 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009443 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009444
9445 if( memcmp( p, ssl_serialized_session_header,
9446 sizeof( ssl_serialized_session_header ) ) != 0 )
9447 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009448 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009449 }
9450 p += sizeof( ssl_serialized_session_header );
9451
9452 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009453 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009454 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009455#if defined(MBEDTLS_HAVE_TIME)
9456 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9458
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009459 start = ( (uint64_t) p[0] << 56 ) |
9460 ( (uint64_t) p[1] << 48 ) |
9461 ( (uint64_t) p[2] << 40 ) |
9462 ( (uint64_t) p[3] << 32 ) |
9463 ( (uint64_t) p[4] << 24 ) |
9464 ( (uint64_t) p[5] << 16 ) |
9465 ( (uint64_t) p[6] << 8 ) |
9466 ( (uint64_t) p[7] );
9467 p += 8;
9468
9469 session->start = (time_t) start;
9470#endif /* MBEDTLS_HAVE_TIME */
9471
9472 /*
9473 * Basic mandatory fields
9474 */
9475 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9476 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9477
9478 session->ciphersuite = ( p[0] << 8 ) | p[1];
9479 p += 2;
9480
9481 session->compression = *p++;
9482
9483 session->id_len = *p++;
9484 memcpy( session->id, p, 32 );
9485 p += 32;
9486
9487 memcpy( session->master, p, 48 );
9488 p += 48;
9489
9490 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9491 ( (uint32_t) p[1] << 16 ) |
9492 ( (uint32_t) p[2] << 8 ) |
9493 ( (uint32_t) p[3] );
9494 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009495
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009496 /* Immediately clear invalid pointer values that have been read, in case
9497 * we exit early before we replaced them with valid ones. */
9498#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009499#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009500 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009501#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009502 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009503#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009504#endif
9505#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9506 session->ticket = NULL;
9507#endif
9508
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009509 /*
9510 * Peer certificate
9511 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009512#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009513#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009514 if( 3 > (size_t)( end - p ) )
9515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9516
9517 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9518 p += 3;
9519
9520 if( cert_len == 0 )
9521 {
9522 session->peer_cert = NULL;
9523 }
9524 else
9525 {
9526 int ret;
9527
9528 if( cert_len > (size_t)( end - p ) )
9529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9530
9531 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9532
9533 if( session->peer_cert == NULL )
9534 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9535
9536 mbedtls_x509_crt_init( session->peer_cert );
9537
9538 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9539 p, cert_len ) ) != 0 )
9540 {
9541 mbedtls_x509_crt_free( session->peer_cert );
9542 mbedtls_free( session->peer_cert );
9543 session->peer_cert = NULL;
9544 return( ret );
9545 }
9546
9547 p += cert_len;
9548 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009549#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009550 /* Deserialize CRT digest from the end of the ticket. */
9551 if( 2 > (size_t)( end - p ) )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
9554 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9555 session->peer_cert_digest_len = (size_t) *p++;
9556
9557 if( session->peer_cert_digest_len != 0 )
9558 {
Hanno Becker2326d202019-06-06 14:54:55 +01009559 const mbedtls_md_info_t *md_info =
9560 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9561 if( md_info == NULL )
9562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9563 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9565
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009566 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9567 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9568
9569 session->peer_cert_digest =
9570 mbedtls_calloc( 1, session->peer_cert_digest_len );
9571 if( session->peer_cert_digest == NULL )
9572 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9573
9574 memcpy( session->peer_cert_digest, p,
9575 session->peer_cert_digest_len );
9576 p += session->peer_cert_digest_len;
9577 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009578#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009579#endif /* MBEDTLS_X509_CRT_PARSE_C */
9580
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009581 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009582 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009583 */
9584#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9585 if( 3 > (size_t)( end - p ) )
9586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9587
9588 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9589 p += 3;
9590
9591 if( session->ticket_len != 0 )
9592 {
9593 if( session->ticket_len > (size_t)( end - p ) )
9594 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9595
9596 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9597 if( session->ticket == NULL )
9598 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9599
9600 memcpy( session->ticket, p, session->ticket_len );
9601 p += session->ticket_len;
9602 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009603
9604 if( 4 > (size_t)( end - p ) )
9605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9606
9607 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9608 ( (uint32_t) p[1] << 16 ) |
9609 ( (uint32_t) p[2] << 8 ) |
9610 ( (uint32_t) p[3] );
9611 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009612#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9613
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009614 /*
9615 * Misc extension-related info
9616 */
9617#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9618 if( 1 > (size_t)( end - p ) )
9619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9620
9621 session->mfl_code = *p++;
9622#endif
9623
9624#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9625 if( 1 > (size_t)( end - p ) )
9626 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9627
9628 session->trunc_hmac = *p++;
9629#endif
9630
9631#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9632 if( 1 > (size_t)( end - p ) )
9633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9634
9635 session->encrypt_then_mac = *p++;
9636#endif
9637
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009638 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009639 if( p != end )
9640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9641
9642 return( 0 );
9643}
9644
9645/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009646 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009647 */
9648int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9649 const unsigned char *buf,
9650 size_t len )
9651{
9652 int ret = ssl_session_load( session, buf, len );
9653
9654 if( ret != 0 )
9655 mbedtls_ssl_session_free( session );
9656
9657 return( ret );
9658}
9659
9660/*
Paul Bakker1961b702013-01-25 14:49:24 +01009661 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009662 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009666
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009667 if( ssl == NULL || ssl->conf == NULL )
9668 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009671 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009673#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009677#endif
9678
Paul Bakker1961b702013-01-25 14:49:24 +01009679 return( ret );
9680}
9681
9682/*
9683 * Perform the SSL handshake
9684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009686{
9687 int ret = 0;
9688
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009689 if( ssl == NULL || ssl->conf == NULL )
9690 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009697
9698 if( ret != 0 )
9699 break;
9700 }
9701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009703
9704 return( ret );
9705}
9706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707#if defined(MBEDTLS_SSL_RENEGOTIATION)
9708#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009709/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009710 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009713{
9714 int ret;
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009717
9718 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9720 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009721
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009722 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009723 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009725 return( ret );
9726 }
9727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009729
9730 return( 0 );
9731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009733
9734/*
9735 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 * - any side: calling mbedtls_ssl_renegotiate(),
9737 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9738 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009739 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009740 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009744{
9745 int ret;
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009748
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009749 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9750 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009751
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009752 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9753 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009755 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009757 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009758 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009759 ssl->handshake->out_msg_seq = 1;
9760 else
9761 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009762 }
9763#endif
9764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9766 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009771 return( ret );
9772 }
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009775
9776 return( 0 );
9777}
9778
9779/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009780 * Renegotiate current connection on client,
9781 * or request renegotiation on server
9782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009784{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009786
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009787 if( ssl == NULL || ssl->conf == NULL )
9788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009791 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009792 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009798
9799 /* Did we already try/start sending HelloRequest? */
9800 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009801 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009802
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009803 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009804 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009808 /*
9809 * On client, either start the renegotiation process or,
9810 * if already in progress, continue the handshake
9811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9815 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009816
9817 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009820 return( ret );
9821 }
9822 }
9823 else
9824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009828 return( ret );
9829 }
9830 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009832
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009833 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009834}
9835
9836/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009837 * Check record counters and renegotiate if they're above the limit.
9838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009840{
Andres AG2196c7f2016-12-15 17:01:16 +00009841 size_t ep_len = ssl_ep_len( ssl );
9842 int in_ctr_cmp;
9843 int out_ctr_cmp;
9844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9846 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009847 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009848 {
9849 return( 0 );
9850 }
9851
Andres AG2196c7f2016-12-15 17:01:16 +00009852 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9853 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009854 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009855 ssl->conf->renego_period + ep_len, 8 - ep_len );
9856
9857 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009858 {
9859 return( 0 );
9860 }
9861
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009866
9867/*
9868 * Receive application data decrypted from the SSL layer
9869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009870int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009871{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009872 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009873 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009874
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009875 if( ssl == NULL || ssl->conf == NULL )
9876 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009881 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009884 return( ret );
9885
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009886 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009888 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009889 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009890 return( ret );
9891 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009892 }
9893#endif
9894
Hanno Becker4a810fb2017-05-24 16:27:30 +01009895 /*
9896 * Check if renegotiation is necessary and/or handshake is
9897 * in process. If yes, perform/continue, and fall through
9898 * if an unexpected packet is received while the client
9899 * is waiting for the ServerHello.
9900 *
9901 * (There is no equivalent to the last condition on
9902 * the server-side as it is not treated as within
9903 * a handshake while waiting for the ClientHello
9904 * after a renegotiation request.)
9905 */
9906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009908 ret = ssl_check_ctr_renegotiate( ssl );
9909 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9910 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009913 return( ret );
9914 }
9915#endif
9916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009920 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9921 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009924 return( ret );
9925 }
9926 }
9927
Hanno Beckere41158b2017-10-23 13:30:32 +01009928 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009929 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009930 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009931 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009932 if( ssl->f_get_timer != NULL &&
9933 ssl->f_get_timer( ssl->p_timer ) == -1 )
9934 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009935 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009936 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009937
Hanno Becker327c93b2018-08-15 13:56:18 +01009938 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009939 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009940 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9941 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009942
Hanno Becker4a810fb2017-05-24 16:27:30 +01009943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9944 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009945 }
9946
9947 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009948 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009949 {
9950 /*
9951 * OpenSSL sends empty messages to randomize the IV
9952 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009953 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009956 return( 0 );
9957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009959 return( ret );
9960 }
9961 }
9962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009966
Hanno Becker4a810fb2017-05-24 16:27:30 +01009967 /*
9968 * - For client-side, expect SERVER_HELLO_REQUEST.
9969 * - For server-side, expect CLIENT_HELLO.
9970 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9971 */
9972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009976 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009979
9980 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009982 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009983 {
9984 continue;
9985 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009986 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009987#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009988#if defined(MBEDTLS_SSL_PROTO_TLS)
9989 {
9990 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9991 }
9992#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009993 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009994#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009995
Hanno Becker4a810fb2017-05-24 16:27:30 +01009996#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010001
10002 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010004 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010005 {
10006 continue;
10007 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010008 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010009#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010010#if defined(MBEDTLS_SSL_PROTO_TLS)
10011 {
10012 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10013 }
10014#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010015 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010016#endif /* MBEDTLS_SSL_SRV_C */
10017
Hanno Becker21df7f92017-10-17 11:03:26 +010010018#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010020 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10021 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010022 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010023 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10024 {
10025 /*
10026 * Accept renegotiation request
10027 */
Paul Bakker48916f92012-09-16 19:57:18 +000010028
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010029 /* DTLS clients need to know renego is server-initiated */
10030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010031 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010032 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10033 {
10034 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10035 }
10036#endif
10037 ret = ssl_start_renegotiation( ssl );
10038 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10039 ret != 0 )
10040 {
10041 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10042 return( ret );
10043 }
10044 }
10045 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010046#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010047 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010048 /*
10049 * Refuse renegotiation
10050 */
10051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_SSL_PROTO_SSL3)
10055 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010056 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010057 /* SSLv3 does not have a "no_renegotiation" warning, so
10058 we send a fatal alert and abort the connection. */
10059 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10060 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10061 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010062 }
10063 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10065#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10066 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10067 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10070 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10071 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010072 {
10073 return( ret );
10074 }
Paul Bakker48916f92012-09-16 19:57:18 +000010075 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10078 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10081 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010082 }
Paul Bakker48916f92012-09-16 19:57:18 +000010083 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010084
Hanno Becker90333da2017-10-10 11:27:13 +010010085 /* At this point, we don't know whether the renegotiation has been
10086 * completed or not. The cases to consider are the following:
10087 * 1) The renegotiation is complete. In this case, no new record
10088 * has been read yet.
10089 * 2) The renegotiation is incomplete because the client received
10090 * an application data record while awaiting the ServerHello.
10091 * 3) The renegotiation is incomplete because the client received
10092 * a non-handshake, non-application data message while awaiting
10093 * the ServerHello.
10094 * In each of these case, looping will be the proper action:
10095 * - For 1), the next iteration will read a new record and check
10096 * if it's application data.
10097 * - For 2), the loop condition isn't satisfied as application data
10098 * is present, hence continue is the same as break
10099 * - For 3), the loop condition is satisfied and read_record
10100 * will re-deliver the message that was held back by the client
10101 * when expecting the ServerHello.
10102 */
10103 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010104 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010105#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010107 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010108 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010109 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010110 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010113 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010115 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010116 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10121 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010124 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010125 }
10126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010127 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10130 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010131 }
10132
10133 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010134
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010135 /* We're going to return something now, cancel timer,
10136 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010138 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010139
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010141 /* If we requested renego but received AppData, resend HelloRequest.
10142 * Do it now, after setting in_offt, to avoid taking this branch
10143 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010145 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010147 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010148 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010151 return( ret );
10152 }
10153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010155#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010156 }
10157
10158 n = ( len < ssl->in_msglen )
10159 ? len : ssl->in_msglen;
10160
10161 memcpy( buf, ssl->in_offt, n );
10162 ssl->in_msglen -= n;
10163
10164 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010165 {
10166 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010167 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010168 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010169 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010170 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010171 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010172 /* more data available */
10173 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010174 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010177
Paul Bakker23986e52011-04-24 08:57:21 +000010178 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010179}
10180
10181/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010182 * Send application data to be encrypted by the SSL layer, taking care of max
10183 * fragment length and buffer size.
10184 *
10185 * According to RFC 5246 Section 6.2.1:
10186 *
10187 * Zero-length fragments of Application data MAY be sent as they are
10188 * potentially useful as a traffic analysis countermeasure.
10189 *
10190 * Therefore, it is possible that the input message length is 0 and the
10191 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010192 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010193static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010194 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010195{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010196 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10197 const size_t max_len = (size_t) ret;
10198
10199 if( ret < 0 )
10200 {
10201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10202 return( ret );
10203 }
10204
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010205 if( len > max_len )
10206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010208 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010211 "maximum fragment length: %d > %d",
10212 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010214 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010215 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010216#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010217#if defined(MBEDTLS_SSL_PROTO_TLS)
10218 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010219 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010220 }
10221#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010222 }
Paul Bakker887bd502011-06-08 13:10:54 +000010223
Paul Bakker5121ce52009-01-03 21:22:43 +000010224 if( ssl->out_left != 0 )
10225 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010226 /*
10227 * The user has previously tried to send the data and
10228 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10229 * written. In this case, we expect the high-level write function
10230 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010234 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010235 return( ret );
10236 }
10237 }
Paul Bakker887bd502011-06-08 13:10:54 +000010238 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010239 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010240 /*
10241 * The user is trying to send a message the first time, so we need to
10242 * copy the data into the internal buffers and setup the data structure
10243 * to keep track of partial writes
10244 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010245 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010247 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010248
Hanno Becker67bc7c32018-08-06 11:33:50 +010010249 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010252 return( ret );
10253 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010254 }
10255
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010256 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010257}
10258
10259/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010260 * Write application data, doing 1/n-1 splitting if necessary.
10261 *
10262 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010263 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010264 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010267static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010268 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010269{
10270 int ret;
10271
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010272 if( ssl->conf->cbc_record_splitting ==
10273 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010274 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10276 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10277 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010278 {
10279 return( ssl_write_real( ssl, buf, len ) );
10280 }
10281
10282 if( ssl->split_done == 0 )
10283 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010284 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010285 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010286 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010287 }
10288
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010289 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10290 return( ret );
10291 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010292
10293 return( ret + 1 );
10294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010296
10297/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010298 * Write application data (public-facing wrapper)
10299 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010300int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010301{
10302 int ret;
10303
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010305
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010306 if( ssl == NULL || ssl->conf == NULL )
10307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10308
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010309#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010310 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10311 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010312 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010313 return( ret );
10314 }
10315#endif
10316
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010317 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010318 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010319 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010320 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010321 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010322 return( ret );
10323 }
10324 }
10325
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010326#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010327 ret = ssl_write_split( ssl, buf, len );
10328#else
10329 ret = ssl_write_real( ssl, buf, len );
10330#endif
10331
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010333
10334 return( ret );
10335}
10336
10337/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010338 * Notify the peer that the connection is being closed
10339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010341{
10342 int ret;
10343
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010344 if( ssl == NULL || ssl->conf == NULL )
10345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010348
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010349 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010350 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10355 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10356 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010359 return( ret );
10360 }
10361 }
10362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010364
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010365 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010366}
10367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010369{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010370 if( transform == NULL )
10371 return;
10372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010374 deflateEnd( &transform->ctx_deflate );
10375 inflateEnd( &transform->ctx_inflate );
10376#endif
10377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010378 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10379 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010380
Hanno Becker92231322018-01-03 15:32:51 +000010381#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382 mbedtls_md_free( &transform->md_ctx_enc );
10383 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010384#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010385
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010386 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010387}
10388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010389#if defined(MBEDTLS_X509_CRT_PARSE_C)
10390static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010391{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010393
10394 while( cur != NULL )
10395 {
10396 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010398 cur = next;
10399 }
10400}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010402
Hanno Becker0271f962018-08-16 13:23:47 +010010403#if defined(MBEDTLS_SSL_PROTO_DTLS)
10404
10405static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10406{
10407 unsigned offset;
10408 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10409
10410 if( hs == NULL )
10411 return;
10412
Hanno Becker283f5ef2018-08-24 09:34:47 +010010413 ssl_free_buffered_record( ssl );
10414
Hanno Becker0271f962018-08-16 13:23:47 +010010415 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010416 ssl_buffering_free_slot( ssl, offset );
10417}
10418
10419static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10420 uint8_t slot )
10421{
10422 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10423 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010424
10425 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10426 return;
10427
Hanno Beckere605b192018-08-21 15:59:07 +010010428 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010429 {
Hanno Beckere605b192018-08-21 15:59:07 +010010430 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010431 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010432 mbedtls_free( hs_buf->data );
10433 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010434 }
10435}
10436
10437#endif /* MBEDTLS_SSL_PROTO_DTLS */
10438
Gilles Peskine9b562d52018-04-25 20:32:43 +020010439void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010440{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010441 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10442
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010443 if( handshake == NULL )
10444 return;
10445
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010446#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10447 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10448 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010449 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010450 handshake->async_in_progress = 0;
10451 }
10452#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10453
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010454#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10455 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10456 mbedtls_md5_free( &handshake->fin_md5 );
10457 mbedtls_sha1_free( &handshake->fin_sha1 );
10458#endif
10459#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10460#if defined(MBEDTLS_SHA256_C)
10461 mbedtls_sha256_free( &handshake->fin_sha256 );
10462#endif
10463#if defined(MBEDTLS_SHA512_C)
10464 mbedtls_sha512_free( &handshake->fin_sha512 );
10465#endif
10466#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468#if defined(MBEDTLS_DHM_C)
10469 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010470#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#if defined(MBEDTLS_ECDH_C)
10472 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010473#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010474#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010475 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010476#if defined(MBEDTLS_SSL_CLI_C)
10477 mbedtls_free( handshake->ecjpake_cache );
10478 handshake->ecjpake_cache = NULL;
10479 handshake->ecjpake_cache_len = 0;
10480#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010481#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010482
Janos Follath4ae5c292016-02-10 11:27:43 +000010483#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10484 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010485 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010487#endif
10488
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010489#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10490 if( handshake->psk != NULL )
10491 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010492 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010493 mbedtls_free( handshake->psk );
10494 }
10495#endif
10496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10498 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010499 /*
10500 * Free only the linked list wrapper, not the keys themselves
10501 * since the belong to the SNI callback
10502 */
10503 if( handshake->sni_key_cert != NULL )
10504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010505 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010506
10507 while( cur != NULL )
10508 {
10509 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010510 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010511 cur = next;
10512 }
10513 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010515
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010516#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010517 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010518 if( handshake->ecrs_peer_cert != NULL )
10519 {
10520 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10521 mbedtls_free( handshake->ecrs_peer_cert );
10522 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010523#endif
10524
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010525#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10526 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10527 mbedtls_pk_free( &handshake->peer_pubkey );
10528#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530#if defined(MBEDTLS_SSL_PROTO_DTLS)
10531 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010532 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010533 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010534#endif
10535
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010536 mbedtls_platform_zeroize( handshake,
10537 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010538}
10539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010541{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010542 if( session == NULL )
10543 return;
10544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010545#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010546 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010547#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010548
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010549#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010550 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010551#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010552
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010553 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010554}
10555
Paul Bakker5121ce52009-01-03 21:22:43 +000010556/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010557 * Serialize a full SSL context
10558 */
10559int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10560 unsigned char *buf,
10561 size_t buf_len,
10562 size_t *olen )
10563{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010564 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010565 (void) ssl;
10566
10567 if( buf != NULL )
10568 memset( buf, 0, buf_len );
10569
10570 *olen = 0;
10571
10572 return( 0 );
10573}
10574
10575/*
10576 * Deserialize a full SSL context
10577 */
10578int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10579 const unsigned char *buf,
10580 size_t len )
10581{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010582 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010583 (void) ssl;
10584 (void) buf;
10585 (void) len;
10586
10587 return( 0 );
10588}
10589
10590/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010591 * Free an SSL context
10592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010594{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010595 if( ssl == NULL )
10596 return;
10597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010599
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010600 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010601 {
Angus Grattond8213d02016-05-25 20:56:48 +100010602 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010604 }
10605
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010606 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010607 {
Angus Grattond8213d02016-05-25 20:56:48 +100010608 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010609 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010610 }
10611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010613 if( ssl->compress_buf != NULL )
10614 {
Angus Grattond8213d02016-05-25 20:56:48 +100010615 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010617 }
10618#endif
10619
Paul Bakker48916f92012-09-16 19:57:18 +000010620 if( ssl->transform )
10621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622 mbedtls_ssl_transform_free( ssl->transform );
10623 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010624 }
10625
10626 if( ssl->handshake )
10627 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010628 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10630 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632 mbedtls_free( ssl->handshake );
10633 mbedtls_free( ssl->transform_negotiate );
10634 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010635 }
10636
Paul Bakkerc0463502013-02-14 11:19:38 +010010637 if( ssl->session )
10638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 mbedtls_ssl_session_free( ssl->session );
10640 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010641 }
10642
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010643#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010644 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010645 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010646 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010648 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010649#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010651#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10652 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10655 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010656 }
10657#endif
10658
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010659#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010660 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010661#endif
10662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010664
Paul Bakker86f04f42013-02-14 11:20:09 +010010665 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010666 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010667}
10668
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010669/*
10670 * Initialze mbedtls_ssl_config
10671 */
10672void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10673{
10674 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010675
10676#if !defined(MBEDTLS_SSL_PROTO_TLS)
10677 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10678#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010679}
10680
Simon Butcherc97b6972015-12-27 23:48:17 +000010681#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010682static int ssl_preset_default_hashes[] = {
10683#if defined(MBEDTLS_SHA512_C)
10684 MBEDTLS_MD_SHA512,
10685 MBEDTLS_MD_SHA384,
10686#endif
10687#if defined(MBEDTLS_SHA256_C)
10688 MBEDTLS_MD_SHA256,
10689 MBEDTLS_MD_SHA224,
10690#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010691#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010692 MBEDTLS_MD_SHA1,
10693#endif
10694 MBEDTLS_MD_NONE
10695};
Simon Butcherc97b6972015-12-27 23:48:17 +000010696#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010697
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010698static int ssl_preset_suiteb_ciphersuites[] = {
10699 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10700 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10701 0
10702};
10703
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010704#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010705static int ssl_preset_suiteb_hashes[] = {
10706 MBEDTLS_MD_SHA256,
10707 MBEDTLS_MD_SHA384,
10708 MBEDTLS_MD_NONE
10709};
10710#endif
10711
10712#if defined(MBEDTLS_ECP_C)
10713static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10714 MBEDTLS_ECP_DP_SECP256R1,
10715 MBEDTLS_ECP_DP_SECP384R1,
10716 MBEDTLS_ECP_DP_NONE
10717};
10718#endif
10719
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010721 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010723int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010724 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010726#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010727 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010728#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010729
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010730 /* Use the functions here so that they are covered in tests,
10731 * but otherwise access member directly for efficiency */
10732 mbedtls_ssl_conf_endpoint( conf, endpoint );
10733 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010734
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010735 /*
10736 * Things that are common to all presets
10737 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010738#if defined(MBEDTLS_SSL_CLI_C)
10739 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10740 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010741#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010742 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010743#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010744#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10745 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10746#endif
10747 }
10748#endif
10749
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010750#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010751 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010752#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010753
10754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10755 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10756#endif
10757
10758#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010759#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010760 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010761#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10762#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010763 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010764 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010765#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010766#endif
10767
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010768#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10769 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10770#endif
10771
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010772#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010773 conf->f_cookie_write = ssl_cookie_write_dummy;
10774 conf->f_cookie_check = ssl_cookie_check_dummy;
10775#endif
10776
Hanno Becker7f376f42019-06-12 16:20:48 +010010777#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
10778 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010779 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10780#endif
10781
Janos Follath088ce432017-04-10 12:42:31 +010010782#if defined(MBEDTLS_SSL_SRV_C)
10783 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10784#endif
10785
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010786#if defined(MBEDTLS_SSL_PROTO_DTLS)
10787 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10788 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10789#endif
10790
10791#if defined(MBEDTLS_SSL_RENEGOTIATION)
10792 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010793 memset( conf->renego_period, 0x00, 2 );
10794 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010795#endif
10796
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010797#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10798 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10799 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010800 const unsigned char dhm_p[] =
10801 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10802 const unsigned char dhm_g[] =
10803 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10804
Hanno Beckera90658f2017-10-04 15:29:08 +010010805 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10806 dhm_p, sizeof( dhm_p ),
10807 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010808 {
10809 return( ret );
10810 }
10811 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010812#endif
10813
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010814 /*
10815 * Preset-specific defaults
10816 */
10817 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010818 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010819 /*
10820 * NSA Suite B
10821 */
10822 case MBEDTLS_SSL_PRESET_SUITEB:
10823 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10824 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10825 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10826 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10827
10828 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10829 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10830 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10831 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10832 ssl_preset_suiteb_ciphersuites;
10833
10834#if defined(MBEDTLS_X509_CRT_PARSE_C)
10835 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010836#endif
10837
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010838#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010839 conf->sig_hashes = ssl_preset_suiteb_hashes;
10840#endif
10841
10842#if defined(MBEDTLS_ECP_C)
10843 conf->curve_list = ssl_preset_suiteb_curves;
10844#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010845 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010846
10847 /*
10848 * Default
10849 */
10850 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010851 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10852 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10853 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10854 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10855 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10856 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10857 MBEDTLS_SSL_MIN_MINOR_VERSION :
10858 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010859 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10860 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10861
10862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010863 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010864 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10865#endif
10866
10867 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10868 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10869 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10870 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10871 mbedtls_ssl_list_ciphersuites();
10872
10873#if defined(MBEDTLS_X509_CRT_PARSE_C)
10874 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10875#endif
10876
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010877#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010878 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010879#endif
10880
10881#if defined(MBEDTLS_ECP_C)
10882 conf->curve_list = mbedtls_ecp_grp_id_list();
10883#endif
10884
10885#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10886 conf->dhm_min_bitlen = 1024;
10887#endif
10888 }
10889
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010890 return( 0 );
10891}
10892
10893/*
10894 * Free mbedtls_ssl_config
10895 */
10896void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10897{
10898#if defined(MBEDTLS_DHM_C)
10899 mbedtls_mpi_free( &conf->dhm_P );
10900 mbedtls_mpi_free( &conf->dhm_G );
10901#endif
10902
10903#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10904 if( conf->psk != NULL )
10905 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010906 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010907 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010908 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010909 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010910 }
10911
10912 if( conf->psk_identity != NULL )
10913 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010914 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010915 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010916 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010917 conf->psk_identity_len = 0;
10918 }
10919#endif
10920
10921#if defined(MBEDTLS_X509_CRT_PARSE_C)
10922 ssl_key_cert_free( conf->key_cert );
10923#endif
10924
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010925 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010926}
10927
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010928#if defined(MBEDTLS_PK_C) && \
10929 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010930/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010931 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010933unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010934{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935#if defined(MBEDTLS_RSA_C)
10936 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10937 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010939#if defined(MBEDTLS_ECDSA_C)
10940 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10941 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010942#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010944}
10945
Hanno Becker7e5437a2017-04-28 17:15:26 +010010946unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10947{
10948 switch( type ) {
10949 case MBEDTLS_PK_RSA:
10950 return( MBEDTLS_SSL_SIG_RSA );
10951 case MBEDTLS_PK_ECDSA:
10952 case MBEDTLS_PK_ECKEY:
10953 return( MBEDTLS_SSL_SIG_ECDSA );
10954 default:
10955 return( MBEDTLS_SSL_SIG_ANON );
10956 }
10957}
10958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010960{
10961 switch( sig )
10962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963#if defined(MBEDTLS_RSA_C)
10964 case MBEDTLS_SSL_SIG_RSA:
10965 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967#if defined(MBEDTLS_ECDSA_C)
10968 case MBEDTLS_SSL_SIG_ECDSA:
10969 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010970#endif
10971 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010972 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010973 }
10974}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010975#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010976
Hanno Becker7e5437a2017-04-28 17:15:26 +010010977#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10978 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10979
10980/* Find an entry in a signature-hash set matching a given hash algorithm. */
10981mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10982 mbedtls_pk_type_t sig_alg )
10983{
10984 switch( sig_alg )
10985 {
10986 case MBEDTLS_PK_RSA:
10987 return( set->rsa );
10988 case MBEDTLS_PK_ECDSA:
10989 return( set->ecdsa );
10990 default:
10991 return( MBEDTLS_MD_NONE );
10992 }
10993}
10994
10995/* Add a signature-hash-pair to a signature-hash set */
10996void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10997 mbedtls_pk_type_t sig_alg,
10998 mbedtls_md_type_t md_alg )
10999{
11000 switch( sig_alg )
11001 {
11002 case MBEDTLS_PK_RSA:
11003 if( set->rsa == MBEDTLS_MD_NONE )
11004 set->rsa = md_alg;
11005 break;
11006
11007 case MBEDTLS_PK_ECDSA:
11008 if( set->ecdsa == MBEDTLS_MD_NONE )
11009 set->ecdsa = md_alg;
11010 break;
11011
11012 default:
11013 break;
11014 }
11015}
11016
11017/* Allow exactly one hash algorithm for each signature. */
11018void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11019 mbedtls_md_type_t md_alg )
11020{
11021 set->rsa = md_alg;
11022 set->ecdsa = md_alg;
11023}
11024
11025#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11026 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11027
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011028/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011029 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011032{
11033 switch( hash )
11034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#if defined(MBEDTLS_MD5_C)
11036 case MBEDTLS_SSL_HASH_MD5:
11037 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039#if defined(MBEDTLS_SHA1_C)
11040 case MBEDTLS_SSL_HASH_SHA1:
11041 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043#if defined(MBEDTLS_SHA256_C)
11044 case MBEDTLS_SSL_HASH_SHA224:
11045 return( MBEDTLS_MD_SHA224 );
11046 case MBEDTLS_SSL_HASH_SHA256:
11047 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_SHA512_C)
11050 case MBEDTLS_SSL_HASH_SHA384:
11051 return( MBEDTLS_MD_SHA384 );
11052 case MBEDTLS_SSL_HASH_SHA512:
11053 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011054#endif
11055 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011057 }
11058}
11059
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011060/*
11061 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11062 */
11063unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11064{
11065 switch( md )
11066 {
11067#if defined(MBEDTLS_MD5_C)
11068 case MBEDTLS_MD_MD5:
11069 return( MBEDTLS_SSL_HASH_MD5 );
11070#endif
11071#if defined(MBEDTLS_SHA1_C)
11072 case MBEDTLS_MD_SHA1:
11073 return( MBEDTLS_SSL_HASH_SHA1 );
11074#endif
11075#if defined(MBEDTLS_SHA256_C)
11076 case MBEDTLS_MD_SHA224:
11077 return( MBEDTLS_SSL_HASH_SHA224 );
11078 case MBEDTLS_MD_SHA256:
11079 return( MBEDTLS_SSL_HASH_SHA256 );
11080#endif
11081#if defined(MBEDTLS_SHA512_C)
11082 case MBEDTLS_MD_SHA384:
11083 return( MBEDTLS_SSL_HASH_SHA384 );
11084 case MBEDTLS_MD_SHA512:
11085 return( MBEDTLS_SSL_HASH_SHA512 );
11086#endif
11087 default:
11088 return( MBEDTLS_SSL_HASH_NONE );
11089 }
11090}
11091
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011092#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011093/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011094 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011095 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011096 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011097int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011099 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011100
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011101 if( ssl->conf->curve_list == NULL )
11102 return( -1 );
11103
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011104 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011105 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011106 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011107
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011108 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011109}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011110#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011111
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011112#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011113/*
11114 * Check if a hash proposed by the peer is in our list.
11115 * Return 0 if we're willing to use it, -1 otherwise.
11116 */
11117int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11118 mbedtls_md_type_t md )
11119{
11120 const int *cur;
11121
11122 if( ssl->conf->sig_hashes == NULL )
11123 return( -1 );
11124
11125 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11126 if( *cur == (int) md )
11127 return( 0 );
11128
11129 return( -1 );
11130}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011131#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011133#if defined(MBEDTLS_X509_CRT_PARSE_C)
11134int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11135 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011136 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011137 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011138{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011139 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011141 int usage = 0;
11142#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011143#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011144 const char *ext_oid;
11145 size_t ext_len;
11146#endif
11147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11149 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011150 ((void) cert);
11151 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011152 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011153#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011155#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11156 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011157 {
11158 /* Server part of the key exchange */
11159 switch( ciphersuite->key_exchange )
11160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011161 case MBEDTLS_KEY_EXCHANGE_RSA:
11162 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011163 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011164 break;
11165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011166 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11167 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11168 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11169 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011170 break;
11171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11173 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011174 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011175 break;
11176
11177 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011178 case MBEDTLS_KEY_EXCHANGE_NONE:
11179 case MBEDTLS_KEY_EXCHANGE_PSK:
11180 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11181 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011182 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011183 usage = 0;
11184 }
11185 }
11186 else
11187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011188 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11189 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011190 }
11191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011192 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011193 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011194 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011195 ret = -1;
11196 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011197#else
11198 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11202 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011204 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11205 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011206 }
11207 else
11208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011209 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11210 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011211 }
11212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011213 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011214 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011215 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011216 ret = -1;
11217 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011218#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011219
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011220 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011223
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011224/*
11225 * Convert version numbers to/from wire format
11226 * and, for DTLS, to/from TLS equivalent.
11227 *
11228 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011229 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011230 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11231 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011233void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011234 unsigned char ver[2] )
11235{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011236#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11237 ((void) transport);
11238#endif
11239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011241 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011244 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11245
11246 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11247 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11248 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011249 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011250#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011251#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011252 {
11253 ver[0] = (unsigned char) major;
11254 ver[1] = (unsigned char) minor;
11255 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011256#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011257}
11258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011259void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011260 const unsigned char ver[2] )
11261{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011262#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11263 ((void) transport);
11264#endif
11265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011266#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011267 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011268 {
11269 *major = 255 - ver[0] + 2;
11270 *minor = 255 - ver[1] + 1;
11271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011273 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11274 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011275 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011277#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011278 {
11279 *major = ver[0];
11280 *minor = ver[1];
11281 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011282#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011283}
11284
Simon Butcher99000142016-10-13 17:21:01 +010011285int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11286{
11287#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11288 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11289 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11290
11291 switch( md )
11292 {
11293#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11294#if defined(MBEDTLS_MD5_C)
11295 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011296 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011297#endif
11298#if defined(MBEDTLS_SHA1_C)
11299 case MBEDTLS_SSL_HASH_SHA1:
11300 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11301 break;
11302#endif
11303#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11304#if defined(MBEDTLS_SHA512_C)
11305 case MBEDTLS_SSL_HASH_SHA384:
11306 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11307 break;
11308#endif
11309#if defined(MBEDTLS_SHA256_C)
11310 case MBEDTLS_SSL_HASH_SHA256:
11311 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11312 break;
11313#endif
11314 default:
11315 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11316 }
11317
11318 return 0;
11319#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11320 (void) ssl;
11321 (void) md;
11322
11323 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11325}
11326
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11328 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11329int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11330 unsigned char *output,
11331 unsigned char *data, size_t data_len )
11332{
11333 int ret = 0;
11334 mbedtls_md5_context mbedtls_md5;
11335 mbedtls_sha1_context mbedtls_sha1;
11336
11337 mbedtls_md5_init( &mbedtls_md5 );
11338 mbedtls_sha1_init( &mbedtls_sha1 );
11339
11340 /*
11341 * digitally-signed struct {
11342 * opaque md5_hash[16];
11343 * opaque sha_hash[20];
11344 * };
11345 *
11346 * md5_hash
11347 * MD5(ClientHello.random + ServerHello.random
11348 * + ServerParams);
11349 * sha_hash
11350 * SHA(ClientHello.random + ServerHello.random
11351 * + ServerParams);
11352 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011353 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 goto exit;
11357 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 ssl->handshake->randbytes, 64 ) ) != 0 )
11360 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011361 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011362 goto exit;
11363 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011364 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011365 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011367 goto exit;
11368 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011369 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011370 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011372 goto exit;
11373 }
11374
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011375 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011376 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011378 goto exit;
11379 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011380 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011381 ssl->handshake->randbytes, 64 ) ) != 0 )
11382 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011384 goto exit;
11385 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011386 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011387 data_len ) ) != 0 )
11388 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011390 goto exit;
11391 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011392 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011393 output + 16 ) ) != 0 )
11394 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011396 goto exit;
11397 }
11398
11399exit:
11400 mbedtls_md5_free( &mbedtls_md5 );
11401 mbedtls_sha1_free( &mbedtls_sha1 );
11402
11403 if( ret != 0 )
11404 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11405 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11406
11407 return( ret );
11408
11409}
11410#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11411 MBEDTLS_SSL_PROTO_TLS1_1 */
11412
11413#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11414 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11415int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011416 unsigned char *hash, size_t *hashlen,
11417 unsigned char *data, size_t data_len,
11418 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419{
11420 int ret = 0;
11421 mbedtls_md_context_t ctx;
11422 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011423 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011424
11425 mbedtls_md_init( &ctx );
11426
11427 /*
11428 * digitally-signed struct {
11429 * opaque client_random[32];
11430 * opaque server_random[32];
11431 * ServerDHParams params;
11432 * };
11433 */
11434 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11435 {
11436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11437 goto exit;
11438 }
11439 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11440 {
11441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11442 goto exit;
11443 }
11444 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11445 {
11446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11447 goto exit;
11448 }
11449 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11450 {
11451 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11452 goto exit;
11453 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011454 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011455 {
11456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11457 goto exit;
11458 }
11459
11460exit:
11461 mbedtls_md_free( &ctx );
11462
11463 if( ret != 0 )
11464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11465 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11466
11467 return( ret );
11468}
11469#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11470 MBEDTLS_SSL_PROTO_TLS1_2 */
11471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011472#endif /* MBEDTLS_SSL_TLS_C */