blob: 95cf554f08f42e917e98a9d2905e7a1ad28d2f67 [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
Hanno Becker1f835fa2019-06-13 10:14:59 +0100321 if( ssl->handshake->retransmit_timeout >=
322 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
323 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 return( -1 );
Hanno Becker1f835fa2019-06-13 10:14:59 +0100325 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200326
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200327 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
328 * in the following way: after the initial transmission and a first
329 * retransmission, back off to a temporary estimated MTU of 508 bytes.
330 * This value is guaranteed to be deliverable (if not guaranteed to be
331 * delivered) of any compliant IPv4 (and IPv6) network, and should work
332 * on most non-IP stacks too. */
Hanno Becker1f835fa2019-06-13 10:14:59 +0100333 if( ssl->handshake->retransmit_timeout !=
334 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400335 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200336 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
338 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200339
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340 new_timeout = 2 * ssl->handshake->retransmit_timeout;
341
342 /* Avoid arithmetic overflow and range overflow */
343 if( new_timeout < ssl->handshake->retransmit_timeout ||
Hanno Becker1f835fa2019-06-13 10:14:59 +0100344 new_timeout > mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200345 {
Hanno Becker1f835fa2019-06-13 10:14:59 +0100346 new_timeout = mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200347 }
348
349 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200351 ssl->handshake->retransmit_timeout ) );
352
353 return( 0 );
354}
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200357{
Hanno Becker1f835fa2019-06-13 10:14:59 +0100358 ssl->handshake->retransmit_timeout = mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200360 ssl->handshake->retransmit_timeout ) );
361}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200365/*
366 * Convert max_fragment_length codes to length.
367 * RFC 6066 says:
368 * enum{
369 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
370 * } MaxFragmentLength;
371 * and we add 0 -> extension unused
372 */
Angus Grattond8213d02016-05-25 20:56:48 +1000373static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200374{
Angus Grattond8213d02016-05-25 20:56:48 +1000375 switch( mfl )
376 {
377 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
378 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
379 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
380 return 512;
381 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
382 return 1024;
383 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
384 return 2048;
385 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
386 return 4096;
387 default:
388 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
389 }
390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200392
Hanno Becker58fccf22019-02-06 14:30:46 +0000393int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
394 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ssl_session_free( dst );
397 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000400
401#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402 if( src->peer_cert != NULL )
403 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200404 int ret;
405
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200406 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200407 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200408 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200413 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 dst->peer_cert = NULL;
417 return( ret );
418 }
419 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100420#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000421 if( src->peer_cert_digest != NULL )
422 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000423 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000424 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000425 if( dst->peer_cert_digest == NULL )
426 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
427
428 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
429 src->peer_cert_digest_len );
430 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000431 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000432 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100433#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200436
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200437#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200438 if( src->ticket != NULL )
439 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200440 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200441 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200442 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200443
444 memcpy( dst->ticket, src->ticket, src->ticket_len );
445 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200446#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200447
448 return( 0 );
449}
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
452int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200453 const unsigned char *key_enc, const unsigned char *key_dec,
454 size_t keylen,
455 const unsigned char *iv_enc, const unsigned char *iv_dec,
456 size_t ivlen,
457 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200458 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
460int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
461int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
462int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
463int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
464#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000465
Paul Bakker5121ce52009-01-03 21:22:43 +0000466/*
467 * Key material generation
468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200470static int ssl3_prf( const unsigned char *secret, size_t slen,
471 const char *label,
472 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 unsigned char *dstbuf, size_t dlen )
474{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100475 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000476 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_context md5;
478 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 unsigned char padding[16];
480 unsigned char sha1sum[20];
481 ((void)label);
482
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200483 mbedtls_md5_init( &md5 );
484 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200485
Paul Bakker5f70b252012-09-13 14:23:06 +0000486 /*
487 * SSLv3:
488 * block =
489 * MD5( secret + SHA1( 'A' + secret + random ) ) +
490 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
491 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
492 * ...
493 */
494 for( i = 0; i < dlen / 16; i++ )
495 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200496 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000497
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100498 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 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, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100501 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100505 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100506 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000508
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100509 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100510 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100511 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100512 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100513 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100514 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100515 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100516 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000517 }
518
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100519exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200520 mbedtls_md5_free( &md5 );
521 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000522
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500523 mbedtls_platform_zeroize( padding, sizeof( padding ) );
524 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000525
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100526 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000527}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200531static int tls1_prf( const unsigned char *secret, size_t slen,
532 const char *label,
533 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000534 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000535{
Paul Bakker23986e52011-04-24 08:57:21 +0000536 size_t nb, hs;
537 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200538 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 unsigned char tmp[128];
540 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 const mbedtls_md_info_t *md_info;
542 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100543 int ret;
544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 hs = ( slen + 1 ) / 2;
551 S1 = secret;
552 S2 = secret + slen - hs;
553
554 nb = strlen( label );
555 memcpy( tmp + 20, label, nb );
556 memcpy( tmp + 20 + nb, random, rlen );
557 nb += rlen;
558
559 /*
560 * First compute P_md5(secret,label+random)[0..dlen]
561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100566 return( ret );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
569 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
570 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 for( i = 0; i < dlen; i += 16 )
573 {
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 + nb );
576 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
580 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
583
584 for( j = 0; j < k; j++ )
585 dstbuf[i + j] = h_i[j];
586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100589
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 /*
591 * XOR out with P_sha1(secret,label+random)[0..dlen]
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
594 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100597 return( ret );
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
600 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
601 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603 for( i = 0; i < dlen; i += 20 )
604 {
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 + nb );
607 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md_hmac_reset ( &md_ctx );
610 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
611 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
614
615 for( j = 0; j < k; j++ )
616 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
617 }
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100620
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500621 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
622 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624 return( 0 );
625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
629static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100630 const unsigned char *secret, size_t slen,
631 const char *label,
632 const unsigned char *random, size_t rlen,
633 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634{
635 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100636 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000637 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
639 const mbedtls_md_info_t *md_info;
640 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100641 int ret;
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
646 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100649
650 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000652
653 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100654 memcpy( tmp + md_len, label, nb );
655 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000656 nb += rlen;
657
658 /*
659 * Compute P_<hash>(secret, label + random)[0..dlen]
660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100662 return( ret );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
665 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
666 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100667
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100668 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000669 {
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 + nb );
672 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_md_hmac_reset ( &md_ctx );
675 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
676 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000677
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100678 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000679
680 for( j = 0; j < k; j++ )
681 dstbuf[i + j] = h_i[j];
682 }
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100685
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500686 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
687 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000688
689 return( 0 );
690}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100693static int tls_prf_sha256( const unsigned char *secret, size_t slen,
694 const char *label,
695 const unsigned char *random, size_t rlen,
696 unsigned char *dstbuf, size_t dlen )
697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100699 label, random, rlen, dstbuf, dlen ) );
700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200704static int tls_prf_sha384( const unsigned char *secret, size_t slen,
705 const char *label,
706 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000707 unsigned char *dstbuf, size_t dlen )
708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100710 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712#endif /* MBEDTLS_SHA512_C */
713#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
718 defined(MBEDTLS_SSL_PROTO_TLS1_1)
719static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200720#endif
Paul Bakker380da532012-04-18 16:10:25 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200723static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725#endif
726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200728static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#endif
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
733#if defined(MBEDTLS_SHA256_C)
734static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200735static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200737#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_SHA512_C)
740static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200741static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100743#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000745
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200746/* Type for the TLS PRF */
747typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
748 const unsigned char *, size_t,
749 unsigned char *, size_t);
750
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200751/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200752 * Populate a transform structure with session keys and all the other
753 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200754 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200755 * Parameters:
756 * - [in/out]: transform: structure to populate
757 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200758 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200759 * - [in] ciphersuite
760 * - [in] master
761 * - [in] encrypt_then_mac
762 * - [in] trunc_hmac
763 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200764 * - [in] tls_prf: pointer to PRF to use for key derivation
765 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200766 * - [in] minor_ver: SSL/TLS minor version
767 * - [in] endpoint: client or server
768 * - [in] ssl: optionally used for:
769 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
770 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
771 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200772 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200773static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200774 int ciphersuite,
775 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100776#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200777#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
778 int encrypt_then_mac,
779#endif
780#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
781 int trunc_hmac,
782#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100783#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200784#if defined(MBEDTLS_ZLIB_SUPPORT)
785 int compression,
786#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200787 ssl_tls_prf_t tls_prf,
788 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200789 int minor_ver,
790 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200791 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000792{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200793 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794 unsigned char keyblk[256];
795 unsigned char *key1;
796 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100797 unsigned char *mac_enc;
798 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000799 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200800 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000801 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000802 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 const mbedtls_cipher_info_t *cipher_info;
804 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100805
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200806#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
807 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
808 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200809 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200810 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000811#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000812
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200813 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100814#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
815 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200816 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000817#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200818 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200820 /*
821 * Get various info structures
822 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200823 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200824 if( ciphersuite_info == NULL )
825 {
826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200827 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
829 }
830
Hanno Becker8759e162017-12-27 21:34:08 +0000831 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100832 if( cipher_info == NULL )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000835 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100837 }
838
Hanno Becker8759e162017-12-27 21:34:08 +0000839 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100840 if( md_info == NULL )
841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000843 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100845 }
846
Hanno Beckera5a2b082019-05-15 14:03:01 +0100847#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100848 /* Copy own and peer's CID if the use of the CID
849 * extension has been negotiated. */
850 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
851 {
852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100853
Hanno Becker4932f9f2019-05-03 15:23:51 +0100854 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100855 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100856 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100857 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100858
859 transform->out_cid_len = ssl->handshake->peer_cid_len;
860 memcpy( transform->out_cid, ssl->handshake->peer_cid,
861 ssl->handshake->peer_cid_len );
862 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
863 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100864 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100865#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100866
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200868 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000869 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200870 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100871 if( ret != 0 )
872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100874 return( ret );
875 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200878 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200879 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200880 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000882
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 /*
884 * Determine the appropriate key, IV and MAC length.
885 */
Paul Bakker68884e32013-01-07 18:20:04 +0100886
Hanno Beckere7f2df02017-12-27 08:17:40 +0000887 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200888
Hanno Beckerf1229442018-01-03 15:32:31 +0000889#if defined(MBEDTLS_GCM_C) || \
890 defined(MBEDTLS_CCM_C) || \
891 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200893 cipher_info->mode == MBEDTLS_MODE_CCM ||
894 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000895 {
Hanno Becker8759e162017-12-27 21:34:08 +0000896 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200897
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200898 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000899 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000900 transform->taglen =
901 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200902
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200903 /* All modes haves 96-bit IVs;
904 * GCM and CCM has 4 implicit and 8 explicit bytes
905 * ChachaPoly has all 12 bytes implicit
906 */
Paul Bakker68884e32013-01-07 18:20:04 +0100907 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200908 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
909 transform->fixed_ivlen = 12;
910 else
911 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200913 /* Minimum length of encrypted record */
914 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000915 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100916 }
917 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000918#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
919#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
920 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
921 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100922 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200923 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
925 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200928 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100929 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000932 mac_key_len = mbedtls_md_get_size( md_info );
933 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200936 /*
937 * If HMAC is to be truncated, we shall keep the leftmost bytes,
938 * (rfc 6066 page 13 or rfc 2104 section 4),
939 * so we only need to adjust the length here.
940 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200941 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000944
945#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
946 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000947 * HMAC implementation which also truncates the key
948 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000949 mac_key_len = transform->maclen;
950#endif
951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200953
954 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100955 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200957 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959 transform->minlen = transform->maclen;
960 else
Paul Bakker68884e32013-01-07 18:20:04 +0100961 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200962 /*
963 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100964 * 1. if EtM is in use: one block plus MAC
965 * otherwise: * first multiple of blocklen greater than maclen
966 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200969 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100970 {
971 transform->minlen = transform->maclen
972 + cipher_info->block_size;
973 }
974 else
975#endif
976 {
977 transform->minlen = transform->maclen
978 + cipher_info->block_size
979 - transform->maclen % cipher_info->block_size;
980 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200983 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
984 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200985 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100986 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200987#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200989 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
990 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200991 {
992 transform->minlen += transform->ivlen;
993 }
994 else
995#endif
996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200999 }
Paul Bakker68884e32013-01-07 18:20:04 +01001000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001001 }
Hanno Beckerf1229442018-01-03 15:32:31 +00001002 else
1003#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1004 {
1005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1007 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001008
Hanno Beckere7f2df02017-12-27 08:17:40 +00001009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1010 (unsigned) keylen,
1011 (unsigned) transform->minlen,
1012 (unsigned) transform->ivlen,
1013 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014
1015 /*
1016 * Finally setup the cipher contexts, IVs and MAC secrets.
1017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001019 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001021 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001022 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001023
Paul Bakker68884e32013-01-07 18:20:04 +01001024 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001025 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001027 /*
1028 * This is not used in TLS v1.1.
1029 */
Paul Bakker48916f92012-09-16 19:57:18 +00001030 iv_copy_len = ( transform->fixed_ivlen ) ?
1031 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001032 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1033 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001034 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 }
1036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#endif /* MBEDTLS_SSL_CLI_C */
1038#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001040 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001041 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001042 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
Hanno Becker81c7b182017-11-09 18:39:33 +00001044 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001045 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001047 /*
1048 * This is not used in TLS v1.1.
1049 */
Paul Bakker48916f92012-09-16 19:57:18 +00001050 iv_copy_len = ( transform->fixed_ivlen ) ?
1051 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001052 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1053 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001054 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001056 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Hanno Becker92231322018-01-03 15:32:51 +00001063#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001065 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001066 {
Hanno Becker92231322018-01-03 15:32:51 +00001067 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1070 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001071 }
1072
Hanno Becker81c7b182017-11-09 18:39:33 +00001073 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1074 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001075 }
1076 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1078#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1079 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001080 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001081 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001082 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1083 For AEAD-based ciphersuites, there is nothing to do here. */
1084 if( mac_key_len != 0 )
1085 {
1086 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1087 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1088 }
Paul Bakker68884e32013-01-07 18:20:04 +01001089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001090 else
1091#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001095 }
Hanno Becker92231322018-01-03 15:32:51 +00001096#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1099 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001100 {
1101 int ret = 0;
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001104
Hanno Beckere7f2df02017-12-27 08:17:40 +00001105 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001106 transform->iv_enc, transform->iv_dec,
1107 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001108 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001109 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1112 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001113 }
1114 }
Hanno Becker92231322018-01-03 15:32:51 +00001115#else
1116 ((void) mac_dec);
1117 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001119
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001120#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1121 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001122 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001123 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001124 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001125 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001126 iv_copy_len );
1127 }
1128#endif
1129
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001130 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001134 return( ret );
1135 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001136
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001137 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001138 cipher_info ) ) != 0 )
1139 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001141 return( ret );
1142 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001145 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001149 return( ret );
1150 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001153 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001157 return( ret );
1158 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_CIPHER_MODE_CBC)
1161 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1164 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001167 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001168 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1171 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001174 return( ret );
1175 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001179 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001181 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001183 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001186
Paul Bakker48916f92012-09-16 19:57:18 +00001187 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1188 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001189
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001190 if( deflateInit( &transform->ctx_deflate,
1191 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001192 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1195 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001196 }
1197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001199
Paul Bakker5121ce52009-01-03 21:22:43 +00001200 return( 0 );
1201}
1202
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001203/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001204 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001205 *
1206 * Inputs:
1207 * - SSL/TLS minor version
1208 * - hash associated with the ciphersuite (only used by TLS 1.2)
1209 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001210 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001211 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1212 */
1213static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1214 int minor_ver,
1215 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001216{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001217#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1218 (void) hash;
1219#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001220
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001222 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001223 {
1224 handshake->tls_prf = ssl3_prf;
1225 handshake->calc_verify = ssl_calc_verify_ssl;
1226 handshake->calc_finished = ssl_calc_finished_ssl;
1227 }
1228 else
1229#endif
1230#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001231 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001232 {
1233 handshake->tls_prf = tls1_prf;
1234 handshake->calc_verify = ssl_calc_verify_tls;
1235 handshake->calc_finished = ssl_calc_finished_tls;
1236 }
1237 else
1238#endif
1239#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1240#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001241 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1242 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001243 {
1244 handshake->tls_prf = tls_prf_sha384;
1245 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1246 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1247 }
1248 else
1249#endif
1250#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001251 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001252 {
1253 handshake->tls_prf = tls_prf_sha256;
1254 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1255 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1256 }
1257 else
1258#endif
1259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1260 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1262 }
1263
1264 return( 0 );
1265}
1266
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001269 *
1270 * Parameters:
1271 * [in/out] handshake
1272 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1273 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001274 * [out] master
1275 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001276 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001277static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001278 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001279 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001280{
1281 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001282
1283#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001284 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001285 (void) ssl;
1286#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001289 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1291 return( 0 );
1292 }
1293
1294 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1295 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001296
1297#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001298 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1299 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001300 {
1301 unsigned char session_hash[48];
1302 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001303
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001304 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001305
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001306 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1307 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001308
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001309 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001310 "extended master secret",
1311 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001312 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001313 }
1314 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001315#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001316 {
1317 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1318 "master secret",
1319 handshake->randbytes, 64,
1320 master, 48 );
1321 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001322 if( ret != 0 )
1323 {
1324 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1325 return( ret );
1326 }
1327
1328 mbedtls_platform_zeroize( handshake->premaster,
1329 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001330
1331 return( 0 );
1332}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001333
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001334int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1335{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001336 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001337 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1338 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001339
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1341
1342 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001343 ret = ssl_set_handshake_prfs( ssl->handshake,
1344 ssl->minor_ver,
1345 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001346 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001347 {
1348 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001349 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001350 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001351
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001352 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001353 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001354 ssl->session_negotiate->master,
1355 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001356 if( ret != 0 )
1357 {
1358 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1359 return( ret );
1360 }
1361
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001362 /* Swap the client and server random values:
1363 * - MS derivation wanted client+server (RFC 5246 8.1)
1364 * - key derivation wants server+client (RFC 5246 6.3) */
1365 {
1366 unsigned char tmp[64];
1367 memcpy( tmp, ssl->handshake->randbytes, 64 );
1368 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1369 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1370 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1371 }
1372
1373 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001374 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001375 ssl->session_negotiate->ciphersuite,
1376 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001377#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001378#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1379 ssl->session_negotiate->encrypt_then_mac,
1380#endif
1381#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1382 ssl->session_negotiate->trunc_hmac,
1383#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001384#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001385#if defined(MBEDTLS_ZLIB_SUPPORT)
1386 ssl->session_negotiate->compression,
1387#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001388 ssl->handshake->tls_prf,
1389 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001390 ssl->minor_ver,
1391 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001392 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001393 if( ret != 0 )
1394 {
1395 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1396 return( ret );
1397 }
1398
1399 /* We no longer need Server/ClientHello.random values */
1400 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1401 sizeof( ssl->handshake->randbytes ) );
1402
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001403 /* Allocate compression buffer */
1404#if defined(MBEDTLS_ZLIB_SUPPORT)
1405 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1406 ssl->compress_buf == NULL )
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1409 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1410 if( ssl->compress_buf == NULL )
1411 {
1412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001413 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001414 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1415 }
1416 }
1417#endif
1418
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1420
1421 return( 0 );
1422}
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001425void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1426 unsigned char hash[36],
1427 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001429 mbedtls_md5_context md5;
1430 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 unsigned char pad_1[48];
1432 unsigned char pad_2[48];
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001436 mbedtls_md5_init( &md5 );
1437 mbedtls_sha1_init( &sha1 );
1438
1439 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1440 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441
Paul Bakker380da532012-04-18 16:10:25 +00001442 memset( pad_1, 0x36, 48 );
1443 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001445 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1446 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1447 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_md5_starts_ret( &md5 );
1450 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1451 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1452 mbedtls_md5_update_ret( &md5, hash, 16 );
1453 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001454
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001455 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1456 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1457 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001458
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001459 mbedtls_sha1_starts_ret( &sha1 );
1460 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1461 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1462 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1463 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001465 *hlen = 36;
1466
1467 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001469
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_md5_free( &md5 );
1471 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001472
Paul Bakker380da532012-04-18 16:10:25 +00001473 return;
1474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001478void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1479 unsigned char hash[36],
1480 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001481{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001482 mbedtls_md5_context md5;
1483 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001487 mbedtls_md5_init( &md5 );
1488 mbedtls_sha1_init( &sha1 );
1489
1490 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1491 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001492
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001493 mbedtls_md5_finish_ret( &md5, hash );
1494 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001495
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001496 *hlen = 36;
1497
1498 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001501 mbedtls_md5_free( &md5 );
1502 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001503
Paul Bakker380da532012-04-18 16:10:25 +00001504 return;
1505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1509#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001510void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1511 unsigned char hash[32],
1512 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001513{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001514 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001515
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001516 mbedtls_sha256_init( &sha256 );
1517
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001519
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001520 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001521 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001522
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001523 *hlen = 32;
1524
1525 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001527
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001528 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001529
Paul Bakker380da532012-04-18 16:10:25 +00001530 return;
1531}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001535void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1536 unsigned char hash[48],
1537 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001538{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001539 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001540
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001541 mbedtls_sha512_init( &sha512 );
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001544
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001545 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001546 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001548 *hlen = 48;
1549
1550 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001553 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001554
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 return;
1556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#endif /* MBEDTLS_SHA512_C */
1558#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1561int 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 +02001562{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001563 unsigned char *p = ssl->handshake->premaster;
1564 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001565 const unsigned char *psk = ssl->conf->psk;
1566 size_t psk_len = ssl->conf->psk_len;
1567
1568 /* If the psk callback was called, use its result */
1569 if( ssl->handshake->psk != NULL )
1570 {
1571 psk = ssl->handshake->psk;
1572 psk_len = ssl->handshake->psk_len;
1573 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001574
1575 /*
1576 * PMS = struct {
1577 * opaque other_secret<0..2^16-1>;
1578 * opaque psk<0..2^16-1>;
1579 * };
1580 * with "other_secret" depending on the particular key exchange
1581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1583 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001584 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001585 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001588 *(p++) = (unsigned char)( psk_len >> 8 );
1589 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001590
1591 if( end < p || (size_t)( end - p ) < psk_len )
1592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1593
1594 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001595 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001596 }
1597 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1599#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1600 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001601 {
1602 /*
1603 * other_secret already set by the ClientKeyExchange message,
1604 * and is 48 bytes long
1605 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001606 if( end - p < 2 )
1607 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1608
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001609 *p++ = 0;
1610 *p++ = 48;
1611 p += 48;
1612 }
1613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1615#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1616 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001617 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001618 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001619 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001620
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001621 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001623 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001624 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001627 return( ret );
1628 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001629 *(p++) = (unsigned char)( len >> 8 );
1630 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001631 p += len;
1632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001634 }
1635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1637#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1638 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001640 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 size_t zlen;
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001644 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001645 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001648 return( ret );
1649 }
1650
1651 *(p++) = (unsigned char)( zlen >> 8 );
1652 *(p++) = (unsigned char)( zlen );
1653 p += zlen;
1654
Janos Follath3fbdada2018-08-15 10:26:53 +01001655 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1656 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001657 }
1658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001663 }
1664
1665 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001666 if( end - p < 2 )
1667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001668
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001669 *(p++) = (unsigned char)( psk_len >> 8 );
1670 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001671
1672 if( end < p || (size_t)( end - p ) < psk_len )
1673 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1674
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001675 memcpy( p, psk, psk_len );
1676 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001677
1678 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1679
1680 return( 0 );
1681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001685/*
1686 * SSLv3.0 MAC functions
1687 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001688#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001689static void ssl_mac( mbedtls_md_context_t *md_ctx,
1690 const unsigned char *secret,
1691 const unsigned char *buf, size_t len,
1692 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001693 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001694{
1695 unsigned char header[11];
1696 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001697 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1699 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001700
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001701 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001703 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001704 else
Paul Bakker68884e32013-01-07 18:20:04 +01001705 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
1707 memcpy( header, ctr, 8 );
1708 header[ 8] = (unsigned char) type;
1709 header[ 9] = (unsigned char)( len >> 8 );
1710 header[10] = (unsigned char)( len );
1711
Paul Bakker68884e32013-01-07 18:20:04 +01001712 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 mbedtls_md_starts( md_ctx );
1714 mbedtls_md_update( md_ctx, secret, md_size );
1715 mbedtls_md_update( md_ctx, padding, padlen );
1716 mbedtls_md_update( md_ctx, header, 11 );
1717 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001718 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Paul Bakker68884e32013-01-07 18:20:04 +01001720 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_md_starts( md_ctx );
1722 mbedtls_md_update( md_ctx, secret, md_size );
1723 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001724 mbedtls_md_update( md_ctx, out, md_size );
1725 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001728
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001729/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001730 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001731#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001732 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1733 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1734 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1735/* This function makes sure every byte in the memory region is accessed
1736 * (in ascending addresses order) */
1737static void ssl_read_memory( unsigned char *p, size_t len )
1738{
1739 unsigned char acc = 0;
1740 volatile unsigned char force;
1741
1742 for( ; len != 0; p++, len-- )
1743 acc ^= *p;
1744
1745 force = acc;
1746 (void) force;
1747}
1748#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1749
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001750/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001752 */
Hanno Becker3307b532017-12-27 21:37:21 +00001753
Hanno Beckera5a2b082019-05-15 14:03:01 +01001754#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001755/* This functions transforms a DTLS plaintext fragment and a record content
1756 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001757 *
1758 * struct {
1759 * opaque content[DTLSPlaintext.length];
1760 * ContentType real_type;
1761 * uint8 zeros[length_of_padding];
1762 * } DTLSInnerPlaintext;
1763 *
1764 * Input:
1765 * - `content`: The beginning of the buffer holding the
1766 * plaintext to be wrapped.
1767 * - `*content_size`: The length of the plaintext in Bytes.
1768 * - `max_len`: The number of Bytes available starting from
1769 * `content`. This must be `>= *content_size`.
1770 * - `rec_type`: The desired record content type.
1771 *
1772 * Output:
1773 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1774 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1775 *
1776 * Returns:
1777 * - `0` on success.
1778 * - A negative error code if `max_len` didn't offer enough space
1779 * for the expansion.
1780 */
1781static int ssl_cid_build_inner_plaintext( unsigned char *content,
1782 size_t *content_size,
1783 size_t remaining,
1784 uint8_t rec_type )
1785{
1786 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001787 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1788 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1789 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001790
1791 /* Write real content type */
1792 if( remaining == 0 )
1793 return( -1 );
1794 content[ len ] = rec_type;
1795 len++;
1796 remaining--;
1797
1798 if( remaining < pad )
1799 return( -1 );
1800 memset( content + len, 0, pad );
1801 len += pad;
1802 remaining -= pad;
1803
1804 *content_size = len;
1805 return( 0 );
1806}
1807
Hanno Becker7dc25772019-05-20 15:08:01 +01001808/* This function parses a DTLSInnerPlaintext structure.
1809 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001810static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1811 size_t *content_size,
1812 uint8_t *rec_type )
1813{
1814 size_t remaining = *content_size;
1815
1816 /* Determine length of padding by skipping zeroes from the back. */
1817 do
1818 {
1819 if( remaining == 0 )
1820 return( -1 );
1821 remaining--;
1822 } while( content[ remaining ] == 0 );
1823
1824 *content_size = remaining;
1825 *rec_type = content[ remaining ];
1826
1827 return( 0 );
1828}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001829#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001830
Hanno Becker99abf512019-05-20 14:50:53 +01001831/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001832 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001833static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001834 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001835 mbedtls_record *rec )
1836{
Hanno Becker99abf512019-05-20 14:50:53 +01001837 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001838 *
1839 * additional_data = seq_num + TLSCompressed.type +
1840 * TLSCompressed.version + TLSCompressed.length;
1841 *
Hanno Becker99abf512019-05-20 14:50:53 +01001842 * For the CID extension, this is extended as follows
1843 * (quoting draft-ietf-tls-dtls-connection-id-05,
1844 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001845 *
1846 * additional_data = seq_num + DTLSPlaintext.type +
1847 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001848 * cid +
1849 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001850 * length_of_DTLSInnerPlaintext;
1851 */
1852
Hanno Becker3307b532017-12-27 21:37:21 +00001853 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1854 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001855 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001856
Hanno Beckera5a2b082019-05-15 14:03:01 +01001857#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001858 if( rec->cid_len != 0 )
1859 {
1860 memcpy( add_data + 11, rec->cid, rec->cid_len );
1861 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1862 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1863 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1864 *add_data_len = 13 + 1 + rec->cid_len;
1865 }
1866 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001867#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001868 {
1869 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1870 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1871 *add_data_len = 13;
1872 }
Hanno Becker3307b532017-12-27 21:37:21 +00001873}
1874
Hanno Becker611a83b2018-01-03 14:27:32 +00001875int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1876 mbedtls_ssl_transform *transform,
1877 mbedtls_record *rec,
1878 int (*f_rng)(void *, unsigned char *, size_t),
1879 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001880{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001882 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001883 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001884 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001885 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001886 size_t post_avail;
1887
1888 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001889#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001890 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001891 ((void) ssl);
1892#endif
1893
1894 /* The PRNG is used for dynamic IV generation that's used
1895 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1896#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1897 ( defined(MBEDTLS_AES_C) || \
1898 defined(MBEDTLS_ARIA_C) || \
1899 defined(MBEDTLS_CAMELLIA_C) ) && \
1900 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1901 ((void) f_rng);
1902 ((void) p_rng);
1903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Hanno Becker3307b532017-12-27 21:37:21 +00001907 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001908 {
Hanno Becker3307b532017-12-27 21:37:21 +00001909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1911 }
Hanno Becker505089d2019-05-01 09:45:57 +01001912 if( rec == NULL
1913 || rec->buf == NULL
1914 || rec->buf_len < rec->data_offset
1915 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001916#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001917 || rec->cid_len != 0
1918#endif
1919 )
Hanno Becker3307b532017-12-27 21:37:21 +00001920 {
1921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001923 }
1924
Hanno Becker3307b532017-12-27 21:37:21 +00001925 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001926 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001928 data, rec->data_len );
1929
1930 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1931
1932 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1933 {
1934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1935 (unsigned) rec->data_len,
1936 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1938 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001939
Hanno Beckera5a2b082019-05-15 14:03:01 +01001940#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001941 /*
1942 * Add CID information
1943 */
1944 rec->cid_len = transform->out_cid_len;
1945 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1946 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001947
1948 if( rec->cid_len != 0 )
1949 {
1950 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001951 * Wrap plaintext into DTLSInnerPlaintext structure.
1952 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001953 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001954 * Note that this changes `rec->data_len`, and hence
1955 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001956 */
1957 if( ssl_cid_build_inner_plaintext( data,
1958 &rec->data_len,
1959 post_avail,
1960 rec->type ) != 0 )
1961 {
1962 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1963 }
1964
1965 rec->type = MBEDTLS_SSL_MSG_CID;
1966 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001968
Hanno Becker92c930f2019-04-29 17:31:37 +01001969 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1970
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001972 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001974#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975 if( mode == MBEDTLS_MODE_STREAM ||
1976 ( mode == MBEDTLS_MODE_CBC
1977#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001978 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001979#endif
1980 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 {
Hanno Becker3307b532017-12-27 21:37:21 +00001982 if( post_avail < transform->maclen )
1983 {
1984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1985 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1986 }
1987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001988#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001989 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001990 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001991 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001992 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1993 data, rec->data_len, rec->ctr, rec->type, mac );
1994 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995 }
1996 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001997#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1999 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00002000 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002001 {
Hanno Becker992b6872017-11-09 18:57:39 +00002002 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2003
Hanno Beckere83efe62019-04-29 13:52:53 +01002004 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002005
Hanno Becker3307b532017-12-27 21:37:21 +00002006 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002007 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002008 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2009 data, rec->data_len );
2010 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2011 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2012
2013 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002014 }
2015 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002016#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002020 }
2021
Hanno Becker3307b532017-12-27 21:37:21 +00002022 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2023 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002024
Hanno Becker3307b532017-12-27 21:37:21 +00002025 rec->data_len += transform->maclen;
2026 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002027 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002028 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002029#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002031 /*
2032 * Encrypt
2033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2035 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002037 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002038 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002040 "including %d bytes of padding",
2041 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042
Hanno Becker3307b532017-12-27 21:37:21 +00002043 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2044 transform->iv_enc, transform->ivlen,
2045 data, rec->data_len,
2046 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002049 return( ret );
2050 }
2051
Hanno Becker3307b532017-12-27 21:37:21 +00002052 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 }
Paul Bakker68884e32013-01-07 18:20:04 +01002058 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002060
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002061#if defined(MBEDTLS_GCM_C) || \
2062 defined(MBEDTLS_CCM_C) || \
2063 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 mode == MBEDTLS_MODE_CCM ||
2066 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002067 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002068 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002069 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002070 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002071
Hanno Becker3307b532017-12-27 21:37:21 +00002072 /* Check that there's space for both the authentication tag
2073 * and the explicit IV before and after the record content. */
2074 if( post_avail < transform->taglen ||
2075 rec->data_offset < explicit_iv_len )
2076 {
2077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2078 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2079 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002080
Paul Bakker68884e32013-01-07 18:20:04 +01002081 /*
2082 * Generate IV
2083 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002084 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2085 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002086 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002087 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002088 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2089 explicit_iv_len );
2090 /* Prefix record content with explicit IV. */
2091 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002092 }
2093 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2094 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002095 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002096 unsigned char i;
2097
2098 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2099
2100 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002101 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002102 }
2103 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002104 {
2105 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2107 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002108 }
2109
Hanno Beckere83efe62019-04-29 13:52:53 +01002110 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002111
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002112 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2113 iv, transform->ivlen );
2114 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002115 data - explicit_iv_len, explicit_iv_len );
2116 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002117 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002119 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002120 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002121
Paul Bakker68884e32013-01-07 18:20:04 +01002122 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002123 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002124 */
Hanno Becker3307b532017-12-27 21:37:21 +00002125
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002126 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002127 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002128 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002129 data, rec->data_len, /* source */
2130 data, &rec->data_len, /* destination */
2131 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002134 return( ret );
2135 }
2136
Hanno Becker3307b532017-12-27 21:37:21 +00002137 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2138 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002139
Hanno Becker3307b532017-12-27 21:37:21 +00002140 rec->data_len += transform->taglen + explicit_iv_len;
2141 rec->data_offset -= explicit_iv_len;
2142 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002143 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2147#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002148 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002151 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002152 size_t padlen, i;
2153 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002154
Hanno Becker3307b532017-12-27 21:37:21 +00002155 /* Currently we're always using minimal padding
2156 * (up to 255 bytes would be allowed). */
2157 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2158 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 padlen = 0;
2160
Hanno Becker3307b532017-12-27 21:37:21 +00002161 /* Check there's enough space in the buffer for the padding. */
2162 if( post_avail < padlen + 1 )
2163 {
2164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2165 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2166 }
2167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002169 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002170
Hanno Becker3307b532017-12-27 21:37:21 +00002171 rec->data_len += padlen + 1;
2172 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002175 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002176 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2177 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002178 */
Hanno Becker3307b532017-12-27 21:37:21 +00002179 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002180 {
Hanno Becker3307b532017-12-27 21:37:21 +00002181 if( f_rng == NULL )
2182 {
2183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2185 }
2186
2187 if( rec->data_offset < transform->ivlen )
2188 {
2189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2190 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2191 }
2192
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002193 /*
2194 * Generate IV
2195 */
Hanno Becker3307b532017-12-27 21:37:21 +00002196 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002197 if( ret != 0 )
2198 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002199
Hanno Becker3307b532017-12-27 21:37:21 +00002200 memcpy( data - transform->ivlen, transform->iv_enc,
2201 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002202
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002206 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002207 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002208 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002209 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210
Hanno Becker3307b532017-12-27 21:37:21 +00002211 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2212 transform->iv_enc,
2213 transform->ivlen,
2214 data, rec->data_len,
2215 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002218 return( ret );
2219 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002220
Hanno Becker3307b532017-12-27 21:37:21 +00002221 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002225 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002228 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002229 {
2230 /*
2231 * Save IV in SSL3 and TLS1
2232 */
Hanno Becker3307b532017-12-27 21:37:21 +00002233 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2234 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
Hanno Becker3307b532017-12-27 21:37:21 +00002236 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002237#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002238 {
2239 data -= transform->ivlen;
2240 rec->data_offset -= transform->ivlen;
2241 rec->data_len += transform->ivlen;
2242 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002245 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002246 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002247 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2248
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002249 /*
2250 * MAC(MAC_write_key, seq_num +
2251 * TLSCipherText.type +
2252 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002253 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254 * IV + // except for TLS 1.0
2255 * ENC(content + padding + padding_length));
2256 */
Hanno Becker3307b532017-12-27 21:37:21 +00002257
2258 if( post_avail < transform->maclen)
2259 {
2260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2261 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2262 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002263
Hanno Beckere83efe62019-04-29 13:52:53 +01002264 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002267 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002268 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002269
Hanno Becker3307b532017-12-27 21:37:21 +00002270 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002271 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002272 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2273 data, rec->data_len );
2274 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2275 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002276
Hanno Becker3307b532017-12-27 21:37:21 +00002277 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002278
Hanno Becker3307b532017-12-27 21:37:21 +00002279 rec->data_len += transform->maclen;
2280 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002281 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002287 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 /* Make extra sure authentication was performed, exactly once */
2294 if( auth_done != 1 )
2295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298 }
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
2302 return( 0 );
2303}
2304
Hanno Becker611a83b2018-01-03 14:27:32 +00002305int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2306 mbedtls_ssl_transform *transform,
2307 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002308{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002309 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002311 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002312#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002313 size_t padlen = 0, correct = 1;
2314#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002315 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002316 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002317 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002318
Hanno Becker611a83b2018-01-03 14:27:32 +00002319#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002320 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002321 ((void) ssl);
2322#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002325 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002326 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2329 }
2330 if( rec == NULL ||
2331 rec->buf == NULL ||
2332 rec->buf_len < rec->data_offset ||
2333 rec->buf_len - rec->data_offset < rec->data_len )
2334 {
2335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002337 }
2338
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 data = rec->buf + rec->data_offset;
2340 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
Hanno Beckera5a2b082019-05-15 14:03:01 +01002342#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002343 /*
2344 * Match record's CID with incoming CID.
2345 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002346 if( rec->cid_len != transform->in_cid_len ||
2347 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2348 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002349 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002350 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2354 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002355 {
2356 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002357 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2358 transform->iv_dec,
2359 transform->ivlen,
2360 data, rec->data_len,
2361 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002364 return( ret );
2365 }
2366
Hanno Becker4c6876b2017-12-27 21:28:58 +00002367 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002371 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 }
Paul Bakker68884e32013-01-07 18:20:04 +01002373 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002374#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002375#if defined(MBEDTLS_GCM_C) || \
2376 defined(MBEDTLS_CCM_C) || \
2377 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002379 mode == MBEDTLS_MODE_CCM ||
2380 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002381 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002382 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002383 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002384
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002385 /*
2386 * Compute and update sizes
2387 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002388 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 "+ taglen (%d)", rec->data_len,
2392 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002394 }
Paul Bakker68884e32013-01-07 18:20:04 +01002395
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002396 /*
2397 * Prepare IV
2398 */
2399 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2400 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002401 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002402 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002403 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002404
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002405 }
2406 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2407 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002408 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002409 unsigned char i;
2410
2411 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2412
2413 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002414 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002415 }
2416 else
2417 {
2418 /* Reminder if we ever add an AEAD mode with a different size */
2419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2421 }
2422
Hanno Becker4c6876b2017-12-27 21:28:58 +00002423 data += explicit_iv_len;
2424 rec->data_offset += explicit_iv_len;
2425 rec->data_len -= explicit_iv_len + transform->taglen;
2426
Hanno Beckere83efe62019-04-29 13:52:53 +01002427 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002428 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002429 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002430
2431 memcpy( transform->iv_dec + transform->fixed_ivlen,
2432 data - explicit_iv_len, explicit_iv_len );
2433
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002434 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002435 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002436 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002437
Paul Bakker68884e32013-01-07 18:20:04 +01002438
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002439 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002440 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002441 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002442 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2443 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002444 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002445 data, rec->data_len,
2446 data, &olen,
2447 data + rec->data_len,
2448 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2453 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002454
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002455 return( ret );
2456 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002457 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002458
Hanno Becker4c6876b2017-12-27 21:28:58 +00002459 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002463 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2467#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002468 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002471 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002472
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 /*
Paul Bakker45829992013-01-03 14:52:21 +01002474 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002477 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2478 {
2479 /* The ciphertext is prefixed with the CBC IV. */
2480 minlen += transform->ivlen;
2481 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002482#endif
Paul Bakker45829992013-01-03 14:52:21 +01002483
Hanno Becker4c6876b2017-12-27 21:28:58 +00002484 /* Size considerations:
2485 *
2486 * - The CBC cipher text must not be empty and hence
2487 * at least of size transform->ivlen.
2488 *
2489 * Together with the potential IV-prefix, this explains
2490 * the first of the two checks below.
2491 *
2492 * - The record must contain a MAC, either in plain or
2493 * encrypted, depending on whether Encrypt-then-MAC
2494 * is used or not.
2495 * - If it is, the message contains the IV-prefix,
2496 * the CBC ciphertext, and the MAC.
2497 * - If it is not, the padded plaintext, and hence
2498 * the CBC ciphertext, has at least length maclen + 1
2499 * because there is at least the padding length byte.
2500 *
2501 * As the CBC ciphertext is not empty, both cases give the
2502 * lower bound minlen + maclen + 1 on the record size, which
2503 * we test for in the second check below.
2504 */
2505 if( rec->data_len < minlen + transform->ivlen ||
2506 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002509 "+ 1 ) ( + expl IV )", rec->data_len,
2510 transform->ivlen,
2511 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002513 }
2514
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515 /*
2516 * Authenticate before decrypt if enabled
2517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002519 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 {
Hanno Becker992b6872017-11-09 18:57:39 +00002521 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002524
Hanno Becker4c6876b2017-12-27 21:28:58 +00002525 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2526 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527
Hanno Beckere83efe62019-04-29 13:52:53 +01002528 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002529
Hanno Beckere83efe62019-04-29 13:52:53 +01002530 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2531 add_data_len );
2532 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2533 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002534 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2535 data, rec->data_len );
2536 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2537 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002538
Hanno Becker4c6876b2017-12-27 21:28:58 +00002539 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2540 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002541 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002542 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002543
Hanno Becker4c6876b2017-12-27 21:28:58 +00002544 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2545 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002549 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002550 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002553
2554 /*
2555 * Check length sanity
2556 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002560 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002562 }
2563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002564#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002565 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002566 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002567 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002568 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002569 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002570 /* This is safe because data_len >= minlen + maclen + 1 initially,
2571 * and at this point we have at most subtracted maclen (note that
2572 * minlen == transform->ivlen here). */
2573 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002574
Hanno Becker4c6876b2017-12-27 21:28:58 +00002575 data += transform->ivlen;
2576 rec->data_offset += transform->ivlen;
2577 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002578 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002580
Hanno Becker4c6876b2017-12-27 21:28:58 +00002581 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2582 transform->iv_dec, transform->ivlen,
2583 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002586 return( ret );
2587 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002588
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002593 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002597 {
2598 /*
2599 * Save IV in SSL3 and TLS1
2600 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002601 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2602 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002604#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002605
Hanno Becker4c6876b2017-12-27 21:28:58 +00002606 /* Safe since data_len >= minlen + maclen + 1, so after having
2607 * subtracted at most minlen and maclen up to this point,
2608 * data_len > 0. */
2609 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002610
Hanno Becker4c6876b2017-12-27 21:28:58 +00002611 if( auth_done == 1 )
2612 {
2613 correct *= ( rec->data_len >= padlen + 1 );
2614 padlen *= ( rec->data_len >= padlen + 1 );
2615 }
2616 else
Paul Bakker45829992013-01-03 14:52:21 +01002617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002619 if( rec->data_len < transform->maclen + padlen + 1 )
2620 {
2621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2622 rec->data_len,
2623 transform->maclen,
2624 padlen + 1 ) );
2625 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002626#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002627
2628 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2629 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002631
Hanno Becker4c6876b2017-12-27 21:28:58 +00002632 padlen++;
2633
2634 /* Regardless of the validity of the padding,
2635 * we have data_len >= padlen here. */
2636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002638 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002639 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002640 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#if defined(MBEDTLS_SSL_DEBUG_ALL)
2643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 "should be no more than %d",
2645 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002646#endif
Paul Bakker45829992013-01-03 14:52:21 +01002647 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 }
2649 }
2650 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002651#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2652#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2653 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002654 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002656 /* The padding check involves a series of up to 256
2657 * consecutive memory reads at the end of the record
2658 * plaintext buffer. In order to hide the length and
2659 * validity of the padding, always perform exactly
2660 * `min(256,plaintext_len)` reads (but take into account
2661 * only the last `padlen` bytes for the padding check). */
2662 size_t pad_count = 0;
2663 size_t real_count = 0;
2664 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002665
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 /* Index of first padding byte; it has been ensured above
2667 * that the subtraction is safe. */
2668 size_t const padding_idx = rec->data_len - padlen;
2669 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2670 size_t const start_idx = rec->data_len - num_checks;
2671 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002672
Hanno Becker4c6876b2017-12-27 21:28:58 +00002673 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002674 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002675 real_count |= ( idx >= padding_idx );
2676 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002677 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002681 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002683#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002684 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002686 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2688 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002692 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002693
Hanno Becker4c6876b2017-12-27 21:28:58 +00002694 /* If the padding was found to be invalid, padlen == 0
2695 * and the subtraction is safe. If the padding was found valid,
2696 * padlen hasn't been changed and the previous assertion
2697 * data_len >= padlen still holds. */
2698 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002700 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002701#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002702 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002708#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002710 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002711#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
2713 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002714 * Authenticate if not done yet.
2715 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002717#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002718 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 {
Hanno Becker992b6872017-11-09 18:57:39 +00002720 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002721
Hanno Becker4c6876b2017-12-27 21:28:58 +00002722 /* If the initial value of padlen was such that
2723 * data_len < maclen + padlen + 1, then padlen
2724 * got reset to 1, and the initial check
2725 * data_len >= minlen + maclen + 1
2726 * guarantees that at this point we still
2727 * have at least data_len >= maclen.
2728 *
2729 * If the initial value of padlen was such that
2730 * data_len >= maclen + padlen + 1, then we have
2731 * subtracted either padlen + 1 (if the padding was correct)
2732 * or 0 (if the padding was incorrect) since then,
2733 * hence data_len >= maclen in any case.
2734 */
2735 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002736
Hanno Beckere83efe62019-04-29 13:52:53 +01002737 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002740 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002741 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002742 ssl_mac( &transform->md_ctx_dec,
2743 transform->mac_dec,
2744 data, rec->data_len,
2745 rec->ctr, rec->type,
2746 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002747 }
2748 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2750#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2751 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002752 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002753 {
2754 /*
2755 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002757 *
2758 * Known timing attacks:
2759 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2760 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 * To compensate for different timings for the MAC calculation
2762 * depending on how much padding was removed (which is determined
2763 * by padlen), process extra_run more blocks through the hash
2764 * function.
2765 *
2766 * The formula in the paper is
2767 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2768 * where L1 is the size of the header plus the decrypted message
2769 * plus CBC padding and L2 is the size of the header plus the
2770 * decrypted message. This is for an underlying hash function
2771 * with 64-byte blocks.
2772 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2773 * correctly. We round down instead of up, so -56 is the correct
2774 * value for our calculations instead of -55.
2775 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002776 * Repeat the formula rather than defining a block_size variable.
2777 * This avoids requiring division by a variable at runtime
2778 * (which would be marginally less efficient and would require
2779 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002780 */
2781 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002782 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002783
2784 /*
2785 * The next two sizes are the minimum and maximum values of
2786 * in_msglen over all padlen values.
2787 *
2788 * They're independent of padlen, since we previously did
2789 * in_msglen -= padlen.
2790 *
2791 * Note that max_len + maclen is never more than the buffer
2792 * length, as we previously did in_msglen -= maclen too.
2793 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002794 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002795 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2796
Hanno Becker4c6876b2017-12-27 21:28:58 +00002797 memset( tmp, 0, sizeof( tmp ) );
2798
2799 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002800 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002801#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2802 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002803 case MBEDTLS_MD_MD5:
2804 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002805 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002806 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002807 extra_run =
2808 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2809 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002810 break;
2811#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002812#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002813 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002814 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002815 extra_run =
2816 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2817 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002818 break;
2819#endif
2820 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002822 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2823 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002824
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002825 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002826
Hanno Beckere83efe62019-04-29 13:52:53 +01002827 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2828 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002829 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2830 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002831 /* Make sure we access everything even when padlen > 0. This
2832 * makes the synchronisation requirements for just-in-time
2833 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002834 ssl_read_memory( data + rec->data_len, padlen );
2835 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002836
2837 /* Call mbedtls_md_process at least once due to cache attacks
2838 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002839 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002840 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002841
Hanno Becker4c6876b2017-12-27 21:28:58 +00002842 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002843
2844 /* Make sure we access all the memory that could contain the MAC,
2845 * before we check it in the next code block. This makes the
2846 * synchronisation requirements for just-in-time Prime+Probe
2847 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002848 ssl_read_memory( data + min_len,
2849 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002850 }
2851 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2853 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002857 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002859#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002860 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2861 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002862#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Hanno Becker4c6876b2017-12-27 21:28:58 +00002864 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2865 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_DEBUG_ALL)
2868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002869#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002870 correct = 0;
2871 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002872 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002873 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002874
2875 /*
2876 * Finally check the correct flag
2877 */
2878 if( correct == 0 )
2879 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002880#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002881
2882 /* Make extra sure authentication was performed, exactly once */
2883 if( auth_done != 1 )
2884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2886 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002887 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002888
Hanno Beckera5a2b082019-05-15 14:03:01 +01002889#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002890 if( rec->cid_len != 0 )
2891 {
2892 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2893 &rec->type );
2894 if( ret != 0 )
2895 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2896 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002897#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
2901 return( 0 );
2902}
2903
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002904#undef MAC_NONE
2905#undef MAC_PLAINTEXT
2906#undef MAC_CIPHERTEXT
2907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909/*
2910 * Compression/decompression functions
2911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002913{
2914 int ret;
2915 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002916 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002918 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002922 if( len_pre == 0 )
2923 return( 0 );
2924
Paul Bakker2770fbd2012-07-03 13:30:23 +00002925 memcpy( msg_pre, ssl->out_msg, len_pre );
2926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 ssl->out_msglen ) );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->out_msg, ssl->out_msglen );
2932
Paul Bakker48916f92012-09-16 19:57:18 +00002933 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2934 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2935 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002936 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002937
Paul Bakker48916f92012-09-16 19:57:18 +00002938 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002939 if( ret != Z_OK )
2940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2942 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 }
2944
Angus Grattond8213d02016-05-25 20:56:48 +10002945 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002946 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002949 ssl->out_msglen ) );
2950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002952 ssl->out_msg, ssl->out_msglen );
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002955
2956 return( 0 );
2957}
2958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002960{
2961 int ret;
2962 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002963 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002964 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002965 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002968
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002969 if( len_pre == 0 )
2970 return( 0 );
2971
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972 memcpy( msg_pre, ssl->in_msg, len_pre );
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975 ssl->in_msglen ) );
2976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978 ssl->in_msg, ssl->in_msglen );
2979
Paul Bakker48916f92012-09-16 19:57:18 +00002980 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2981 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2982 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002983 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002984 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002985
Paul Bakker48916f92012-09-16 19:57:18 +00002986 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002987 if( ret != Z_OK )
2988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2990 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002991 }
2992
Angus Grattond8213d02016-05-25 20:56:48 +10002993 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002994 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002997 ssl->in_msglen ) );
2998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003000 ssl->in_msg, ssl->in_msglen );
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003003
3004 return( 0 );
3005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3009static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003011#if defined(MBEDTLS_SSL_PROTO_DTLS)
3012static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003013{
3014 /* If renegotiation is not enforced, retransmit until we would reach max
3015 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003016 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003017 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01003018 uint32_t ratio =
3019 mbedtls_ssl_conf_get_hs_timeout_max( ssl->conf ) /
3020 mbedtls_ssl_conf_get_hs_timeout_min( ssl->conf ) + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003021 unsigned char doublings = 1;
3022
3023 while( ratio != 0 )
3024 {
3025 ++doublings;
3026 ratio >>= 1;
3027 }
3028
3029 if( ++ssl->renego_records_seen > doublings )
3030 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003032 return( 0 );
3033 }
3034 }
3035
3036 return( ssl_write_hello_request( ssl ) );
3037}
3038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003039#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003040
Paul Bakker5121ce52009-01-03 21:22:43 +00003041/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003042 * Fill the input message buffer by appending data to it.
3043 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003044 *
3045 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3046 * available (from this read and/or a previous one). Otherwise, an error code
3047 * is returned (possibly EOF or WANT_READ).
3048 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003049 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3050 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3051 * since we always read a whole datagram at once.
3052 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003053 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003054 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003057{
Paul Bakker23986e52011-04-24 08:57:21 +00003058 int ret;
3059 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003063 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003066 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003068 }
3069
Angus Grattond8213d02016-05-25 20:56:48 +10003070 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3073 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003074 }
3075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003077 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003079 uint32_t timeout;
3080
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003081 /* Just to be sure */
3082 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3083 {
3084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3085 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3087 }
3088
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003089 /*
3090 * The point is, we need to always read a full datagram at once, so we
3091 * sometimes read more then requested, and handle the additional data.
3092 * It could be the rest of the current record (while fetching the
3093 * header) and/or some other records in the same datagram.
3094 */
3095
3096 /*
3097 * Move to the next record in the already read datagram if applicable
3098 */
3099 if( ssl->next_record_offset != 0 )
3100 {
3101 if( ssl->in_left < ssl->next_record_offset )
3102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003105 }
3106
3107 ssl->in_left -= ssl->next_record_offset;
3108
3109 if( ssl->in_left != 0 )
3110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003112 ssl->next_record_offset ) );
3113 memmove( ssl->in_hdr,
3114 ssl->in_hdr + ssl->next_record_offset,
3115 ssl->in_left );
3116 }
3117
3118 ssl->next_record_offset = 0;
3119 }
3120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003122 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003123
3124 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003125 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003126 */
3127 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003130 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003131 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003132
3133 /*
3134 * A record can't be split accross datagrams. If we need to read but
3135 * are not at the beginning of a new record, the caller did something
3136 * wrong.
3137 */
3138 if( ssl->in_left != 0 )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3141 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003142 }
3143
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003144 /*
3145 * Don't even try to read if time's out already.
3146 * This avoids by-passing the timer when repeatedly receiving messages
3147 * that will end up being dropped.
3148 */
3149 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003150 {
3151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003152 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003153 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003154 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003155 {
Angus Grattond8213d02016-05-25 20:56:48 +10003156 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003159 timeout = ssl->handshake->retransmit_timeout;
3160 else
Hanno Becker1f835fa2019-06-13 10:14:59 +01003161 timeout = mbedtls_ssl_conf_get_read_timeout( ssl->conf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003164
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003165 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003166 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3167 timeout );
3168 else
3169 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003171 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003172
3173 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003175 }
3176
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003177 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003180 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003183 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003184 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003187 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003188 }
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003193 return( ret );
3194 }
3195
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003196 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003197 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003199 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003201 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003202 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003205 return( ret );
3206 }
3207
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003208 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003209 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003211 }
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ret < 0 )
3214 return( ret );
3215
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003216 ssl->in_left = ret;
3217 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003218 MBEDTLS_SSL_TRANSPORT_ELSE
3219#endif /* MBEDTLS_SSL_PROTO_DTLS */
3220#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003223 ssl->in_left, nb_want ) );
3224
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003225 while( ssl->in_left < nb_want )
3226 {
3227 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003228
3229 if( ssl_check_timer( ssl ) != 0 )
3230 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3231 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003232 {
3233 if( ssl->f_recv_timeout != NULL )
3234 {
3235 ret = ssl->f_recv_timeout( ssl->p_bio,
Hanno Becker1f835fa2019-06-13 10:14:59 +01003236 ssl->in_hdr + ssl->in_left, len,
3237 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003238 }
3239 else
3240 {
3241 ret = ssl->f_recv( ssl->p_bio,
3242 ssl->in_hdr + ssl->in_left, len );
3243 }
3244 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003247 ssl->in_left, nb_want ) );
3248 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003249
3250 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003252
3253 if( ret < 0 )
3254 return( ret );
3255
mohammad160352aecb92018-03-28 23:41:40 -07003256 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003257 {
Darryl Green11999bb2018-03-13 15:22:58 +00003258 MBEDTLS_SSL_DEBUG_MSG( 1,
3259 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003260 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003261 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3262 }
3263
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003264 ssl->in_left += ret;
3265 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003267#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270
3271 return( 0 );
3272}
3273
3274/*
3275 * Flush any data not yet written
3276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003278{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003279 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003280 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003284 if( ssl->f_send == NULL )
3285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003287 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003289 }
3290
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003291 /* Avoid incrementing counter if data is flushed */
3292 if( ssl->out_left == 0 )
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003295 return( 0 );
3296 }
3297
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 while( ssl->out_left > 0 )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003301 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Hanno Becker2b1e3542018-08-06 11:19:13 +01003303 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003304 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 if( ret <= 0 )
3309 return( ret );
3310
mohammad160352aecb92018-03-28 23:41:40 -07003311 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003312 {
Darryl Green11999bb2018-03-13 15:22:58 +00003313 MBEDTLS_SSL_DEBUG_MSG( 1,
3314 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003315 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003316 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3317 }
3318
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 ssl->out_left -= ret;
3320 }
3321
Hanno Becker2b1e3542018-08-06 11:19:13 +01003322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003324 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003325 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003326 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003327 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003328#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003329#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003330 {
3331 ssl->out_hdr = ssl->out_buf + 8;
3332 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003333#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003334 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337
3338 return( 0 );
3339}
3340
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003341/*
3342 * Functions to handle the DTLS retransmission state machine
3343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003345/*
3346 * Append current handshake message to current outgoing flight
3347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3352 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3353 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003354
3355 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003356 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003357 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003360 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003361 }
3362
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003363 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368 }
3369
3370 /* Copy current handshake message with headers */
3371 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3372 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003373 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374 msg->next = NULL;
3375
3376 /* Append to the current flight */
3377 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003378 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003379 else
3380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003382 while( cur->next != NULL )
3383 cur = cur->next;
3384 cur->next = msg;
3385 }
3386
Hanno Becker3b235902018-08-06 09:54:53 +01003387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003388 return( 0 );
3389}
3390
3391/*
3392 * Free the current flight of handshake messages
3393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003395{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 mbedtls_ssl_flight_item *cur = flight;
3397 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003398
3399 while( cur != NULL )
3400 {
3401 next = cur->next;
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 mbedtls_free( cur->p );
3404 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003405
3406 cur = next;
3407 }
3408}
3409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3411static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003412#endif
3413
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003414/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003415 * Swap transform_out and out_ctr with the alternative ones
3416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003418{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003420 unsigned char tmp_out_ctr[8];
3421
3422 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003425 return;
3426 }
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003430 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003431 tmp_transform = ssl->transform_out;
3432 ssl->transform_out = ssl->handshake->alt_transform_out;
3433 ssl->handshake->alt_transform_out = tmp_transform;
3434
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003435 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003436 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3437 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003438 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003439
3440 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003441 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3444 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3449 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003450 }
3451 }
3452#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003453}
3454
3455/*
3456 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003457 */
3458int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3459{
3460 int ret = 0;
3461
3462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3463
3464 ret = mbedtls_ssl_flight_transmit( ssl );
3465
3466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3467
3468 return( ret );
3469}
3470
3471/*
3472 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003473 *
3474 * Need to remember the current message in case flush_output returns
3475 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003476 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003477 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003478int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003479{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003480 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003484 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003486
3487 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003488 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003489 ssl_swap_epochs( ssl );
3490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003492 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003493
3494 while( ssl->handshake->cur_msg != NULL )
3495 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003496 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003497 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498
Hanno Beckere1dcb032018-08-17 16:47:58 +01003499 int const is_finished =
3500 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3501 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3502
Hanno Becker04da1892018-08-14 13:22:10 +01003503 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3504 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3505
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003506 /* Swap epochs before sending Finished: we can't do it after
3507 * sending ChangeCipherSpec, in case write returns WANT_READ.
3508 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003509 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003510 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003511 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003512 ssl_swap_epochs( ssl );
3513 }
3514
Hanno Becker67bc7c32018-08-06 11:33:50 +01003515 ret = ssl_get_remaining_payload_in_datagram( ssl );
3516 if( ret < 0 )
3517 return( ret );
3518 max_frag_len = (size_t) ret;
3519
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003520 /* CCS is copied as is, while HS messages may need fragmentation */
3521 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3522 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003523 if( max_frag_len == 0 )
3524 {
3525 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3526 return( ret );
3527
3528 continue;
3529 }
3530
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003531 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003532 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003533 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003534
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003535 /* Update position inside current message */
3536 ssl->handshake->cur_msg_p += cur->len;
3537 }
3538 else
3539 {
3540 const unsigned char * const p = ssl->handshake->cur_msg_p;
3541 const size_t hs_len = cur->len - 12;
3542 const size_t frag_off = p - ( cur->p + 12 );
3543 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003544 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003545
Hanno Beckere1dcb032018-08-17 16:47:58 +01003546 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003547 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003548 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003549 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003550
Hanno Becker67bc7c32018-08-06 11:33:50 +01003551 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3552 return( ret );
3553
3554 continue;
3555 }
3556 max_hs_frag_len = max_frag_len - 12;
3557
3558 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3559 max_hs_frag_len : rem_len;
3560
3561 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003562 {
3563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003564 (unsigned) cur_hs_frag_len,
3565 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003566 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003567
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003568 /* Messages are stored with handshake headers as if not fragmented,
3569 * copy beginning of headers then fill fragmentation fields.
3570 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3571 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003572
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003573 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3574 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3575 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3576
Hanno Becker67bc7c32018-08-06 11:33:50 +01003577 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3578 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3579 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003580
3581 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3582
Hanno Becker3f7b9732018-08-28 09:53:25 +01003583 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003584 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3585 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003586 ssl->out_msgtype = cur->type;
3587
3588 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003589 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003590 }
3591
3592 /* If done with the current message move to the next one if any */
3593 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3594 {
3595 if( cur->next != NULL )
3596 {
3597 ssl->handshake->cur_msg = cur->next;
3598 ssl->handshake->cur_msg_p = cur->next->p + 12;
3599 }
3600 else
3601 {
3602 ssl->handshake->cur_msg = NULL;
3603 ssl->handshake->cur_msg_p = NULL;
3604 }
3605 }
3606
3607 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003608 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 return( ret );
3612 }
3613 }
3614
Hanno Becker67bc7c32018-08-06 11:33:50 +01003615 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3616 return( ret );
3617
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003618 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3620 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003621 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003624 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3625 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003626
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628
3629 return( 0 );
3630}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631
3632/*
3633 * To be called when the last message of an incoming flight is received.
3634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003636{
3637 /* We won't need to resend that one any more */
3638 ssl_flight_free( ssl->handshake->flight );
3639 ssl->handshake->flight = NULL;
3640 ssl->handshake->cur_msg = NULL;
3641
3642 /* The next incoming flight will start with this msg_seq */
3643 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3644
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003645 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003646 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003647
Hanno Becker0271f962018-08-16 13:23:47 +01003648 /* Clear future message buffering structure. */
3649 ssl_buffering_free( ssl );
3650
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003651 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003652 ssl_set_timer( ssl, 0 );
3653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3655 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003657 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003658 }
3659 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003662
3663/*
3664 * To be called when the last message of an outgoing flight is send.
3665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003667{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003668 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003669 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3672 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003675 }
3676 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003680
Paul Bakker5121ce52009-01-03 21:22:43 +00003681/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003682 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003684
3685/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003686 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003687 *
3688 * - fill in handshake headers
3689 * - update handshake checksum
3690 * - DTLS: save message for resending
3691 * - then pass to the record layer
3692 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003693 * DTLS: except for HelloRequest, messages are only queued, and will only be
3694 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003695 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003696 * Inputs:
3697 * - ssl->out_msglen: 4 + actual handshake message len
3698 * (4 is the size of handshake headers for TLS)
3699 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3700 * - ssl->out_msg + 4: the handshake message body
3701 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003702 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003703 * - ssl->out_msglen: the length of the record contents
3704 * (including handshake headers but excluding record headers)
3705 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003706 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003707int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003708{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003709 int ret;
3710 const size_t hs_len = ssl->out_msglen - 4;
3711 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3714
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003715 /*
3716 * Sanity checks
3717 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003718 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003719 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3720 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003721 /* In SSLv3, the client might send a NoCertificate alert. */
3722#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3723 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3724 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3725 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3726#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3727 {
3728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3730 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003733 /* Whenever we send anything different from a
3734 * HelloRequest we should be in a handshake - double check. */
3735 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3736 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003737 ssl->handshake == NULL )
3738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003744 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003745 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003746 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003747 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3749 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003750 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003751#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003752
Hanno Beckerb50a2532018-08-06 11:52:54 +01003753 /* Double-check that we did not exceed the bounds
3754 * of the outgoing record buffer.
3755 * This should never fail as the various message
3756 * writing functions must obey the bounds of the
3757 * outgoing record buffer, but better be safe.
3758 *
3759 * Note: We deliberately do not check for the MTU or MFL here.
3760 */
3761 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3762 {
3763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3764 "size %u, maximum %u",
3765 (unsigned) ssl->out_msglen,
3766 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3767 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3768 }
3769
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003770 /*
3771 * Fill handshake headers
3772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003773 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003774 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003775 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3776 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3777 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003779 /*
3780 * DTLS has additional fields in the Handshake layer,
3781 * between the length field and the actual payload:
3782 * uint16 message_seq;
3783 * uint24 fragment_offset;
3784 * uint24 fragment_length;
3785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003786#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003787 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003788 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003789 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003790 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003791 {
3792 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3793 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003794 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003795 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3797 }
3798
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003799 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003800 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003801
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003802 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003803 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003804 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003805 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3806 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3807 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003808 }
3809 else
3810 {
3811 ssl->out_msg[4] = 0;
3812 ssl->out_msg[5] = 0;
3813 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003814
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003815 /* Handshake hashes are computed without fragmentation,
3816 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003817 memset( ssl->out_msg + 6, 0x00, 3 );
3818 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003821
Hanno Becker0207e532018-08-28 10:28:28 +01003822 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003823 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3824 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 }
3826
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003827 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003829 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003830 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3831 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003832 {
3833 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003836 return( ret );
3837 }
3838 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003839 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003840#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003841 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003842 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003843 {
3844 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3845 return( ret );
3846 }
3847 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003848
3849 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3850
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003851 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003852}
3853
3854/*
3855 * Record layer functions
3856 */
3857
3858/*
3859 * Write current record.
3860 *
3861 * Uses:
3862 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3863 * - ssl->out_msglen: length of the record content (excl headers)
3864 * - ssl->out_msg: record content
3865 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003866int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003867{
3868 int ret, done = 0;
3869 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003870 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003871
3872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003875 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003877 {
3878 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003881 return( ret );
3882 }
3883
3884 len = ssl->out_msglen;
3885 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3889 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 ret = mbedtls_ssl_hw_record_write( ssl );
3894 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3897 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003898 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003899
3900 if( ret == 0 )
3901 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003902 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003904 if( !done )
3905 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003906 unsigned i;
3907 size_t protected_record_size;
3908
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003909 /* Skip writing the record content type to after the encryption,
3910 * as it may change when using the CID extension. */
3911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003913 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003914
Hanno Becker19859472018-08-06 09:40:20 +01003915 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003916 ssl->out_len[0] = (unsigned char)( len >> 8 );
3917 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003918
Paul Bakker48916f92012-09-16 19:57:18 +00003919 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003920 {
Hanno Becker3307b532017-12-27 21:37:21 +00003921 mbedtls_record rec;
3922
3923 rec.buf = ssl->out_iv;
3924 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3925 ( ssl->out_iv - ssl->out_buf );
3926 rec.data_len = ssl->out_msglen;
3927 rec.data_offset = ssl->out_msg - rec.buf;
3928
3929 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3930 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3931 ssl->conf->transport, rec.ver );
3932 rec.type = ssl->out_msgtype;
3933
Hanno Beckera5a2b082019-05-15 14:03:01 +01003934#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003935 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003936 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003937#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003938
Hanno Becker611a83b2018-01-03 14:27:32 +00003939 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003940 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003943 return( ret );
3944 }
3945
Hanno Becker3307b532017-12-27 21:37:21 +00003946 if( rec.data_offset != 0 )
3947 {
3948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3949 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3950 }
3951
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003952 /* Update the record content type and CID. */
3953 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003954#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003955 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003956#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003957 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003958 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3959 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003960 }
3961
Hanno Becker43395762019-05-03 14:46:38 +01003962 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003963
3964#if defined(MBEDTLS_SSL_PROTO_DTLS)
3965 /* In case of DTLS, double-check that we don't exceed
3966 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003967 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003968 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003969 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003970 if( ret < 0 )
3971 return( ret );
3972
3973 if( protected_record_size > (size_t) ret )
3974 {
3975 /* Should never happen */
3976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3977 }
3978 }
3979#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003980
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003981 /* Now write the potentially updated record content type. */
3982 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003985 "version = [%d:%d], msglen = %d",
3986 ssl->out_hdr[0], ssl->out_hdr[1],
3987 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003990 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003991
3992 ssl->out_left += protected_record_size;
3993 ssl->out_hdr += protected_record_size;
3994 ssl_update_out_pointers( ssl, ssl->transform_out );
3995
Hanno Becker04484622018-08-06 09:49:38 +01003996 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3997 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3998 break;
3999
4000 /* The loop goes to its end iff the counter is wrapping */
4001 if( i == ssl_ep_len( ssl ) )
4002 {
4003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4004 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4005 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 }
4007
Hanno Becker67bc7c32018-08-06 11:33:50 +01004008#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004009 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01004010 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004011 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004012 size_t remaining;
4013 ret = ssl_get_remaining_payload_in_datagram( ssl );
4014 if( ret < 0 )
4015 {
4016 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4017 ret );
4018 return( ret );
4019 }
4020
4021 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004022 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004023 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004024 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004025 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004026 else
4027 {
Hanno Becker513815a2018-08-20 11:56:09 +01004028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004029 }
4030 }
4031#endif /* MBEDTLS_SSL_PROTO_DTLS */
4032
4033 if( ( flush == SSL_FORCE_FLUSH ) &&
4034 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037 return( ret );
4038 }
4039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041
4042 return( 0 );
4043}
4044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004046
4047static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4048{
4049 if( ssl->in_msglen < ssl->in_hslen ||
4050 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4051 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4052 {
4053 return( 1 );
4054 }
4055 return( 0 );
4056}
Hanno Becker44650b72018-08-16 12:51:11 +01004057
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004058static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004059{
4060 return( ( ssl->in_msg[9] << 16 ) |
4061 ( ssl->in_msg[10] << 8 ) |
4062 ssl->in_msg[11] );
4063}
4064
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004065static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004066{
4067 return( ( ssl->in_msg[6] << 16 ) |
4068 ( ssl->in_msg[7] << 8 ) |
4069 ssl->in_msg[8] );
4070}
4071
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004072static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004073{
4074 uint32_t msg_len, frag_off, frag_len;
4075
4076 msg_len = ssl_get_hs_total_len( ssl );
4077 frag_off = ssl_get_hs_frag_off( ssl );
4078 frag_len = ssl_get_hs_frag_len( ssl );
4079
4080 if( frag_off > msg_len )
4081 return( -1 );
4082
4083 if( frag_len > msg_len - frag_off )
4084 return( -1 );
4085
4086 if( frag_len + 12 > ssl->in_msglen )
4087 return( -1 );
4088
4089 return( 0 );
4090}
4091
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004092/*
4093 * Mark bits in bitmask (used for DTLS HS reassembly)
4094 */
4095static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4096{
4097 unsigned int start_bits, end_bits;
4098
4099 start_bits = 8 - ( offset % 8 );
4100 if( start_bits != 8 )
4101 {
4102 size_t first_byte_idx = offset / 8;
4103
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004104 /* Special case */
4105 if( len <= start_bits )
4106 {
4107 for( ; len != 0; len-- )
4108 mask[first_byte_idx] |= 1 << ( start_bits - len );
4109
4110 /* Avoid potential issues with offset or len becoming invalid */
4111 return;
4112 }
4113
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004114 offset += start_bits; /* Now offset % 8 == 0 */
4115 len -= start_bits;
4116
4117 for( ; start_bits != 0; start_bits-- )
4118 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4119 }
4120
4121 end_bits = len % 8;
4122 if( end_bits != 0 )
4123 {
4124 size_t last_byte_idx = ( offset + len ) / 8;
4125
4126 len -= end_bits; /* Now len % 8 == 0 */
4127
4128 for( ; end_bits != 0; end_bits-- )
4129 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4130 }
4131
4132 memset( mask + offset / 8, 0xFF, len / 8 );
4133}
4134
4135/*
4136 * Check that bitmask is full
4137 */
4138static int ssl_bitmask_check( unsigned char *mask, size_t len )
4139{
4140 size_t i;
4141
4142 for( i = 0; i < len / 8; i++ )
4143 if( mask[i] != 0xFF )
4144 return( -1 );
4145
4146 for( i = 0; i < len % 8; i++ )
4147 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4148 return( -1 );
4149
4150 return( 0 );
4151}
4152
Hanno Becker56e205e2018-08-16 09:06:12 +01004153/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004154static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004155 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004156{
Hanno Becker56e205e2018-08-16 09:06:12 +01004157 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004158
Hanno Becker56e205e2018-08-16 09:06:12 +01004159 alloc_len = 12; /* Handshake header */
4160 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004161
Hanno Beckerd07df862018-08-16 09:14:58 +01004162 if( add_bitmap )
4163 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004164
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004165 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004166}
Hanno Becker56e205e2018-08-16 09:06:12 +01004167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004169
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004170static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004171{
4172 return( ( ssl->in_msg[1] << 16 ) |
4173 ( ssl->in_msg[2] << 8 ) |
4174 ssl->in_msg[3] );
4175}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004176
Simon Butcher99000142016-10-13 17:21:01 +01004177int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004182 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004184 }
4185
Hanno Becker12555c62018-08-16 12:47:53 +01004186 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004189 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004190 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004193 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004194 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004195 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004196 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004197
Hanno Becker44650b72018-08-16 12:51:11 +01004198 if( ssl_check_hs_header( ssl ) != 0 )
4199 {
4200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4201 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4202 }
4203
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004204 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004205 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4206 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4207 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4208 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004209 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004210 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4211 {
4212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4213 recv_msg_seq,
4214 ssl->handshake->in_msg_seq ) );
4215 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4216 }
4217
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004218 /* Retransmit only on last message from previous flight, to avoid
4219 * too many retransmissions.
4220 * Besides, No sane server ever retransmits HelloVerifyRequest */
4221 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004222 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004225 "message_seq = %d, start_of_flight = %d",
4226 recv_msg_seq,
4227 ssl->handshake->in_flight_start_seq ) );
4228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004232 return( ret );
4233 }
4234 }
4235 else
4236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004238 "message_seq = %d, expected = %d",
4239 recv_msg_seq,
4240 ssl->handshake->in_msg_seq ) );
4241 }
4242
Hanno Becker90333da2017-10-10 11:27:13 +01004243 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004244 }
4245 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004246
Hanno Becker6d97ef52018-08-16 13:09:04 +01004247 /* Message reassembly is handled alongside buffering of future
4248 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004249 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004250 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004251 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004254 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004255 }
4256 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004257 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004258#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004259#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004260 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004261 /* With TLS we don't handle fragmentation (for now) */
4262 if( ssl->in_msglen < ssl->in_hslen )
4263 {
4264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4265 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4266 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004267 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004268#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004269
Simon Butcher99000142016-10-13 17:21:01 +01004270 return( 0 );
4271}
4272
4273void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4274{
Hanno Becker0271f962018-08-16 13:23:47 +01004275 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004276
Hanno Becker0271f962018-08-16 13:23:47 +01004277 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004278 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004279 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004280 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004281
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004282 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004284 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004285 ssl->handshake != NULL )
4286 {
Hanno Becker0271f962018-08-16 13:23:47 +01004287 unsigned offset;
4288 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004289
Hanno Becker0271f962018-08-16 13:23:47 +01004290 /* Increment handshake sequence number */
4291 hs->in_msg_seq++;
4292
4293 /*
4294 * Clear up handshake buffering and reassembly structure.
4295 */
4296
4297 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004298 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004299
4300 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004301 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4302 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004303 offset++, hs_buf++ )
4304 {
4305 *hs_buf = *(hs_buf + 1);
4306 }
4307
4308 /* Create a fresh last entry */
4309 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004310 }
4311#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004312}
4313
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004314/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004315 * DTLS anti-replay: RFC 6347 4.1.2.6
4316 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004317 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4318 * Bit n is set iff record number in_window_top - n has been seen.
4319 *
4320 * Usually, in_window_top is the last record number seen and the lsb of
4321 * in_window is set. The only exception is the initial state (record number 0
4322 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004324#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4325static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326{
4327 ssl->in_window_top = 0;
4328 ssl->in_window = 0;
4329}
4330
4331static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4332{
4333 return( ( (uint64_t) buf[0] << 40 ) |
4334 ( (uint64_t) buf[1] << 32 ) |
4335 ( (uint64_t) buf[2] << 24 ) |
4336 ( (uint64_t) buf[3] << 16 ) |
4337 ( (uint64_t) buf[4] << 8 ) |
4338 ( (uint64_t) buf[5] ) );
4339}
4340
4341/*
4342 * Return 0 if sequence number is acceptable, -1 otherwise
4343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345{
4346 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4347 uint64_t bit;
4348
Hanno Becker7f376f42019-06-12 16:20:48 +01004349 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4350 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4351 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004352 return( 0 );
Hanno Becker7f376f42019-06-12 16:20:48 +01004353 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004354
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355 if( rec_seqnum > ssl->in_window_top )
4356 return( 0 );
4357
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004358 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004359
4360 if( bit >= 64 )
4361 return( -1 );
4362
4363 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4364 return( -1 );
4365
4366 return( 0 );
4367}
4368
4369/*
4370 * Update replay window on new validated record
4371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004373{
4374 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4375
Hanno Becker7f376f42019-06-12 16:20:48 +01004376 if( mbedtls_ssl_conf_get_anti_replay( ssl->conf ) ==
4377 MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
4378 {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004379 return;
Hanno Becker7f376f42019-06-12 16:20:48 +01004380 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004381
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004382 if( rec_seqnum > ssl->in_window_top )
4383 {
4384 /* Update window_top and the contents of the window */
4385 uint64_t shift = rec_seqnum - ssl->in_window_top;
4386
4387 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004388 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004389 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004390 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004391 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004392 ssl->in_window |= 1;
4393 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004394
4395 ssl->in_window_top = rec_seqnum;
4396 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004397 else
4398 {
4399 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004400 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004401
4402 if( bit < 64 ) /* Always true, but be extra sure */
4403 ssl->in_window |= (uint64_t) 1 << bit;
4404 }
4405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004407
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004408#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004409/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004410static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4411
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004412/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004413 * Without any SSL context, check if a datagram looks like a ClientHello with
4414 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004415 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004416 *
4417 * - if cookie is valid, return 0
4418 * - if ClientHello looks superficially valid but cookie is not,
4419 * fill obuf and set olen, then
4420 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4421 * - otherwise return a specific error code
4422 */
4423static int ssl_check_dtls_clihlo_cookie(
4424 mbedtls_ssl_cookie_write_t *f_cookie_write,
4425 mbedtls_ssl_cookie_check_t *f_cookie_check,
4426 void *p_cookie,
4427 const unsigned char *cli_id, size_t cli_id_len,
4428 const unsigned char *in, size_t in_len,
4429 unsigned char *obuf, size_t buf_len, size_t *olen )
4430{
4431 size_t sid_len, cookie_len;
4432 unsigned char *p;
4433
4434 if( f_cookie_write == NULL || f_cookie_check == NULL )
4435 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4436
4437 /*
4438 * Structure of ClientHello with record and handshake headers,
4439 * and expected values. We don't need to check a lot, more checks will be
4440 * done when actually parsing the ClientHello - skipping those checks
4441 * avoids code duplication and does not make cookie forging any easier.
4442 *
4443 * 0-0 ContentType type; copied, must be handshake
4444 * 1-2 ProtocolVersion version; copied
4445 * 3-4 uint16 epoch; copied, must be 0
4446 * 5-10 uint48 sequence_number; copied
4447 * 11-12 uint16 length; (ignored)
4448 *
4449 * 13-13 HandshakeType msg_type; (ignored)
4450 * 14-16 uint24 length; (ignored)
4451 * 17-18 uint16 message_seq; copied
4452 * 19-21 uint24 fragment_offset; copied, must be 0
4453 * 22-24 uint24 fragment_length; (ignored)
4454 *
4455 * 25-26 ProtocolVersion client_version; (ignored)
4456 * 27-58 Random random; (ignored)
4457 * 59-xx SessionID session_id; 1 byte len + sid_len content
4458 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4459 * ...
4460 *
4461 * Minimum length is 61 bytes.
4462 */
4463 if( in_len < 61 ||
4464 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4465 in[3] != 0 || in[4] != 0 ||
4466 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4467 {
4468 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4469 }
4470
4471 sid_len = in[59];
4472 if( sid_len > in_len - 61 )
4473 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4474
4475 cookie_len = in[60 + sid_len];
4476 if( cookie_len > in_len - 60 )
4477 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4478
4479 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4480 cli_id, cli_id_len ) == 0 )
4481 {
4482 /* Valid cookie */
4483 return( 0 );
4484 }
4485
4486 /*
4487 * If we get here, we've got an invalid cookie, let's prepare HVR.
4488 *
4489 * 0-0 ContentType type; copied
4490 * 1-2 ProtocolVersion version; copied
4491 * 3-4 uint16 epoch; copied
4492 * 5-10 uint48 sequence_number; copied
4493 * 11-12 uint16 length; olen - 13
4494 *
4495 * 13-13 HandshakeType msg_type; hello_verify_request
4496 * 14-16 uint24 length; olen - 25
4497 * 17-18 uint16 message_seq; copied
4498 * 19-21 uint24 fragment_offset; copied
4499 * 22-24 uint24 fragment_length; olen - 25
4500 *
4501 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4502 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4503 *
4504 * Minimum length is 28.
4505 */
4506 if( buf_len < 28 )
4507 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4508
4509 /* Copy most fields and adapt others */
4510 memcpy( obuf, in, 25 );
4511 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4512 obuf[25] = 0xfe;
4513 obuf[26] = 0xff;
4514
4515 /* Generate and write actual cookie */
4516 p = obuf + 28;
4517 if( f_cookie_write( p_cookie,
4518 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4519 {
4520 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4521 }
4522
4523 *olen = p - obuf;
4524
4525 /* Go back and fill length fields */
4526 obuf[27] = (unsigned char)( *olen - 28 );
4527
4528 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4529 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4530 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4531
4532 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4533 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4534
4535 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4536}
4537
4538/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539 * Handle possible client reconnect with the same UDP quadruplet
4540 * (RFC 6347 Section 4.2.8).
4541 *
4542 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4543 * that looks like a ClientHello.
4544 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545 * - if the input looks like a ClientHello without cookies,
4546 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004547 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004548 * - if the input looks like a ClientHello with a valid cookie,
4549 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004550 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004551 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004552 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004553 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004554 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4555 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004556 */
4557static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4558{
4559 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004560 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004562 ret = ssl_check_dtls_clihlo_cookie(
4563 ssl->conf->f_cookie_write,
4564 ssl->conf->f_cookie_check,
4565 ssl->conf->p_cookie,
4566 ssl->cli_id, ssl->cli_id_len,
4567 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004568 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004569
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004570 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4571
4572 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004573 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004574 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004575 * If the error is permanent we'll catch it later,
4576 * if it's not, then hopefully it'll work next time. */
4577 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4578
4579 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004580 }
4581
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004582 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004583 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004584 /* Got a valid cookie, partially reset context */
4585 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4586 {
4587 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4588 return( ret );
4589 }
4590
4591 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004592 }
4593
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004594 return( ret );
4595}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004596#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004597
Hanno Becker46483f12019-05-03 13:25:54 +01004598static int ssl_check_record_type( uint8_t record_type )
4599{
4600 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4601 record_type != MBEDTLS_SSL_MSG_ALERT &&
4602 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4603 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4604 {
4605 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4606 }
4607
4608 return( 0 );
4609}
4610
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004612 * ContentType type;
4613 * ProtocolVersion version;
4614 * uint16 epoch; // DTLS only
4615 * uint48 sequence_number; // DTLS only
4616 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004617 *
4618 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004619 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004620 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4621 *
4622 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004623 * 1. proceed with the record if this function returns 0
4624 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4625 * 3. return CLIENT_RECONNECT if this function return that value
4626 * 4. drop the whole datagram if this function returns anything else.
4627 * Point 2 is needed when the peer is resending, and we have already received
4628 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004631{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004632 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004633 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004634
Hanno Becker8b09b732019-05-08 12:03:28 +01004635 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004636
Paul Bakker5121ce52009-01-03 21:22:43 +00004637 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004638 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004640 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004642 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004643 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01004644 mbedtls_ssl_conf_get_cid_len( ssl->conf ) != 0 )
Hanno Becker8b09b732019-05-08 12:03:28 +01004645 {
4646 /* Shift pointers to account for record header including CID
4647 * struct {
4648 * ContentType special_type = tls12_cid;
4649 * ProtocolVersion version;
4650 * uint16 epoch;
4651 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004652 * opaque cid[cid_length]; // Additional field compared to
4653 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004654 * uint16 length;
4655 * opaque enc_content[DTLSCiphertext.length];
4656 * } DTLSCiphertext;
4657 */
4658
4659 /* So far, we only support static CID lengths
4660 * fixed in the configuration. */
Hanno Beckere0200da2019-06-13 09:23:43 +01004661 ssl->in_len = ssl->in_cid + mbedtls_ssl_conf_get_cid_len( ssl->conf );
Hanno Becker8b09b732019-05-08 12:03:28 +01004662 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4663 }
4664 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004665#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004666 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004669
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004670#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004671 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004672 * Section 4.1.2.7, that is, send alert only with TLS */
4673 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4674 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004675 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4676 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004677 }
4678#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004680 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004681 }
4682
4683 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004684 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4687 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 }
4689
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004690 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4693 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 }
4695
Hanno Becker8b09b732019-05-08 12:03:28 +01004696 /* Now that the total length of the record header is known, ensure
4697 * that the current datagram is large enough to hold it.
4698 * This would fail, for example, if we received a datagram of
4699 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4700 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4701 if( ret != 0 )
4702 {
4703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4704 return( ret );
4705 }
4706 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4707
4708 /* Parse and validate record length
4709 * This must happen after the CID parsing because
4710 * its position in the record header depends on
4711 * the presence of a CID. */
4712
4713 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004714 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004715 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4718 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004719 }
4720
Hanno Becker8b09b732019-05-08 12:03:28 +01004721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004722 "version = [%d:%d], msglen = %d",
4723 ssl->in_msgtype,
4724 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004725
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004726 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004727 * DTLS-related tests.
4728 * Check epoch before checking length constraint because
4729 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4730 * message gets duplicated before the corresponding Finished message,
4731 * the second ChangeCipherSpec should be discarded because it belongs
4732 * to an old epoch, but not because its length is shorter than
4733 * the minimum record length for packets using the new record transform.
4734 * Note that these two kinds of failures are handled differently,
4735 * as an unexpected record is silently skipped but an invalid
4736 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004737 */
4738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004739 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004740 {
4741 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4742
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004743 /* Check epoch (and sequence number) with DTLS */
4744 if( rec_epoch != ssl->in_epoch )
4745 {
4746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4747 "expected %d, received %d",
4748 ssl->in_epoch, rec_epoch ) );
4749
4750#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4751 /*
4752 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4753 * access the first byte of record content (handshake type), as we
4754 * have an active transform (possibly iv_len != 0), so use the
4755 * fact that the record header len is 13 instead.
4756 */
4757 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4758 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4759 rec_epoch == 0 &&
4760 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4761 ssl->in_left > 13 &&
4762 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4763 {
4764 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4765 "from the same port" ) );
4766 return( ssl_handle_possible_reconnect( ssl ) );
4767 }
4768 else
4769#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004770 {
4771 /* Consider buffering the record. */
4772 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4773 {
4774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4775 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4776 }
4777
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004778 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004779 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004780 }
4781
4782#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4783 /* Replay detection only works for the current epoch */
4784 if( rec_epoch == ssl->in_epoch &&
4785 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4786 {
4787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4788 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4789 }
4790#endif
4791 }
4792#endif /* MBEDTLS_SSL_PROTO_DTLS */
4793
Hanno Becker52c6dc62017-05-26 16:07:36 +01004794
4795 /* Check length against bounds of the current transform and version */
4796 if( ssl->transform_in == NULL )
4797 {
4798 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004799 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004800 {
4801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4802 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4803 }
4804 }
4805 else
4806 {
4807 if( ssl->in_msglen < ssl->transform_in->minlen )
4808 {
4809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4810 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4811 }
4812
4813#if defined(MBEDTLS_SSL_PROTO_SSL3)
4814 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004815 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004816 {
4817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4818 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4819 }
4820#endif
4821#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4822 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4823 /*
4824 * TLS encrypted messages can have up to 256 bytes of padding
4825 */
4826 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4827 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004828 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004829 {
4830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4831 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4832 }
4833#endif
4834 }
4835
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004836 return( 0 );
4837}
Paul Bakker5121ce52009-01-03 21:22:43 +00004838
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004839/*
4840 * If applicable, decrypt (and decompress) record content
4841 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004842static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004843{
4844 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004846 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004847 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4850 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 ret = mbedtls_ssl_hw_record_read( ssl );
4855 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4858 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004859 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004860
4861 if( ret == 0 )
4862 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004863 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004865 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004867 mbedtls_record rec;
4868
4869 rec.buf = ssl->in_iv;
4870 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4871 - ( ssl->in_iv - ssl->in_buf );
4872 rec.data_len = ssl->in_msglen;
4873 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004874#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004875 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004876 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004877#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004878
4879 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4880 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4881 ssl->conf->transport, rec.ver );
4882 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004883 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4884 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004887
Hanno Beckera5a2b082019-05-15 14:03:01 +01004888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004889 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Beckere0200da2019-06-13 09:23:43 +01004890 mbedtls_ssl_conf_get_ignore_unexpected_cid( ssl->conf )
4891 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004892 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004894 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004895 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004896#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004897
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 return( ret );
4899 }
4900
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004901 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004902 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004903 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4904 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004906
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004907 /* The record content type may change during decryption,
4908 * so re-read it. */
4909 ssl->in_msgtype = rec.type;
4910 /* Also update the input buffer, because unfortunately
4911 * the server-side ssl_parse_client_hello() reparses the
4912 * record header when receiving a ClientHello initiating
4913 * a renegotiation. */
4914 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004915 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004916 ssl->in_msglen = rec.data_len;
4917 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4918 ssl->in_len[1] = (unsigned char)( rec.data_len );
4919
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4921 ssl->in_msg, ssl->in_msglen );
4922
Hanno Beckera5a2b082019-05-15 14:03:01 +01004923#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004924 /* We have already checked the record content type
4925 * in ssl_parse_record_header(), failing or silently
4926 * dropping the record in the case of an unknown type.
4927 *
4928 * Since with the use of CIDs, the record content type
4929 * might change during decryption, re-check the record
4930 * content type, but treat a failure as fatal this time. */
4931 if( ssl_check_record_type( ssl->in_msgtype ) )
4932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4935 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004936#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004937
Angus Grattond8213d02016-05-25 20:56:48 +10004938 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4941 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004942 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004943 else if( ssl->in_msglen == 0 )
4944 {
4945#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4946 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4947 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4948 {
4949 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4951 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4952 }
4953#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4954
4955 ssl->nb_zero++;
4956
4957 /*
4958 * Three or more empty messages may be a DoS attack
4959 * (excessive CPU consumption).
4960 */
4961 if( ssl->nb_zero > 3 )
4962 {
4963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004964 "messages, possible DoS attack" ) );
4965 /* Treat the records as if they were not properly authenticated,
4966 * thereby failing the connection if we see more than allowed
4967 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004968 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4969 }
4970 }
4971 else
4972 ssl->nb_zero = 0;
4973
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004974 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4975#if defined(MBEDTLS_SSL_PROTO_TLS)
4976 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004977 {
4978 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004979 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004980 if( ++ssl->in_ctr[i - 1] != 0 )
4981 break;
4982
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004983 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004984 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004985 {
4986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4987 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4988 }
4989 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004990#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004991
Paul Bakker5121ce52009-01-03 21:22:43 +00004992 }
4993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004994#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004995 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004996 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004997 {
4998 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005001 return( ret );
5002 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005004#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005007 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005010 }
5011#endif
5012
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005013 return( 0 );
5014}
5015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005017
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005018/*
5019 * Read a record.
5020 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005021 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5022 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5023 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005024 */
Hanno Becker1097b342018-08-15 14:09:41 +01005025
5026/* Helper functions for mbedtls_ssl_read_record(). */
5027static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005028static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5029static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005030
Hanno Becker327c93b2018-08-15 13:56:18 +01005031int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005032 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005033{
5034 int ret;
5035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005038 if( ssl->keep_current_message == 0 )
5039 {
5040 do {
Simon Butcher99000142016-10-13 17:21:01 +01005041
Hanno Becker26994592018-08-15 14:14:59 +01005042 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005043 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005044 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005045
Hanno Beckere74d5562018-08-15 14:26:08 +01005046 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005047 {
Hanno Becker40f50842018-08-15 14:48:01 +01005048#if defined(MBEDTLS_SSL_PROTO_DTLS)
5049 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005050
Hanno Becker40f50842018-08-15 14:48:01 +01005051 /* We only check for buffered messages if the
5052 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005053 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005054 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005055 {
Hanno Becker40f50842018-08-15 14:48:01 +01005056 if( ssl_load_buffered_message( ssl ) == 0 )
5057 have_buffered = 1;
5058 }
5059
5060 if( have_buffered == 0 )
5061#endif /* MBEDTLS_SSL_PROTO_DTLS */
5062 {
5063 ret = ssl_get_next_record( ssl );
5064 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5065 continue;
5066
5067 if( ret != 0 )
5068 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005069 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005070 return( ret );
5071 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005072 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005073 }
5074
5075 ret = mbedtls_ssl_handle_message_type( ssl );
5076
Hanno Becker40f50842018-08-15 14:48:01 +01005077#if defined(MBEDTLS_SSL_PROTO_DTLS)
5078 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5079 {
5080 /* Buffer future message */
5081 ret = ssl_buffer_message( ssl );
5082 if( ret != 0 )
5083 return( ret );
5084
5085 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5086 }
5087#endif /* MBEDTLS_SSL_PROTO_DTLS */
5088
Hanno Becker90333da2017-10-10 11:27:13 +01005089 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5090 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005091
5092 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005093 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005094 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005095 return( ret );
5096 }
5097
Hanno Becker327c93b2018-08-15 13:56:18 +01005098 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005099 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005100 {
5101 mbedtls_ssl_update_handshake_status( ssl );
5102 }
Simon Butcher99000142016-10-13 17:21:01 +01005103 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005104 else
Simon Butcher99000142016-10-13 17:21:01 +01005105 {
Hanno Becker02f59072018-08-15 14:00:24 +01005106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005107 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005108 }
5109
5110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5111
5112 return( 0 );
5113}
5114
Hanno Becker40f50842018-08-15 14:48:01 +01005115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005116static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005117{
Hanno Becker40f50842018-08-15 14:48:01 +01005118 if( ssl->in_left > ssl->next_record_offset )
5119 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005120
Hanno Becker40f50842018-08-15 14:48:01 +01005121 return( 0 );
5122}
5123
5124static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5125{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005126 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005127 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005128 int ret = 0;
5129
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005130 if( hs == NULL )
5131 return( -1 );
5132
Hanno Beckere00ae372018-08-20 09:39:42 +01005133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5134
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005135 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5136 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5137 {
5138 /* Check if we have seen a ChangeCipherSpec before.
5139 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005140 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005141 {
5142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5143 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005144 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005145 }
5146
Hanno Becker39b8bc92018-08-28 17:17:13 +01005147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005148 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5149 ssl->in_msglen = 1;
5150 ssl->in_msg[0] = 1;
5151
5152 /* As long as they are equal, the exact value doesn't matter. */
5153 ssl->in_left = 0;
5154 ssl->next_record_offset = 0;
5155
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005156 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005157 goto exit;
5158 }
Hanno Becker37f95322018-08-16 13:55:32 +01005159
Hanno Beckerb8f50142018-08-28 10:01:34 +01005160#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005161 /* Debug only */
5162 {
5163 unsigned offset;
5164 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5165 {
5166 hs_buf = &hs->buffering.hs[offset];
5167 if( hs_buf->is_valid == 1 )
5168 {
5169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5170 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005171 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005172 }
5173 }
5174 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005175#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005176
5177 /* Check if we have buffered and/or fully reassembled the
5178 * next handshake message. */
5179 hs_buf = &hs->buffering.hs[0];
5180 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5181 {
5182 /* Synthesize a record containing the buffered HS message. */
5183 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5184 ( hs_buf->data[2] << 8 ) |
5185 hs_buf->data[3];
5186
5187 /* Double-check that we haven't accidentally buffered
5188 * a message that doesn't fit into the input buffer. */
5189 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5190 {
5191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5193 }
5194
5195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5196 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5197 hs_buf->data, msg_len + 12 );
5198
5199 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5200 ssl->in_hslen = msg_len + 12;
5201 ssl->in_msglen = msg_len + 12;
5202 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5203
5204 ret = 0;
5205 goto exit;
5206 }
5207 else
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5210 hs->in_msg_seq ) );
5211 }
5212
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005213 ret = -1;
5214
5215exit:
5216
5217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5218 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005219}
5220
Hanno Beckera02b0b42018-08-21 17:20:27 +01005221static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5222 size_t desired )
5223{
5224 int offset;
5225 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5227 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005228
Hanno Becker01315ea2018-08-21 17:22:17 +01005229 /* Get rid of future records epoch first, if such exist. */
5230 ssl_free_buffered_record( ssl );
5231
5232 /* Check if we have enough space available now. */
5233 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5234 hs->buffering.total_bytes_buffered ) )
5235 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005237 return( 0 );
5238 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005239
Hanno Becker4f432ad2018-08-28 10:02:32 +01005240 /* We don't have enough space to buffer the next expected handshake
5241 * message. Remove buffers used for future messages to gain space,
5242 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005243 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5244 offset >= 0; offset-- )
5245 {
5246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5247 offset ) );
5248
Hanno Beckerb309b922018-08-23 13:18:05 +01005249 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005250
5251 /* Check if we have enough space available now. */
5252 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5253 hs->buffering.total_bytes_buffered ) )
5254 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005256 return( 0 );
5257 }
5258 }
5259
5260 return( -1 );
5261}
5262
Hanno Becker40f50842018-08-15 14:48:01 +01005263static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5264{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005265 int ret = 0;
5266 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5267
5268 if( hs == NULL )
5269 return( 0 );
5270
5271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5272
5273 switch( ssl->in_msgtype )
5274 {
5275 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005277
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005278 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005279 break;
5280
5281 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005282 {
5283 unsigned recv_msg_seq_offset;
5284 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5285 mbedtls_ssl_hs_buffer *hs_buf;
5286 size_t msg_len = ssl->in_hslen - 12;
5287
5288 /* We should never receive an old handshake
5289 * message - double-check nonetheless. */
5290 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5291 {
5292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5294 }
5295
5296 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5297 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5298 {
5299 /* Silently ignore -- message too far in the future */
5300 MBEDTLS_SSL_DEBUG_MSG( 2,
5301 ( "Ignore future HS message with sequence number %u, "
5302 "buffering window %u - %u",
5303 recv_msg_seq, ssl->handshake->in_msg_seq,
5304 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5305
5306 goto exit;
5307 }
5308
5309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5310 recv_msg_seq, recv_msg_seq_offset ) );
5311
5312 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5313
5314 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005315 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005316 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005317 size_t reassembly_buf_sz;
5318
Hanno Becker37f95322018-08-16 13:55:32 +01005319 hs_buf->is_fragmented =
5320 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5321
5322 /* We copy the message back into the input buffer
5323 * after reassembly, so check that it's not too large.
5324 * This is an implementation-specific limitation
5325 * and not one from the standard, hence it is not
5326 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005327 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005328 {
5329 /* Ignore message */
5330 goto exit;
5331 }
5332
Hanno Beckere0b150f2018-08-21 15:51:03 +01005333 /* Check if we have enough space to buffer the message. */
5334 if( hs->buffering.total_bytes_buffered >
5335 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5336 {
5337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5339 }
5340
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005341 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5342 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005343
5344 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5345 hs->buffering.total_bytes_buffered ) )
5346 {
5347 if( recv_msg_seq_offset > 0 )
5348 {
5349 /* If we can't buffer a future message because
5350 * of space limitations -- ignore. */
5351 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",
5352 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5353 (unsigned) hs->buffering.total_bytes_buffered ) );
5354 goto exit;
5355 }
Hanno Beckere1801392018-08-21 16:51:05 +01005356 else
5357 {
5358 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",
5359 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5360 (unsigned) hs->buffering.total_bytes_buffered ) );
5361 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005362
Hanno Beckera02b0b42018-08-21 17:20:27 +01005363 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005364 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005365 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",
5366 (unsigned) msg_len,
5367 (unsigned) reassembly_buf_sz,
5368 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005369 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005370 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5371 goto exit;
5372 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005373 }
5374
5375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5376 msg_len ) );
5377
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005378 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5379 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005380 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005381 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005382 goto exit;
5383 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005384 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005385
5386 /* Prepare final header: copy msg_type, length and message_seq,
5387 * then add standardised fragment_offset and fragment_length */
5388 memcpy( hs_buf->data, ssl->in_msg, 6 );
5389 memset( hs_buf->data + 6, 0, 3 );
5390 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5391
5392 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005393
5394 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005395 }
5396 else
5397 {
5398 /* Make sure msg_type and length are consistent */
5399 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5402 /* Ignore */
5403 goto exit;
5404 }
5405 }
5406
Hanno Becker4422bbb2018-08-20 09:40:19 +01005407 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005408 {
5409 size_t frag_len, frag_off;
5410 unsigned char * const msg = hs_buf->data + 12;
5411
5412 /*
5413 * Check and copy current fragment
5414 */
5415
5416 /* Validation of header fields already done in
5417 * mbedtls_ssl_prepare_handshake_record(). */
5418 frag_off = ssl_get_hs_frag_off( ssl );
5419 frag_len = ssl_get_hs_frag_len( ssl );
5420
5421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5422 frag_off, frag_len ) );
5423 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5424
5425 if( hs_buf->is_fragmented )
5426 {
5427 unsigned char * const bitmask = msg + msg_len;
5428 ssl_bitmask_set( bitmask, frag_off, frag_len );
5429 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5430 msg_len ) == 0 );
5431 }
5432 else
5433 {
5434 hs_buf->is_complete = 1;
5435 }
5436
5437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5438 hs_buf->is_complete ? "" : "not yet " ) );
5439 }
5440
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005441 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005442 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005443
5444 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005445 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005446 break;
5447 }
5448
5449exit:
5450
5451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5452 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005453}
5454#endif /* MBEDTLS_SSL_PROTO_DTLS */
5455
Hanno Becker1097b342018-08-15 14:09:41 +01005456static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005457{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005458 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 * Consume last content-layer message and potentially
5460 * update in_msglen which keeps track of the contents'
5461 * consumption state.
5462 *
5463 * (1) Handshake messages:
5464 * Remove last handshake message, move content
5465 * and adapt in_msglen.
5466 *
5467 * (2) Alert messages:
5468 * Consume whole record content, in_msglen = 0.
5469 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005470 * (3) Change cipher spec:
5471 * Consume whole record content, in_msglen = 0.
5472 *
5473 * (4) Application data:
5474 * Don't do anything - the record layer provides
5475 * the application data as a stream transport
5476 * and consumes through mbedtls_ssl_read only.
5477 *
5478 */
5479
5480 /* Case (1): Handshake messages */
5481 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005482 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005483 /* Hard assertion to be sure that no application data
5484 * is in flight, as corrupting ssl->in_msglen during
5485 * ssl->in_offt != NULL is fatal. */
5486 if( ssl->in_offt != NULL )
5487 {
5488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5490 }
5491
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005492 /*
5493 * Get next Handshake message in the current record
5494 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005495
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005497 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005498 * current handshake content: If DTLS handshake
5499 * fragmentation is used, that's the fragment
5500 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005501 * size here is faulty and should be changed at
5502 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005503 * (2) While it doesn't seem to cause problems, one
5504 * has to be very careful not to assume that in_hslen
5505 * is always <= in_msglen in a sensible communication.
5506 * Again, it's wrong for DTLS handshake fragmentation.
5507 * The following check is therefore mandatory, and
5508 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005509 * Additionally, ssl->in_hslen might be arbitrarily out of
5510 * bounds after handling a DTLS message with an unexpected
5511 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005512 */
5513 if( ssl->in_hslen < ssl->in_msglen )
5514 {
5515 ssl->in_msglen -= ssl->in_hslen;
5516 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5517 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005518
Hanno Becker4a810fb2017-05-24 16:27:30 +01005519 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5520 ssl->in_msg, ssl->in_msglen );
5521 }
5522 else
5523 {
5524 ssl->in_msglen = 0;
5525 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005526
Hanno Becker4a810fb2017-05-24 16:27:30 +01005527 ssl->in_hslen = 0;
5528 }
5529 /* Case (4): Application data */
5530 else if( ssl->in_offt != NULL )
5531 {
5532 return( 0 );
5533 }
5534 /* Everything else (CCS & Alerts) */
5535 else
5536 {
5537 ssl->in_msglen = 0;
5538 }
5539
Hanno Becker1097b342018-08-15 14:09:41 +01005540 return( 0 );
5541}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005542
Hanno Beckere74d5562018-08-15 14:26:08 +01005543static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5544{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005545 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005546 return( 1 );
5547
5548 return( 0 );
5549}
5550
Hanno Becker5f066e72018-08-16 14:56:31 +01005551#if defined(MBEDTLS_SSL_PROTO_DTLS)
5552
5553static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5554{
5555 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5556 if( hs == NULL )
5557 return;
5558
Hanno Becker01315ea2018-08-21 17:22:17 +01005559 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005560 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005561 hs->buffering.total_bytes_buffered -=
5562 hs->buffering.future_record.len;
5563
5564 mbedtls_free( hs->buffering.future_record.data );
5565 hs->buffering.future_record.data = NULL;
5566 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005567}
5568
5569static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5570{
5571 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5572 unsigned char * rec;
5573 size_t rec_len;
5574 unsigned rec_epoch;
5575
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005576 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005577 return( 0 );
5578
5579 if( hs == NULL )
5580 return( 0 );
5581
Hanno Becker5f066e72018-08-16 14:56:31 +01005582 rec = hs->buffering.future_record.data;
5583 rec_len = hs->buffering.future_record.len;
5584 rec_epoch = hs->buffering.future_record.epoch;
5585
5586 if( rec == NULL )
5587 return( 0 );
5588
Hanno Becker4cb782d2018-08-20 11:19:05 +01005589 /* Only consider loading future records if the
5590 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005591 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005592 return( 0 );
5593
Hanno Becker5f066e72018-08-16 14:56:31 +01005594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5595
5596 if( rec_epoch != ssl->in_epoch )
5597 {
5598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5599 goto exit;
5600 }
5601
5602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5603
5604 /* Double-check that the record is not too large */
5605 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5606 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5607 {
5608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5609 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5610 }
5611
5612 memcpy( ssl->in_hdr, rec, rec_len );
5613 ssl->in_left = rec_len;
5614 ssl->next_record_offset = 0;
5615
5616 ssl_free_buffered_record( ssl );
5617
5618exit:
5619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5620 return( 0 );
5621}
5622
5623static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5624{
5625 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5626 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005627 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005628
5629 /* Don't buffer future records outside handshakes. */
5630 if( hs == NULL )
5631 return( 0 );
5632
5633 /* Only buffer handshake records (we are only interested
5634 * in Finished messages). */
5635 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5636 return( 0 );
5637
5638 /* Don't buffer more than one future epoch record. */
5639 if( hs->buffering.future_record.data != NULL )
5640 return( 0 );
5641
Hanno Becker01315ea2018-08-21 17:22:17 +01005642 /* Don't buffer record if there's not enough buffering space remaining. */
5643 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5644 hs->buffering.total_bytes_buffered ) )
5645 {
5646 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",
5647 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5648 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005649 return( 0 );
5650 }
5651
Hanno Becker5f066e72018-08-16 14:56:31 +01005652 /* Buffer record */
5653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5654 ssl->in_epoch + 1 ) );
5655 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5656 rec_hdr_len + ssl->in_msglen );
5657
5658 /* ssl_parse_record_header() only considers records
5659 * of the next epoch as candidates for buffering. */
5660 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005661 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005662
5663 hs->buffering.future_record.data =
5664 mbedtls_calloc( 1, hs->buffering.future_record.len );
5665 if( hs->buffering.future_record.data == NULL )
5666 {
5667 /* If we run out of RAM trying to buffer a
5668 * record from the next epoch, just ignore. */
5669 return( 0 );
5670 }
5671
Hanno Becker01315ea2018-08-21 17:22:17 +01005672 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005673
Hanno Becker01315ea2018-08-21 17:22:17 +01005674 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005675 return( 0 );
5676}
5677
5678#endif /* MBEDTLS_SSL_PROTO_DTLS */
5679
Hanno Beckere74d5562018-08-15 14:26:08 +01005680static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005681{
5682 int ret;
5683
Hanno Becker5f066e72018-08-16 14:56:31 +01005684#if defined(MBEDTLS_SSL_PROTO_DTLS)
5685 /* We might have buffered a future record; if so,
5686 * and if the epoch matches now, load it.
5687 * On success, this call will set ssl->in_left to
5688 * the length of the buffered record, so that
5689 * the calls to ssl_fetch_input() below will
5690 * essentially be no-ops. */
5691 ret = ssl_load_buffered_record( ssl );
5692 if( ret != 0 )
5693 return( ret );
5694#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005695
Hanno Becker8b09b732019-05-08 12:03:28 +01005696 /* Reset in pointers to default state for TLS/DTLS records,
5697 * assuming no CID and no offset between record content and
5698 * record plaintext. */
5699 ssl_update_in_pointers( ssl );
5700
5701 /* Ensure that we have enough space available for the default form
5702 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5703 * with no space for CIDs counted in). */
5704 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5705 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 return( ret );
5709 }
5710
5711 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005715 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005716 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005717 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5718 {
5719 ret = ssl_buffer_future_record( ssl );
5720 if( ret != 0 )
5721 return( ret );
5722
5723 /* Fall through to handling of unexpected records */
5724 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5725 }
5726
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005727 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5728 {
5729 /* Skip unexpected record (but not whole datagram) */
5730 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005731 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005732
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5734 "(header)" ) );
5735 }
5736 else
5737 {
5738 /* Skip invalid record and the rest of the datagram */
5739 ssl->next_record_offset = 0;
5740 ssl->in_left = 0;
5741
5742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5743 "(header)" ) );
5744 }
5745
5746 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005747 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005748 }
5749#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005750 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005751 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005752
5753 /*
5754 * Read and optionally decrypt the message contents
5755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005756 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005757 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005760 return( ret );
5761 }
5762
5763 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005765 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005766 {
Hanno Becker43395762019-05-03 14:46:38 +01005767 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005768 if( ssl->next_record_offset < ssl->in_left )
5769 {
5770 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5771 }
5772 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005773 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005774#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005775#if defined(MBEDTLS_SSL_PROTO_TLS)
5776 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005777 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005778 }
5779#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005780
5781 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005784 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005785 {
5786 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005787 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005788 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005789 /* Except when waiting for Finished as a bad mac here
5790 * probably means something went wrong in the handshake
5791 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5792 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5793 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5794 {
5795#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5796 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5797 {
5798 mbedtls_ssl_send_alert_message( ssl,
5799 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5800 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5801 }
5802#endif
5803 return( ret );
5804 }
5805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005806#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Hanno Beckerde671542019-06-12 16:30:46 +01005807 if( mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) != 0 &&
5808 ++ssl->badmac_seen >= mbedtls_ssl_conf_get_badmac_limit( ssl->conf ) )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005809 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005810 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5811 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005812 }
5813#endif
5814
Hanno Becker4a810fb2017-05-24 16:27:30 +01005815 /* As above, invalid records cause
5816 * dismissal of the whole datagram. */
5817
5818 ssl->next_record_offset = 0;
5819 ssl->in_left = 0;
5820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005822 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005823 }
5824
5825 return( ret );
5826 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005827 MBEDTLS_SSL_TRANSPORT_ELSE
5828#endif /* MBEDTLS_SSL_PROTO_DTLS */
5829#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005830 {
5831 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005832#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5833 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005835 mbedtls_ssl_send_alert_message( ssl,
5836 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5837 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005838 }
5839#endif
5840 return( ret );
5841 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005842#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005843 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005844
Simon Butcher99000142016-10-13 17:21:01 +01005845 return( 0 );
5846}
5847
5848int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5849{
5850 int ret;
5851
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005852 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005853 * Handle particular types of records
5854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005855 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005856 {
Simon Butcher99000142016-10-13 17:21:01 +01005857 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5858 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005859 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005860 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 }
5862
Hanno Beckere678eaa2018-08-21 14:57:46 +01005863 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005864 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005865 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005866 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5868 ssl->in_msglen ) );
5869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005870 }
5871
Hanno Beckere678eaa2018-08-21 14:57:46 +01005872 if( ssl->in_msg[0] != 1 )
5873 {
5874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5875 ssl->in_msg[0] ) );
5876 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5877 }
5878
5879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005880 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005881 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5882 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5883 {
5884 if( ssl->handshake == NULL )
5885 {
5886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5887 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5888 }
5889
5890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5891 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5892 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005893#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005894 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005896 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005898 if( ssl->in_msglen != 2 )
5899 {
5900 /* Note: Standard allows for more than one 2 byte alert
5901 to be packed in a single message, but Mbed TLS doesn't
5902 currently support this. */
5903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5904 ssl->in_msglen ) );
5905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5906 }
5907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005909 ssl->in_msg[0], ssl->in_msg[1] ) );
5910
5911 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005912 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005917 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005918 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005919 }
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5922 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5925 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005927
5928#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5929 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5930 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5931 {
Hanno Becker90333da2017-10-10 11:27:13 +01005932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005933 /* Will be handled when trying to parse ServerHello */
5934 return( 0 );
5935 }
5936#endif
5937
5938#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5939 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5940 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5941 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5942 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5943 {
5944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5945 /* Will be handled in mbedtls_ssl_parse_certificate() */
5946 return( 0 );
5947 }
5948#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5949
5950 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005951 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 }
5953
Hanno Beckerc76c6192017-06-06 10:03:17 +01005954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005955 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005956 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005957 /* Drop unexpected ApplicationData records,
5958 * except at the beginning of renegotiations */
5959 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5960 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5961#if defined(MBEDTLS_SSL_RENEGOTIATION)
5962 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5963 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005964#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005965 )
5966 {
5967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5968 return( MBEDTLS_ERR_SSL_NON_FATAL );
5969 }
5970
5971 if( ssl->handshake != NULL &&
5972 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5973 {
5974 ssl_handshake_wrapup_free_hs_transform( ssl );
5975 }
5976 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005977#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005978
Paul Bakker5121ce52009-01-03 21:22:43 +00005979 return( 0 );
5980}
5981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005983{
5984 int ret;
5985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5987 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5988 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005989 {
5990 return( ret );
5991 }
5992
5993 return( 0 );
5994}
5995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005997 unsigned char level,
5998 unsigned char message )
5999{
6000 int ret;
6001
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006002 if( ssl == NULL || ssl->conf == NULL )
6003 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006005 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006006 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006008 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006009 ssl->out_msglen = 2;
6010 ssl->out_msg[0] = level;
6011 ssl->out_msg[1] = message;
6012
Hanno Becker67bc7c32018-08-06 11:33:50 +01006013 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006016 return( ret );
6017 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006019
6020 return( 0 );
6021}
6022
Hanno Becker17572472019-02-08 07:19:04 +00006023#if defined(MBEDTLS_X509_CRT_PARSE_C)
6024static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6025{
6026#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6027 if( session->peer_cert != NULL )
6028 {
6029 mbedtls_x509_crt_free( session->peer_cert );
6030 mbedtls_free( session->peer_cert );
6031 session->peer_cert = NULL;
6032 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006033#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006034 if( session->peer_cert_digest != NULL )
6035 {
6036 /* Zeroization is not necessary. */
6037 mbedtls_free( session->peer_cert_digest );
6038 session->peer_cert_digest = NULL;
6039 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6040 session->peer_cert_digest_len = 0;
6041 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006042#else
6043 ((void) session);
6044#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006045}
6046#endif /* MBEDTLS_X509_CRT_PARSE_C */
6047
Paul Bakker5121ce52009-01-03 21:22:43 +00006048/*
6049 * Handshake functions
6050 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006051#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006052/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006054{
Hanno Becker8759e162017-12-27 21:34:08 +00006055 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006058
Hanno Becker5097cba2019-02-05 13:36:46 +00006059 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006062 ssl->state++;
6063 return( 0 );
6064 }
6065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6067 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006068}
6069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006071{
Hanno Becker8759e162017-12-27 21:34:08 +00006072 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006075
Hanno Becker5097cba2019-02-05 13:36:46 +00006076 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006079 ssl->state++;
6080 return( 0 );
6081 }
6082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6084 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006085}
Gilles Peskinef9828522017-05-03 12:28:43 +02006086
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006087#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006088/* Some certificate support -> implement write and parse */
6089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006092 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006093 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006095 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006098
Hanno Becker5097cba2019-02-05 13:36:46 +00006099 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006102 ssl->state++;
6103 return( 0 );
6104 }
6105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006107 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 {
6109 if( ssl->client_auth == 0 )
6110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 ssl->state++;
6113 return( 0 );
6114 }
6115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006117 /*
6118 * If using SSLv3 and got no cert, send an Alert message
6119 * (otherwise an empty Certificate message will be sent).
6120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6122 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 {
6124 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6126 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6127 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 goto write_msg;
6131 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134#endif /* MBEDTLS_SSL_CLI_C */
6135#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006136 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6141 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006142 }
6143 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006144#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
6148 /*
6149 * 0 . 0 handshake type
6150 * 1 . 3 handshake length
6151 * 4 . 6 length of all certs
6152 * 7 . 9 length of cert. 1
6153 * 10 . n-1 peer certificate
6154 * n . n+2 length of cert. 2
6155 * n+3 . ... upper level cert, etc.
6156 */
6157 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006158 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
Paul Bakker29087132010-03-21 21:03:34 +00006160 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006161 {
6162 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006163 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006166 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006167 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 }
6169
6170 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6171 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6172 ssl->out_msg[i + 2] = (unsigned char)( n );
6173
6174 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6175 i += n; crt = crt->next;
6176 }
6177
6178 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6179 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6180 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6181
6182 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6184 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006185
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006186#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006187write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006189
6190 ssl->state++;
6191
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006192 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006193 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 return( ret );
6196 }
6197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006199
Paul Bakkered27a042013-04-18 22:46:23 +02006200 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006201}
6202
Hanno Becker285ff0c2019-01-31 07:44:03 +00006203#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006204
6205#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006206static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6207 unsigned char *crt_buf,
6208 size_t crt_buf_len )
6209{
6210 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6211
6212 if( peer_crt == NULL )
6213 return( -1 );
6214
6215 if( peer_crt->raw.len != crt_buf_len )
6216 return( -1 );
6217
Hanno Becker68b856d2019-02-08 14:00:04 +00006218 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006219}
Hanno Becker5882dd02019-06-06 16:25:57 +01006220#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006221static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6222 unsigned char *crt_buf,
6223 size_t crt_buf_len )
6224{
6225 int ret;
6226 unsigned char const * const peer_cert_digest =
6227 ssl->session->peer_cert_digest;
6228 mbedtls_md_type_t const peer_cert_digest_type =
6229 ssl->session->peer_cert_digest_type;
6230 mbedtls_md_info_t const * const digest_info =
6231 mbedtls_md_info_from_type( peer_cert_digest_type );
6232 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6233 size_t digest_len;
6234
6235 if( peer_cert_digest == NULL || digest_info == NULL )
6236 return( -1 );
6237
6238 digest_len = mbedtls_md_get_size( digest_info );
6239 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6240 return( -1 );
6241
6242 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6243 if( ret != 0 )
6244 return( -1 );
6245
6246 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6247}
Hanno Becker5882dd02019-06-06 16:25:57 +01006248#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006249#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006250
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006251/*
6252 * Once the certificate message is read, parse it into a cert chain and
6253 * perform basic checks, but leave actual verification to the caller
6254 */
Hanno Becker35e41772019-02-05 15:37:23 +00006255static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6256 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006257{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006258 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006259#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6260 int crt_cnt=0;
6261#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006262 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006263 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006268 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6269 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006271 }
6272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6274 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006277 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6278 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006280 }
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006283
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006287 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006288
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006289 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006293 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6294 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296 }
6297
Hanno Becker33c3dc82019-01-30 14:46:46 +00006298 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6299 i += 3;
6300
Hanno Becker33c3dc82019-01-30 14:46:46 +00006301 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 while( i < ssl->in_hslen )
6303 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006304 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006305 if ( i + 3 > ssl->in_hslen ) {
6306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006307 mbedtls_ssl_send_alert_message( ssl,
6308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6309 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006310 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6311 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006312 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6313 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006314 if( ssl->in_msg[i] != 0 )
6315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006317 mbedtls_ssl_send_alert_message( ssl,
6318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6319 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006321 }
6322
Hanno Becker33c3dc82019-01-30 14:46:46 +00006323 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6325 | (unsigned int) ssl->in_msg[i + 2];
6326 i += 3;
6327
6328 if( n < 128 || i + n > ssl->in_hslen )
6329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006331 mbedtls_ssl_send_alert_message( ssl,
6332 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6333 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 }
6336
Hanno Becker33c3dc82019-01-30 14:46:46 +00006337 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006338#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6339 if( crt_cnt++ == 0 &&
6340 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6341 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006342 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006343 /* During client-side renegotiation, check that the server's
6344 * end-CRTs hasn't changed compared to the initial handshake,
6345 * mitigating the triple handshake attack. On success, reuse
6346 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6348 if( ssl_check_peer_crt_unchanged( ssl,
6349 &ssl->in_msg[i],
6350 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006351 {
Hanno Becker35e41772019-02-05 15:37:23 +00006352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6353 mbedtls_ssl_send_alert_message( ssl,
6354 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6355 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6356 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006357 }
Hanno Becker35e41772019-02-05 15:37:23 +00006358
6359 /* Now we can safely free the original chain. */
6360 ssl_clear_peer_cert( ssl->session );
6361 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006362#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6363
Hanno Becker33c3dc82019-01-30 14:46:46 +00006364 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006365#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006366 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006367#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006368 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006369 * it in-place from the input buffer instead of making a copy. */
6370 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6371#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006372 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006374 case 0: /*ok*/
6375 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6376 /* Ignore certificate with an unknown algorithm: maybe a
6377 prior certificate was already trusted. */
6378 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006379
Hanno Becker33c3dc82019-01-30 14:46:46 +00006380 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6381 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6382 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006383
Hanno Becker33c3dc82019-01-30 14:46:46 +00006384 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6385 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6386 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006387
Hanno Becker33c3dc82019-01-30 14:46:46 +00006388 default:
6389 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6390 crt_parse_der_failed:
6391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6392 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6393 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 }
6395
6396 i += n;
6397 }
6398
Hanno Becker35e41772019-02-05 15:37:23 +00006399 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006400 return( 0 );
6401}
6402
Hanno Beckerb8a08572019-02-05 12:49:06 +00006403#if defined(MBEDTLS_SSL_SRV_C)
6404static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6405{
6406 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6407 return( -1 );
6408
6409#if defined(MBEDTLS_SSL_PROTO_SSL3)
6410 /*
6411 * Check if the client sent an empty certificate
6412 */
6413 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6414 {
6415 if( ssl->in_msglen == 2 &&
6416 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6417 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6418 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6419 {
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6421 return( 0 );
6422 }
6423
6424 return( -1 );
6425 }
6426#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6427
6428#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6429 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6430 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6431 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6432 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6433 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6434 {
6435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6436 return( 0 );
6437 }
6438
Hanno Beckerb8a08572019-02-05 12:49:06 +00006439#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6440 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006441
6442 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006443}
6444#endif /* MBEDTLS_SSL_SRV_C */
6445
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006446/* Check if a certificate message is expected.
6447 * Return either
6448 * - SSL_CERTIFICATE_EXPECTED, or
6449 * - SSL_CERTIFICATE_SKIP
6450 * indicating whether a Certificate message is expected or not.
6451 */
6452#define SSL_CERTIFICATE_EXPECTED 0
6453#define SSL_CERTIFICATE_SKIP 1
6454static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6455 int authmode )
6456{
6457 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6458 ssl->handshake->ciphersuite_info;
6459
6460 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6461 return( SSL_CERTIFICATE_SKIP );
6462
6463#if defined(MBEDTLS_SSL_SRV_C)
6464 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6465 {
6466 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6467 return( SSL_CERTIFICATE_SKIP );
6468
6469 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6470 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006471 ssl->session_negotiate->verify_result =
6472 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6473 return( SSL_CERTIFICATE_SKIP );
6474 }
6475 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006476#else
6477 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006478#endif /* MBEDTLS_SSL_SRV_C */
6479
6480 return( SSL_CERTIFICATE_EXPECTED );
6481}
6482
Hanno Becker3cf50612019-02-05 14:36:34 +00006483static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6484 int authmode,
6485 mbedtls_x509_crt *chain,
6486 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006487{
Hanno Becker613d4902019-02-05 13:11:17 +00006488 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006489 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6490 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006491 mbedtls_x509_crt *ca_chain;
6492 mbedtls_x509_crl *ca_crl;
6493
6494 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6495 return( 0 );
6496
6497#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6498 if( ssl->handshake->sni_ca_chain != NULL )
6499 {
6500 ca_chain = ssl->handshake->sni_ca_chain;
6501 ca_crl = ssl->handshake->sni_ca_crl;
6502 }
6503 else
6504#endif
6505 {
6506 ca_chain = ssl->conf->ca_chain;
6507 ca_crl = ssl->conf->ca_crl;
6508 }
6509
6510 /*
6511 * Main check: verify certificate
6512 */
6513 ret = mbedtls_x509_crt_verify_restartable(
6514 chain,
6515 ca_chain, ca_crl,
6516 ssl->conf->cert_profile,
6517 ssl->hostname,
6518 &ssl->session_negotiate->verify_result,
6519 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6520
6521 if( ret != 0 )
6522 {
6523 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6524 }
6525
6526#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6527 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6528 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6529#endif
6530
6531 /*
6532 * Secondary checks: always done, but change 'ret' only if it was 0
6533 */
6534
6535#if defined(MBEDTLS_ECP_C)
6536 {
6537 const mbedtls_pk_context *pk = &chain->pk;
6538
6539 /* If certificate uses an EC key, make sure the curve is OK */
6540 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6541 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6542 {
6543 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6544
6545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6546 if( ret == 0 )
6547 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6548 }
6549 }
6550#endif /* MBEDTLS_ECP_C */
6551
6552 if( mbedtls_ssl_check_cert_usage( chain,
6553 ciphersuite_info,
6554 ! ssl->conf->endpoint,
6555 &ssl->session_negotiate->verify_result ) != 0 )
6556 {
6557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6558 if( ret == 0 )
6559 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6560 }
6561
6562 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6563 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6564 * with details encoded in the verification flags. All other kinds
6565 * of error codes, including those from the user provided f_vrfy
6566 * functions, are treated as fatal and lead to a failure of
6567 * ssl_parse_certificate even if verification was optional. */
6568 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6569 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6570 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6571 {
6572 ret = 0;
6573 }
6574
6575 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6576 {
6577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6578 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6579 }
6580
6581 if( ret != 0 )
6582 {
6583 uint8_t alert;
6584
6585 /* The certificate may have been rejected for several reasons.
6586 Pick one and send the corresponding alert. Which alert to send
6587 may be a subject of debate in some cases. */
6588 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6589 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6590 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6591 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6592 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6593 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6594 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6595 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6596 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6597 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6598 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6599 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6600 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6601 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6602 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6603 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6604 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6605 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6606 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6607 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6608 else
6609 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6610 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6611 alert );
6612 }
6613
6614#if defined(MBEDTLS_DEBUG_C)
6615 if( ssl->session_negotiate->verify_result != 0 )
6616 {
6617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6618 ssl->session_negotiate->verify_result ) );
6619 }
6620 else
6621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6623 }
6624#endif /* MBEDTLS_DEBUG_C */
6625
6626 return( ret );
6627}
6628
Hanno Becker34106f62019-02-08 14:59:05 +00006629#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006630
6631#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006632static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6633 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006634{
6635 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006636 /* Remember digest of the peer's end-CRT. */
6637 ssl->session_negotiate->peer_cert_digest =
6638 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6639 if( ssl->session_negotiate->peer_cert_digest == NULL )
6640 {
6641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6642 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6643 mbedtls_ssl_send_alert_message( ssl,
6644 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6645 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6646
6647 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6648 }
6649
6650 ret = mbedtls_md( mbedtls_md_info_from_type(
6651 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6652 start, len,
6653 ssl->session_negotiate->peer_cert_digest );
6654
6655 ssl->session_negotiate->peer_cert_digest_type =
6656 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6657 ssl->session_negotiate->peer_cert_digest_len =
6658 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6659
6660 return( ret );
6661}
Hanno Becker5882dd02019-06-06 16:25:57 +01006662#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006663
6664static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6665 unsigned char *start, size_t len )
6666{
6667 unsigned char *end = start + len;
6668 int ret;
6669
6670 /* Make a copy of the peer's raw public key. */
6671 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6672 ret = mbedtls_pk_parse_subpubkey( &start, end,
6673 &ssl->handshake->peer_pubkey );
6674 if( ret != 0 )
6675 {
6676 /* We should have parsed the public key before. */
6677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6678 }
6679
6680 return( 0 );
6681}
6682#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6683
Hanno Becker3cf50612019-02-05 14:36:34 +00006684int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6685{
6686 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006687 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006688#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6689 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6690 ? ssl->handshake->sni_authmode
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006691 : mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006692#else
Hanno Beckeracd4fc02019-06-12 16:40:50 +01006693 const int authmode = mbedtls_ssl_conf_get_authmode( ssl->conf );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006694#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006695 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006696 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006697
6698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6699
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006700 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6701 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006702 {
6703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006704 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006705 }
6706
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006707#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6708 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006709 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006710 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006711 chain = ssl->handshake->ecrs_peer_cert;
6712 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006713 goto crt_verify;
6714 }
6715#endif
6716
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006717 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006718 {
6719 /* mbedtls_ssl_read_record may have sent an alert already. We
6720 let it decide whether to alert. */
6721 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006722 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006723 }
6724
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006725#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006726 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6727 {
6728 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006729
Hanno Beckerb8a08572019-02-05 12:49:06 +00006730 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006731 ret = 0;
6732 else
6733 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006734
Hanno Becker613d4902019-02-05 13:11:17 +00006735 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006736 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006737#endif /* MBEDTLS_SSL_SRV_C */
6738
Hanno Becker35e41772019-02-05 15:37:23 +00006739 /* Clear existing peer CRT structure in case we tried to
6740 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006741 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006742
6743 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6744 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006745 {
6746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6747 sizeof( mbedtls_x509_crt ) ) );
6748 mbedtls_ssl_send_alert_message( ssl,
6749 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6750 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006751
Hanno Beckere4aeb762019-02-05 17:19:52 +00006752 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6753 goto exit;
6754 }
6755 mbedtls_x509_crt_init( chain );
6756
6757 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006758 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006759 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006760
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006761#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6762 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006763 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006764
6765crt_verify:
6766 if( ssl->handshake->ecrs_enabled)
6767 rs_ctx = &ssl->handshake->ecrs_ctx;
6768#endif
6769
Hanno Becker3cf50612019-02-05 14:36:34 +00006770 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006771 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006772 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006773 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006774
Hanno Becker3008d282019-02-05 17:02:28 +00006775#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006776 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006777 size_t pk_len;
6778 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006779
Hanno Becker34106f62019-02-08 14:59:05 +00006780 /* We parse the CRT chain without copying, so
6781 * these pointers point into the input buffer,
6782 * and are hence still valid after freeing the
6783 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006784
Hanno Becker5882dd02019-06-06 16:25:57 +01006785#if defined(MBEDTLS_SSL_RENEGOTIATION)
6786 unsigned char *crt_start;
6787 size_t crt_len;
6788
Hanno Becker34106f62019-02-08 14:59:05 +00006789 crt_start = chain->raw.p;
6790 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006791#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006792
Hanno Becker34106f62019-02-08 14:59:05 +00006793 pk_start = chain->pk_raw.p;
6794 pk_len = chain->pk_raw.len;
6795
6796 /* Free the CRT structures before computing
6797 * digest and copying the peer's public key. */
6798 mbedtls_x509_crt_free( chain );
6799 mbedtls_free( chain );
6800 chain = NULL;
6801
Hanno Becker5882dd02019-06-06 16:25:57 +01006802#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006803 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006805 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006806#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006807
Hanno Becker34106f62019-02-08 14:59:05 +00006808 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006810 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 }
Hanno Becker34106f62019-02-08 14:59:05 +00006812#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6813 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006814 ssl->session_negotiate->peer_cert = chain;
6815 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006816#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
Hanno Becker613d4902019-02-05 13:11:17 +00006820exit:
6821
Hanno Beckere4aeb762019-02-05 17:19:52 +00006822 if( ret == 0 )
6823 ssl->state++;
6824
6825#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6826 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6827 {
6828 ssl->handshake->ecrs_peer_cert = chain;
6829 chain = NULL;
6830 }
6831#endif
6832
6833 if( chain != NULL )
6834 {
6835 mbedtls_x509_crt_free( chain );
6836 mbedtls_free( chain );
6837 }
6838
Paul Bakker5121ce52009-01-03 21:22:43 +00006839 return( ret );
6840}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006841#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006844{
6845 int ret;
6846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006849 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006850 ssl->out_msglen = 1;
6851 ssl->out_msg[0] = 1;
6852
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 ssl->state++;
6854
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006855 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006857 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006858 return( ret );
6859 }
6860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006862
6863 return( 0 );
6864}
6865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006867{
6868 int ret;
6869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006871
Hanno Becker327c93b2018-08-15 13:56:18 +01006872 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006875 return( ret );
6876 }
6877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006878 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006881 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6882 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006883 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006884 }
6885
Hanno Beckere678eaa2018-08-21 14:57:46 +01006886 /* CCS records are only accepted if they have length 1 and content '1',
6887 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006889 /*
6890 * Switch to our negotiated transform and session parameters for inbound
6891 * data.
6892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006894 ssl->transform_in = ssl->transform_negotiate;
6895 ssl->session_in = ssl->session_negotiate;
6896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006898 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006900#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006901 ssl_dtls_replay_reset( ssl );
6902#endif
6903
6904 /* Increment epoch */
6905 if( ++ssl->in_epoch == 0 )
6906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006908 /* This is highly unlikely to happen for legitimate reasons, so
6909 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006910 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006911 }
6912 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006913 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006914#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006915#if defined(MBEDTLS_SSL_PROTO_TLS)
6916 {
6917 memset( ssl->in_ctr, 0, 8 );
6918 }
6919#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006920
Hanno Beckerf5970a02019-05-08 09:38:41 +01006921 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6924 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006926 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006929 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6930 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006931 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006932 }
6933 }
6934#endif
6935
Paul Bakker5121ce52009-01-03 21:22:43 +00006936 ssl->state++;
6937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006939
6940 return( 0 );
6941}
6942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6944 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006945{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006946 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6949 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6950 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006951 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006952 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006954#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6955#if defined(MBEDTLS_SHA512_C)
6956 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006957 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6958 else
6959#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006960#if defined(MBEDTLS_SHA256_C)
6961 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006962 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006963 else
6964#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006965#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006968 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006969 }
Paul Bakker380da532012-04-18 16:10:25 +00006970}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006972void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006973{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6975 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006976 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6977 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006979#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6980#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006983#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006984 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006985#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006987}
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006990 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006991{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006992#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6993 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006994 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6995 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006996#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6998#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006999 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007000#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007001#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007002 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007003#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007004#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007005}
7006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7008 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7009static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007010 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007011{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007012 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7013 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007015#endif
Paul Bakker380da532012-04-18 16:10:25 +00007016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7018#if defined(MBEDTLS_SHA256_C)
7019static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007020 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007021{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007022 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007023}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007024#endif
Paul Bakker380da532012-04-18 16:10:25 +00007025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007026#if defined(MBEDTLS_SHA512_C)
7027static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007028 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007029{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007030 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007031}
Paul Bakker769075d2012-11-24 11:26:46 +01007032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007036static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007037 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007038{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007039 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007040 mbedtls_md5_context md5;
7041 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007042
Paul Bakker5121ce52009-01-03 21:22:43 +00007043 unsigned char padbuf[48];
7044 unsigned char md5sum[16];
7045 unsigned char sha1sum[20];
7046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007048 if( !session )
7049 session = ssl->session;
7050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007052
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007053 mbedtls_md5_init( &md5 );
7054 mbedtls_sha1_init( &sha1 );
7055
7056 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7057 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007058
7059 /*
7060 * SSLv3:
7061 * hash =
7062 * MD5( master + pad2 +
7063 * MD5( handshake + sender + master + pad1 ) )
7064 * + SHA1( master + pad2 +
7065 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007066 */
7067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007068#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007069 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7070 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007071#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007074 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7075 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007079 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Paul Bakker1ef83d62012-04-11 12:09:53 +00007081 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007082
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007083 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7084 mbedtls_md5_update_ret( &md5, session->master, 48 );
7085 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7086 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007088 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7089 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7090 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7091 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007095 mbedtls_md5_starts_ret( &md5 );
7096 mbedtls_md5_update_ret( &md5, session->master, 48 );
7097 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7098 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7099 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007100
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007101 mbedtls_sha1_starts_ret( &sha1 );
7102 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7103 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7104 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7105 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007108
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007109 mbedtls_md5_free( &md5 );
7110 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007112 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7113 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7114 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007121static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007124 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007125 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007126 mbedtls_md5_context md5;
7127 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007131 if( !session )
7132 session = ssl->session;
7133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007135
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007136 mbedtls_md5_init( &md5 );
7137 mbedtls_sha1_init( &sha1 );
7138
7139 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7140 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007141
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142 /*
7143 * TLSv1:
7144 * hash = PRF( master, finished_label,
7145 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7146 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007149 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7150 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007151#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007154 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7155 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007156#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007159 ? "client finished"
7160 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007161
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007162 mbedtls_md5_finish_ret( &md5, padbuf );
7163 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007164
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007165 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007166 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007169
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007170 mbedtls_md5_free( &md5 );
7171 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007172
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007173 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7180#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007181static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007183{
7184 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007185 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007186 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007187 unsigned char padbuf[32];
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007190 if( !session )
7191 session = ssl->session;
7192
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007193 mbedtls_sha256_init( &sha256 );
7194
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007197 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198
7199 /*
7200 * TLSv1.2:
7201 * hash = PRF( master, finished_label,
7202 * Hash( handshake ) )[0.11]
7203 */
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if !defined(MBEDTLS_SHA256_ALT)
7206 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007207 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007208#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007211 ? "client finished"
7212 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007213
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007214 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007215
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007216 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007217 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007220
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007221 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007222
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007223 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007230static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007232{
7233 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007234 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007235 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007236 unsigned char padbuf[48];
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007239 if( !session )
7240 session = ssl->session;
7241
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007242 mbedtls_sha512_init( &sha512 );
7243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007246 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007247
7248 /*
7249 * TLSv1.2:
7250 * hash = PRF( master, finished_label,
7251 * Hash( handshake ) )[0.11]
7252 */
7253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007254#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007255 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7256 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007257#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007260 ? "client finished"
7261 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007262
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007263 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007264
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007265 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007266 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007269
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007270 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007271
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007272 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007275}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#endif /* MBEDTLS_SHA512_C */
7277#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007280{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007281 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007282
7283 /*
7284 * Free our handshake params
7285 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007286 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007288 ssl->handshake = NULL;
7289
7290 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007291 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007292 */
7293 if( ssl->transform )
7294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007295 mbedtls_ssl_transform_free( ssl->transform );
7296 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007297 }
7298 ssl->transform = ssl->transform_negotiate;
7299 ssl->transform_negotiate = NULL;
7300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007302}
7303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007305{
7306 int resume = ssl->handshake->resume;
7307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_RENEGOTIATION)
7311 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007314 ssl->renego_records_seen = 0;
7315 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007316#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007317
7318 /*
7319 * Free the previous session and switch in the current one
7320 */
Paul Bakker0a597072012-09-25 21:55:46 +00007321 if( ssl->session )
7322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007323#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007324 /* RFC 7366 3.1: keep the EtM state */
7325 ssl->session_negotiate->encrypt_then_mac =
7326 ssl->session->encrypt_then_mac;
7327#endif
7328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329 mbedtls_ssl_session_free( ssl->session );
7330 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007331 }
7332 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007333 ssl->session_negotiate = NULL;
7334
Paul Bakker0a597072012-09-25 21:55:46 +00007335 /*
7336 * Add cache entry
7337 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007338 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007339 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007340 resume == 0 )
7341 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007342 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007344 }
Paul Bakker0a597072012-09-25 21:55:46 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007347 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007348 ssl->handshake->flight != NULL )
7349 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007350 /* Cancel handshake timer */
7351 ssl_set_timer( ssl, 0 );
7352
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007353 /* Keep last flight around in case we need to resend it:
7354 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007356 }
7357 else
7358#endif
7359 ssl_handshake_wrapup_free_hs_transform( ssl );
7360
Paul Bakker48916f92012-09-16 19:57:18 +00007361 ssl->state++;
7362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007363 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007364}
7365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007367{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007368 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007371
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007372 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007373
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007374 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007375
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007376 /*
7377 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7378 * may define some other value. Currently (early 2016), no defined
7379 * ciphersuite does this (and this is unlikely to change as activity has
7380 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007385 ssl->verify_data_len = hash_len;
7386 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007387#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007388
Paul Bakker5121ce52009-01-03 21:22:43 +00007389 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7391 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007392
7393 /*
7394 * In case of session resuming, invert the client and server
7395 * ChangeCipherSpec messages order.
7396 */
Paul Bakker0a597072012-09-25 21:55:46 +00007397 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007400 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007402#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007406#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007407 }
7408 else
7409 ssl->state++;
7410
Paul Bakker48916f92012-09-16 19:57:18 +00007411 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007412 * Switch to our negotiated transform and session parameters for outbound
7413 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007418 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007419 {
7420 unsigned char i;
7421
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007422 /* Remember current epoch settings for resending */
7423 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007424 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007425
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007426 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007427 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007428
7429 /* Increment epoch */
7430 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007431 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007432 break;
7433
7434 /* The loop goes to its end iff the counter is wrapping */
7435 if( i == 0 )
7436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7438 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007439 }
7440 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007441 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007443#if defined(MBEDTLS_SSL_PROTO_TLS)
7444 {
7445 memset( ssl->cur_out_ctr, 0, 8 );
7446 }
7447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007448
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007449 ssl->transform_out = ssl->transform_negotiate;
7450 ssl->session_out = ssl->session_negotiate;
7451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7453 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7458 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007459 }
7460 }
7461#endif
7462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007464 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007466#endif
7467
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007468 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007469 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007470 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 return( ret );
7472 }
7473
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007474#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007475 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007476 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7477 {
7478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7479 return( ret );
7480 }
7481#endif
7482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484
7485 return( 0 );
7486}
7487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007489#define SSL_MAX_HASH_LEN 36
7490#else
7491#define SSL_MAX_HASH_LEN 12
7492#endif
7493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007494int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007495{
Paul Bakker23986e52011-04-24 08:57:21 +00007496 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007497 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007498 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007501
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007502 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503
Hanno Becker327c93b2018-08-15 13:56:18 +01007504 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 return( ret );
7508 }
7509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007511 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007513 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7514 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007516 }
7517
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007518 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SSL_PROTO_SSL3)
7520 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007521 hash_len = 36;
7522 else
7523#endif
7524 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7527 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007530 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7531 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007533 }
7534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007536 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007539 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7540 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007542 }
7543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007545 ssl->verify_data_len = hash_len;
7546 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007547#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007548
Paul Bakker0a597072012-09-25 21:55:46 +00007549 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007550 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007551#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007552 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007559 }
7560 else
7561 ssl->state++;
7562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007564 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007566#endif
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007569
7570 return( 0 );
7571}
7572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7578 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7579 mbedtls_md5_init( &handshake->fin_md5 );
7580 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007581 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7582 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007584#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7585#if defined(MBEDTLS_SHA256_C)
7586 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007587 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589#if defined(MBEDTLS_SHA512_C)
7590 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007591 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007594
7595 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007596
7597#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7598 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7599 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7600#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_DHM_C)
7603 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_ECDH_C)
7606 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007607#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007608#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007609 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007610#if defined(MBEDTLS_SSL_CLI_C)
7611 handshake->ecjpake_cache = NULL;
7612 handshake->ecjpake_cache_len = 0;
7613#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007614#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007615
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007616#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007617 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007618#endif
7619
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007620#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7621 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7622#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007623
7624#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7625 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7626 mbedtls_pk_init( &handshake->peer_pubkey );
7627#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007628}
7629
Hanno Becker611a83b2018-01-03 14:27:32 +00007630void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7635 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007636
Hanno Becker92231322018-01-03 15:32:51 +00007637#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 mbedtls_md_init( &transform->md_ctx_enc );
7639 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007640#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007641}
7642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007646}
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007649{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007650 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007651 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007653 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007655 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007656 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007657
7658 /*
7659 * Either the pointers are now NULL or cleared properly and can be freed.
7660 * Now allocate missing structures.
7661 */
7662 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007663 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007664 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007665 }
Paul Bakker48916f92012-09-16 19:57:18 +00007666
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007667 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007668 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007669 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007670 }
Paul Bakker48916f92012-09-16 19:57:18 +00007671
Paul Bakker82788fb2014-10-20 13:59:19 +02007672 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007673 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007674 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007675 }
Paul Bakker48916f92012-09-16 19:57:18 +00007676
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007677 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007678 if( ssl->handshake == NULL ||
7679 ssl->transform_negotiate == NULL ||
7680 ssl->session_negotiate == NULL )
7681 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 mbedtls_free( ssl->handshake );
7685 mbedtls_free( ssl->transform_negotiate );
7686 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007687
7688 ssl->handshake = NULL;
7689 ssl->transform_negotiate = NULL;
7690 ssl->session_negotiate = NULL;
7691
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007692 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007693 }
7694
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007695 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007696 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007697 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007698 ssl_handshake_params_init( ssl->handshake );
7699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007701 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007702 {
7703 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007704
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7706 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7707 else
7708 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007709
7710 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007711 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007712#endif
7713
Paul Bakker48916f92012-09-16 19:57:18 +00007714 return( 0 );
7715}
7716
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007717#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007718/* Dummy cookie callbacks for defaults */
7719static int ssl_cookie_write_dummy( void *ctx,
7720 unsigned char **p, unsigned char *end,
7721 const unsigned char *cli_id, size_t cli_id_len )
7722{
7723 ((void) ctx);
7724 ((void) p);
7725 ((void) end);
7726 ((void) cli_id);
7727 ((void) cli_id_len);
7728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007730}
7731
7732static int ssl_cookie_check_dummy( void *ctx,
7733 const unsigned char *cookie, size_t cookie_len,
7734 const unsigned char *cli_id, size_t cli_id_len )
7735{
7736 ((void) ctx);
7737 ((void) cookie);
7738 ((void) cookie_len);
7739 ((void) cli_id);
7740 ((void) cli_id_len);
7741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007743}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007744#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007745
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007746/* Once ssl->out_hdr as the address of the beginning of the
7747 * next outgoing record is set, deduce the other pointers.
7748 *
7749 * Note: For TLS, we save the implicit record sequence number
7750 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7751 * and the caller has to make sure there's space for this.
7752 */
7753
7754static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7755 mbedtls_ssl_transform *transform )
7756{
7757#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007758 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007759 {
7760 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007761#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007762 ssl->out_cid = ssl->out_ctr + 8;
7763 ssl->out_len = ssl->out_cid;
7764 if( transform != NULL )
7765 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007766#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007767 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007768#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007769 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007770 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007771 MBEDTLS_SSL_TRANSPORT_ELSE
7772#endif /* MBEDTLS_SSL_PROTO_DTLS */
7773#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007774 {
7775 ssl->out_ctr = ssl->out_hdr - 8;
7776 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007777#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007778 ssl->out_cid = ssl->out_len;
7779#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007780 ssl->out_iv = ssl->out_hdr + 5;
7781 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007782#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007783
7784 /* Adjust out_msg to make space for explicit IV, if used. */
7785 if( transform != NULL &&
7786 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7787 {
7788 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7789 }
7790 else
7791 ssl->out_msg = ssl->out_iv;
7792}
7793
7794/* Once ssl->in_hdr as the address of the beginning of the
7795 * next incoming record is set, deduce the other pointers.
7796 *
7797 * Note: For TLS, we save the implicit record sequence number
7798 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7799 * and the caller has to make sure there's space for this.
7800 */
7801
Hanno Beckerf5970a02019-05-08 09:38:41 +01007802static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007803{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007804 /* This function sets the pointers to match the case
7805 * of unprotected TLS/DTLS records, with both ssl->in_iv
7806 * and ssl->in_msg pointing to the beginning of the record
7807 * content.
7808 *
7809 * When decrypting a protected record, ssl->in_msg
7810 * will be shifted to point to the beginning of the
7811 * record plaintext.
7812 */
7813
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007815 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007816 {
Hanno Becker70e79282019-05-03 14:34:53 +01007817 /* This sets the header pointers to match records
7818 * without CID. When we receive a record containing
7819 * a CID, the fields are shifted accordingly in
7820 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007821 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007823 ssl->in_cid = ssl->in_ctr + 8;
7824 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007825#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007826 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007827#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007828 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007829 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007830 MBEDTLS_SSL_TRANSPORT_ELSE
7831#endif /* MBEDTLS_SSL_PROTO_DTLS */
7832#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007833 {
7834 ssl->in_ctr = ssl->in_hdr - 8;
7835 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007836#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007837 ssl->in_cid = ssl->in_len;
7838#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007839 ssl->in_iv = ssl->in_hdr + 5;
7840 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007841#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007842
Hanno Beckerf5970a02019-05-08 09:38:41 +01007843 /* This will be adjusted at record decryption time. */
7844 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007845}
7846
Paul Bakker5121ce52009-01-03 21:22:43 +00007847/*
7848 * Initialize an SSL context
7849 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007850void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7851{
7852 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7853}
7854
7855/*
7856 * Setup an SSL context
7857 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007858
7859static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7860{
7861 /* Set the incoming and outgoing record pointers. */
7862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007863 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007864 {
7865 ssl->out_hdr = ssl->out_buf;
7866 ssl->in_hdr = ssl->in_buf;
7867 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007868 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007869#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007870#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007871 {
7872 ssl->out_hdr = ssl->out_buf + 8;
7873 ssl->in_hdr = ssl->in_buf + 8;
7874 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007875#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007876
7877 /* Derive other internal pointers. */
7878 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007879 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007880}
7881
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007882int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007883 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007884{
Paul Bakker48916f92012-09-16 19:57:18 +00007885 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007886
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007887 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007888
7889 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007890 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007891 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007892
7893 /* Set to NULL in case of an error condition */
7894 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007895
Angus Grattond8213d02016-05-25 20:56:48 +10007896 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7897 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007898 {
Angus Grattond8213d02016-05-25 20:56:48 +10007899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007900 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007901 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007902 }
7903
7904 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7905 if( ssl->out_buf == NULL )
7906 {
7907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007908 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007909 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007910 }
7911
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007912 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007913
Paul Bakker48916f92012-09-16 19:57:18 +00007914 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007915 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007916
7917 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007918
7919error:
7920 mbedtls_free( ssl->in_buf );
7921 mbedtls_free( ssl->out_buf );
7922
7923 ssl->conf = NULL;
7924
7925 ssl->in_buf = NULL;
7926 ssl->out_buf = NULL;
7927
7928 ssl->in_hdr = NULL;
7929 ssl->in_ctr = NULL;
7930 ssl->in_len = NULL;
7931 ssl->in_iv = NULL;
7932 ssl->in_msg = NULL;
7933
7934 ssl->out_hdr = NULL;
7935 ssl->out_ctr = NULL;
7936 ssl->out_len = NULL;
7937 ssl->out_iv = NULL;
7938 ssl->out_msg = NULL;
7939
k-stachowiak9f7798e2018-07-31 16:52:32 +02007940 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007941}
7942
7943/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007944 * Reset an initialized and used SSL context for re-use while retaining
7945 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007946 *
7947 * If partial is non-zero, keep data in the input buffer and client ID.
7948 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007949 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007950static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007951{
Paul Bakker48916f92012-09-16 19:57:18 +00007952 int ret;
7953
Hanno Becker7e772132018-08-10 12:38:21 +01007954#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7955 !defined(MBEDTLS_SSL_SRV_C)
7956 ((void) partial);
7957#endif
7958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007960
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007961 /* Cancel any possibly running timer */
7962 ssl_set_timer( ssl, 0 );
7963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964#if defined(MBEDTLS_SSL_RENEGOTIATION)
7965 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007966 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007967
7968 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7970 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007973
Paul Bakker7eb013f2011-10-06 12:37:39 +00007974 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007975 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007976
7977 ssl->in_msgtype = 0;
7978 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007980 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007981 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007982#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007984 ssl_dtls_replay_reset( ssl );
7985#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007986
7987 ssl->in_hslen = 0;
7988 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007989
7990 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007991
7992 ssl->out_msgtype = 0;
7993 ssl->out_msglen = 0;
7994 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7996 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007997 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007998#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007999
Hanno Becker19859472018-08-06 09:40:20 +01008000 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8001
Paul Bakker48916f92012-09-16 19:57:18 +00008002 ssl->transform_in = NULL;
8003 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008004
Hanno Becker78640902018-08-13 16:35:15 +01008005 ssl->session_in = NULL;
8006 ssl->session_out = NULL;
8007
Angus Grattond8213d02016-05-25 20:56:48 +10008008 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008009
8010#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008011 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008012#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8013 {
8014 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008015 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008016 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8019 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8022 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8025 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008026 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008027 }
8028#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008029
Paul Bakker48916f92012-09-16 19:57:18 +00008030 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008032 mbedtls_ssl_transform_free( ssl->transform );
8033 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008034 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008035 }
Paul Bakker48916f92012-09-16 19:57:18 +00008036
Paul Bakkerc0463502013-02-14 11:19:38 +01008037 if( ssl->session )
8038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039 mbedtls_ssl_session_free( ssl->session );
8040 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008041 ssl->session = NULL;
8042 }
8043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008044#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008045 ssl->alpn_chosen = NULL;
8046#endif
8047
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008048#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008049#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008050 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008051#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008052 {
8053 mbedtls_free( ssl->cli_id );
8054 ssl->cli_id = NULL;
8055 ssl->cli_id_len = 0;
8056 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008057#endif
8058
Paul Bakker48916f92012-09-16 19:57:18 +00008059 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8060 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008061
8062 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008063}
8064
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008065/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008066 * Reset an initialized and used SSL context for re-use while retaining
8067 * all application-set variables, function pointers and data.
8068 */
8069int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8070{
8071 return( ssl_session_reset_int( ssl, 0 ) );
8072}
8073
8074/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008075 * SSL set accessors
8076 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008077void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008079 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008080}
8081
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008082void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008083{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008084 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008085}
8086
Hanno Becker7f376f42019-06-12 16:20:48 +01008087#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
8088 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008089void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008090{
Hanno Becker7f376f42019-06-12 16:20:48 +01008091 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008092}
Hanno Becker7f376f42019-06-12 16:20:48 +01008093#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY && !MBEDTLS_SSL_CONF_ANTI_REPLAY */
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008094
Hanno Beckerde671542019-06-12 16:30:46 +01008095#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) && \
8096 !defined(MBEDTLS_SSL_CONF_BADMAC_LIMIT)
8097void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf,
8098 unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008099{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008100 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008101}
Hanno Beckerde671542019-06-12 16:30:46 +01008102#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT && !MBEDTLS_SSL_CONF_BADMAC_LIMIT */
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008105
Hanno Becker1841b0a2018-08-24 11:13:57 +01008106void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8107 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008108{
8109 ssl->disable_datagram_packing = !allow_packing;
8110}
8111
Hanno Becker1f835fa2019-06-13 10:14:59 +01008112#if !( defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX) && \
8113 defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN) )
Hanno Becker04da1892018-08-14 13:22:10 +01008114void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8115 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008117 conf->hs_timeout_min = min;
8118 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008119}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008120#else /* !( MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8121 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX ) */
8122void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8123 uint32_t min, uint32_t max )
8124{
8125 ((void) conf);
8126 ((void) min);
8127 ((void) max);
8128}
8129#endif /* MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN &&
8130 MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
8131
8132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008134void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008135{
Hanno Beckeracd4fc02019-06-12 16:40:50 +01008136#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
8137 conf->authmode = authmode;
8138#else
8139 ((void) conf);
8140 ((void) authmode);
8141#endif /* MBEDTLS_SSL_CONF_AUTHMODE */
Paul Bakker5121ce52009-01-03 21:22:43 +00008142}
8143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008145void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008146 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008147 void *p_vrfy )
8148{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008149 conf->f_vrfy = f_vrfy;
8150 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008153
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008154void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008155 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008156 void *p_rng )
8157{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008158 conf->f_rng = f_rng;
8159 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008160}
8161
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008162void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008163 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008164 void *p_dbg )
8165{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008166 conf->f_dbg = f_dbg;
8167 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008168}
8169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008171 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008172 mbedtls_ssl_send_t *f_send,
8173 mbedtls_ssl_recv_t *f_recv,
8174 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008175{
8176 ssl->p_bio = p_bio;
8177 ssl->f_send = f_send;
8178 ssl->f_recv = f_recv;
8179 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008180}
8181
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008182#if defined(MBEDTLS_SSL_PROTO_DTLS)
8183void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8184{
8185 ssl->mtu = mtu;
8186}
8187#endif
8188
Hanno Becker1f835fa2019-06-13 10:14:59 +01008189#if !defined(MBEDTLS_SSL_CONF_READ_TIMEOUT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008190void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008191{
8192 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008193}
Hanno Becker1f835fa2019-06-13 10:14:59 +01008194#endif /* MBEDTLS_SSL_CONF_READ_TIMEOUT */
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008195
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008196void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8197 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008198 mbedtls_ssl_set_timer_t *f_set_timer,
8199 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008200{
8201 ssl->p_timer = p_timer;
8202 ssl->f_set_timer = f_set_timer;
8203 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008204
8205 /* Make sure we start with no timer running */
8206 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008207}
8208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008210void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008211 void *p_cache,
8212 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8213 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008214{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008215 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008216 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008217 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008219#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008221#if defined(MBEDTLS_SSL_CLI_C)
8222int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008223{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008224 int ret;
8225
8226 if( ssl == NULL ||
8227 session == NULL ||
8228 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008229 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008232 }
8233
Hanno Becker58fccf22019-02-06 14:30:46 +00008234 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8235 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008236 return( ret );
8237
Paul Bakker0a597072012-09-25 21:55:46 +00008238 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008239
8240 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008242#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008243
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008244void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008245 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008246{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008247 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8248 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8249 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8250 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008251}
8252
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008253void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008254 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008255 int major, int minor )
8256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008257 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008258 return;
8259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008260 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008261 return;
8262
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008263 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008264}
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008267void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008268 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008269{
8270 conf->cert_profile = profile;
8271}
8272
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008273/* Append a new keycert entry to a (possibly empty) list */
8274static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8275 mbedtls_x509_crt *cert,
8276 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008277{
niisato8ee24222018-06-25 19:05:48 +09008278 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008279
niisato8ee24222018-06-25 19:05:48 +09008280 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8281 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008283
niisato8ee24222018-06-25 19:05:48 +09008284 new_cert->cert = cert;
8285 new_cert->key = key;
8286 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008287
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008288 /* Update head is the list was null, else add to the end */
8289 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008290 {
niisato8ee24222018-06-25 19:05:48 +09008291 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008292 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008293 else
8294 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008295 mbedtls_ssl_key_cert *cur = *head;
8296 while( cur->next != NULL )
8297 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008298 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008299 }
8300
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008301 return( 0 );
8302}
8303
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008304int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008305 mbedtls_x509_crt *own_cert,
8306 mbedtls_pk_context *pk_key )
8307{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008308 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008309}
8310
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008311void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008312 mbedtls_x509_crt *ca_chain,
8313 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008314{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008315 conf->ca_chain = ca_chain;
8316 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008319
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008320#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8321int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8322 mbedtls_x509_crt *own_cert,
8323 mbedtls_pk_context *pk_key )
8324{
8325 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8326 own_cert, pk_key ) );
8327}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008328
8329void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8330 mbedtls_x509_crt *ca_chain,
8331 mbedtls_x509_crl *ca_crl )
8332{
8333 ssl->handshake->sni_ca_chain = ca_chain;
8334 ssl->handshake->sni_ca_crl = ca_crl;
8335}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008336
8337void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8338 int authmode )
8339{
8340 ssl->handshake->sni_authmode = authmode;
8341}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008342#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8343
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008344#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008345/*
8346 * Set EC J-PAKE password for current handshake
8347 */
8348int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8349 const unsigned char *pw,
8350 size_t pw_len )
8351{
8352 mbedtls_ecjpake_role role;
8353
Janos Follath8eb64132016-06-03 15:40:57 +01008354 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8356
8357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8358 role = MBEDTLS_ECJPAKE_SERVER;
8359 else
8360 role = MBEDTLS_ECJPAKE_CLIENT;
8361
8362 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8363 role,
8364 MBEDTLS_MD_SHA256,
8365 MBEDTLS_ECP_DP_SECP256R1,
8366 pw, pw_len ) );
8367}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008368#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008370#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008371int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008372 const unsigned char *psk, size_t psk_len,
8373 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008374{
Paul Bakker6db455e2013-09-18 17:29:31 +02008375 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008378 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8379 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008380
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008381 /* Identity len will be encoded on two bytes */
8382 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008383 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008384 {
8385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8386 }
8387
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008388 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008389 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008390 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008391
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008392 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008393 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008394 conf->psk_len = 0;
8395 }
8396 if( conf->psk_identity != NULL )
8397 {
8398 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008399 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008400 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008401 }
8402
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008403 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8404 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008405 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008406 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008407 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008408 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008409 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008410 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008411 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008412
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008413 conf->psk_len = psk_len;
8414 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008415
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008416 memcpy( conf->psk, psk, conf->psk_len );
8417 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008418
8419 return( 0 );
8420}
8421
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008422int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8423 const unsigned char *psk, size_t psk_len )
8424{
8425 if( psk == NULL || ssl->handshake == NULL )
8426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8427
8428 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8430
8431 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008432 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008433 mbedtls_platform_zeroize( ssl->handshake->psk,
8434 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008435 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008436 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008437 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008438
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008439 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008440 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008441
8442 ssl->handshake->psk_len = psk_len;
8443 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8444
8445 return( 0 );
8446}
8447
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008448void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008450 size_t),
8451 void *p_psk )
8452{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008453 conf->f_psk = f_psk;
8454 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008456#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008457
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008458#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008459
8460#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008462{
8463 int ret;
8464
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008465 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8466 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8467 {
8468 mbedtls_mpi_free( &conf->dhm_P );
8469 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008470 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008471 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008472
8473 return( 0 );
8474}
Hanno Becker470a8c42017-10-04 15:28:46 +01008475#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008476
Hanno Beckera90658f2017-10-04 15:29:08 +01008477int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8478 const unsigned char *dhm_P, size_t P_len,
8479 const unsigned char *dhm_G, size_t G_len )
8480{
8481 int ret;
8482
8483 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8484 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8485 {
8486 mbedtls_mpi_free( &conf->dhm_P );
8487 mbedtls_mpi_free( &conf->dhm_G );
8488 return( ret );
8489 }
8490
8491 return( 0 );
8492}
Paul Bakker5121ce52009-01-03 21:22:43 +00008493
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008494int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008495{
8496 int ret;
8497
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008498 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8499 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8500 {
8501 mbedtls_mpi_free( &conf->dhm_P );
8502 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008503 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008504 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008505
8506 return( 0 );
8507}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008508#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008509
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008510#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8511/*
8512 * Set the minimum length for Diffie-Hellman parameters
8513 */
8514void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8515 unsigned int bitlen )
8516{
8517 conf->dhm_min_bitlen = bitlen;
8518}
8519#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8520
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008521#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008522/*
8523 * Set allowed/preferred hashes for handshake signatures
8524 */
8525void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8526 const int *hashes )
8527{
8528 conf->sig_hashes = hashes;
8529}
Hanno Becker947194e2017-04-07 13:25:49 +01008530#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008531
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008532#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008533/*
8534 * Set the allowed elliptic curves
8535 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008536void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008537 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008539 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008540}
Hanno Becker947194e2017-04-07 13:25:49 +01008541#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008542
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008543#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008545{
Hanno Becker947194e2017-04-07 13:25:49 +01008546 /* Initialize to suppress unnecessary compiler warning */
8547 size_t hostname_len = 0;
8548
8549 /* Check if new hostname is valid before
8550 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008551 if( hostname != NULL )
8552 {
8553 hostname_len = strlen( hostname );
8554
8555 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8557 }
8558
8559 /* Now it's clear that we will overwrite the old hostname,
8560 * so we can free it safely */
8561
8562 if( ssl->hostname != NULL )
8563 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008564 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008565 mbedtls_free( ssl->hostname );
8566 }
8567
8568 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008569
Paul Bakker5121ce52009-01-03 21:22:43 +00008570 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008571 {
8572 ssl->hostname = NULL;
8573 }
8574 else
8575 {
8576 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008577 if( ssl->hostname == NULL )
8578 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008579
Hanno Becker947194e2017-04-07 13:25:49 +01008580 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008581
Hanno Becker947194e2017-04-07 13:25:49 +01008582 ssl->hostname[hostname_len] = '\0';
8583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008584
8585 return( 0 );
8586}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008587#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008588
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008589#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008590void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008591 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008592 const unsigned char *, size_t),
8593 void *p_sni )
8594{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008595 conf->f_sni = f_sni;
8596 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008598#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008602{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008603 size_t cur_len, tot_len;
8604 const char **p;
8605
8606 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008607 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8608 * MUST NOT be truncated."
8609 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008610 */
8611 tot_len = 0;
8612 for( p = protos; *p != NULL; p++ )
8613 {
8614 cur_len = strlen( *p );
8615 tot_len += cur_len;
8616
8617 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008618 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008619 }
8620
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008621 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008622
8623 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008624}
8625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008627{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008628 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008630#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008631
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008632void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008634 conf->max_major_ver = major;
8635 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008636}
8637
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008638void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008640 conf->min_major_ver = major;
8641 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008642}
8643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008645void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008646{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008647 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008648}
8649#endif
8650
Janos Follath088ce432017-04-10 12:42:31 +01008651#if defined(MBEDTLS_SSL_SRV_C)
8652void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8653 char cert_req_ca_list )
8654{
8655 conf->cert_req_ca_list = cert_req_ca_list;
8656}
8657#endif
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008660void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008662 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008663}
8664#endif
8665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008666#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008667#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008669{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008670 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008671}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008672#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8673#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008674void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008675 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008676{
8677 conf->enforce_extended_master_secret = ems_enf;
8678}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008679#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008680#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008681
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008682#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008683void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008684{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008685 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008686}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008687#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008689#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008690int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008691{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008692 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008693 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008695 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008696 }
8697
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008698 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008699
8700 return( 0 );
8701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008702#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008704#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008705void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008706{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008707 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008709#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008711#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008712void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008713{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008714 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008715}
8716#endif
8717
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008718#if !defined(MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008719void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008721 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008722}
Hanno Beckerb0b2b672019-06-12 16:58:10 +01008723#endif /* !MBEDTLS_SSL_CONF_ALLOW_LEGACY_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00008724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008725#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008726void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008727{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008728 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008729}
8730
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008733 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008734}
8735
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008736void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008737 const unsigned char period[8] )
8738{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008739 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008740}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008741#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008743#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008744#if defined(MBEDTLS_SSL_CLI_C)
8745void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008746{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008747 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008748}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008749#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008750
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008751#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008752void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8753 mbedtls_ssl_ticket_write_t *f_ticket_write,
8754 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8755 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008756{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008757 conf->f_ticket_write = f_ticket_write;
8758 conf->f_ticket_parse = f_ticket_parse;
8759 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008760}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008761#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008763
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008764#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8765void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8766 mbedtls_ssl_export_keys_t *f_export_keys,
8767 void *p_export_keys )
8768{
8769 conf->f_export_keys = f_export_keys;
8770 conf->p_export_keys = p_export_keys;
8771}
8772#endif
8773
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008774#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008775void mbedtls_ssl_conf_async_private_cb(
8776 mbedtls_ssl_config *conf,
8777 mbedtls_ssl_async_sign_t *f_async_sign,
8778 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8779 mbedtls_ssl_async_resume_t *f_async_resume,
8780 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008781 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008782{
8783 conf->f_async_sign_start = f_async_sign;
8784 conf->f_async_decrypt_start = f_async_decrypt;
8785 conf->f_async_resume = f_async_resume;
8786 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008787 conf->p_async_config_data = async_config_data;
8788}
8789
Gilles Peskine8f97af72018-04-26 11:46:10 +02008790void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8791{
8792 return( conf->p_async_config_data );
8793}
8794
Gilles Peskine1febfef2018-04-30 11:54:39 +02008795void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008796{
8797 if( ssl->handshake == NULL )
8798 return( NULL );
8799 else
8800 return( ssl->handshake->user_async_ctx );
8801}
8802
Gilles Peskine1febfef2018-04-30 11:54:39 +02008803void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008804 void *ctx )
8805{
8806 if( ssl->handshake != NULL )
8807 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008808}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008809#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008810
Paul Bakker5121ce52009-01-03 21:22:43 +00008811/*
8812 * SSL get accessors
8813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008814size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008815{
8816 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8817}
8818
Hanno Becker8b170a02017-10-10 11:51:19 +01008819int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8820{
8821 /*
8822 * Case A: We're currently holding back
8823 * a message for further processing.
8824 */
8825
8826 if( ssl->keep_current_message == 1 )
8827 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008828 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008829 return( 1 );
8830 }
8831
8832 /*
8833 * Case B: Further records are pending in the current datagram.
8834 */
8835
8836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008837 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008838 ssl->in_left > ssl->next_record_offset )
8839 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008841 return( 1 );
8842 }
8843#endif /* MBEDTLS_SSL_PROTO_DTLS */
8844
8845 /*
8846 * Case C: A handshake message is being processed.
8847 */
8848
Hanno Becker8b170a02017-10-10 11:51:19 +01008849 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8850 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008851 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008852 return( 1 );
8853 }
8854
8855 /*
8856 * Case D: An application data message is being processed
8857 */
8858 if( ssl->in_offt != NULL )
8859 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008860 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008861 return( 1 );
8862 }
8863
8864 /*
8865 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008866 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008867 * we implement support for multiple alerts in single records.
8868 */
8869
8870 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8871 return( 0 );
8872}
8873
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008874uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008875{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008876 if( ssl->session != NULL )
8877 return( ssl->session->verify_result );
8878
8879 if( ssl->session_negotiate != NULL )
8880 return( ssl->session_negotiate->verify_result );
8881
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008882 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008883}
8884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008885const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008886{
Paul Bakker926c8e42013-03-06 10:23:34 +01008887 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008888 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008891}
8892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008893const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008896 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008897 {
8898 switch( ssl->minor_ver )
8899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008901 return( "DTLSv1.0" );
8902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008903 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008904 return( "DTLSv1.2" );
8905
8906 default:
8907 return( "unknown (DTLS)" );
8908 }
8909 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008910 MBEDTLS_SSL_TRANSPORT_ELSE
8911#endif /* MBEDTLS_SSL_PROTO_DTLS */
8912#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008913 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008914 switch( ssl->minor_ver )
8915 {
8916 case MBEDTLS_SSL_MINOR_VERSION_0:
8917 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008918
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008919 case MBEDTLS_SSL_MINOR_VERSION_1:
8920 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008921
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008922 case MBEDTLS_SSL_MINOR_VERSION_2:
8923 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008924
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008925 case MBEDTLS_SSL_MINOR_VERSION_3:
8926 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008927
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008928 default:
8929 return( "unknown" );
8930 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008931 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008932#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008933}
8934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008935int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008936{
Hanno Becker3136ede2018-08-17 15:28:19 +01008937 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008938 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008939 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008940
Hanno Becker43395762019-05-03 14:46:38 +01008941 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8942
Hanno Becker78640902018-08-13 16:35:15 +01008943 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008944 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008946#if defined(MBEDTLS_ZLIB_SUPPORT)
8947 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8948 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008949#endif
8950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953 case MBEDTLS_MODE_GCM:
8954 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008955 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008957 transform_expansion = transform->minlen;
8958 break;
8959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008960 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008961
8962 block_size = mbedtls_cipher_get_block_size(
8963 &transform->cipher_ctx_enc );
8964
Hanno Becker3136ede2018-08-17 15:28:19 +01008965 /* Expansion due to the addition of the MAC. */
8966 transform_expansion += transform->maclen;
8967
8968 /* Expansion due to the addition of CBC padding;
8969 * Theoretically up to 256 bytes, but we never use
8970 * more than the block size of the underlying cipher. */
8971 transform_expansion += block_size;
8972
8973 /* For TLS 1.1 or higher, an explicit IV is added
8974 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008975#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8976 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008977 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008978#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008979
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008980 break;
8981
8982 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008985 }
8986
Hanno Beckera5a2b082019-05-15 14:03:01 +01008987#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008988 if( transform->out_cid_len != 0 )
8989 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008990#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008991
Hanno Becker43395762019-05-03 14:46:38 +01008992 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008993}
8994
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008995#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8996size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8997{
8998 size_t max_len;
8999
9000 /*
9001 * Assume mfl_code is correct since it was checked when set
9002 */
Angus Grattond8213d02016-05-25 20:56:48 +10009003 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009004
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009005 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009006 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009007 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009008 {
Angus Grattond8213d02016-05-25 20:56:48 +10009009 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009010 }
9011
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009012 /* During a handshake, use the value being negotiated */
9013 if( ssl->session_negotiate != NULL &&
9014 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9015 {
9016 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9017 }
9018
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009019 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009020}
9021#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9022
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009023#if defined(MBEDTLS_SSL_PROTO_DTLS)
9024static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9025{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009026 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9027 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9028 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9029 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9030 return ( 0 );
9031
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009032 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9033 return( ssl->mtu );
9034
9035 if( ssl->mtu == 0 )
9036 return( ssl->handshake->mtu );
9037
9038 return( ssl->mtu < ssl->handshake->mtu ?
9039 ssl->mtu : ssl->handshake->mtu );
9040}
9041#endif /* MBEDTLS_SSL_PROTO_DTLS */
9042
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009043int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9044{
9045 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9046
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009047#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9048 !defined(MBEDTLS_SSL_PROTO_DTLS)
9049 (void) ssl;
9050#endif
9051
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009052#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9053 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9054
9055 if( max_len > mfl )
9056 max_len = mfl;
9057#endif
9058
9059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009060 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009061 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009062 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009063 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9064 const size_t overhead = (size_t) ret;
9065
9066 if( ret < 0 )
9067 return( ret );
9068
9069 if( mtu <= overhead )
9070 {
9071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9072 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9073 }
9074
9075 if( max_len > mtu - overhead )
9076 max_len = mtu - overhead;
9077 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009078#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009079
Hanno Becker0defedb2018-08-10 12:35:02 +01009080#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9081 !defined(MBEDTLS_SSL_PROTO_DTLS)
9082 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009083#endif
9084
9085 return( (int) max_len );
9086}
9087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009088#if defined(MBEDTLS_X509_CRT_PARSE_C)
9089const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009090{
9091 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009092 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009093
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009094#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009095 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009096#else
9097 return( NULL );
9098#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009100#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009103int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9104 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009105{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009106 if( ssl == NULL ||
9107 dst == NULL ||
9108 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009109 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009112 }
9113
Hanno Becker58fccf22019-02-06 14:30:46 +00009114 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009116#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009117
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009118const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9119{
9120 if( ssl == NULL )
9121 return( NULL );
9122
9123 return( ssl->session );
9124}
9125
Paul Bakker5121ce52009-01-03 21:22:43 +00009126/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009127 * Define ticket header determining Mbed TLS version
9128 * and structure of the ticket.
9129 */
9130
Hanno Becker41527622019-05-16 12:50:45 +01009131/*
Hanno Becker26829e92019-05-28 14:30:45 +01009132 * Define bitflag determining compile-time settings influencing
9133 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009134 */
9135
Hanno Becker26829e92019-05-28 14:30:45 +01009136#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009137#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009138#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009139#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009140#endif /* MBEDTLS_HAVE_TIME */
9141
9142#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009143#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009144#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009145#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009146#endif /* MBEDTLS_X509_CRT_PARSE_C */
9147
9148#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009149#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009150#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009151#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009152#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9153
9154#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009155#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009156#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009157#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009158#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9159
9160#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009161#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009162#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009163#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009164#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9165
9166#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009167#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009168#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009169#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009170#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9171
Hanno Becker41527622019-05-16 12:50:45 +01009172#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9173#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9174#else
9175#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9176#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9177
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009178#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9179#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9180#else
9181#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9182#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9183
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009184#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9185#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9186#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9187#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9188#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9189#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9190#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009191#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009192
Hanno Becker26829e92019-05-28 14:30:45 +01009193#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009194 ( (uint16_t) ( \
9195 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9196 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9197 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9198 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9199 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9200 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009201 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9202 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009203
Hanno Becker557fe9f2019-05-16 12:41:07 +01009204static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009205 MBEDTLS_VERSION_MAJOR,
9206 MBEDTLS_VERSION_MINOR,
9207 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009208 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9209 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009210};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009211
9212/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009213 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009214 * (in the presentation language of TLS, RFC 8446 section 3)
9215 *
Hanno Becker26829e92019-05-28 14:30:45 +01009216 * opaque mbedtls_version[3]; // major, minor, patch
9217 * opaque session_format[2]; // version-specific 16-bit field determining
9218 * // the format of the remaining
9219 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009220 *
9221 * Note: When updating the format, remember to keep
9222 * these version+format bytes.
9223 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009224 * // In this version, `session_format` determines
9225 * // the setting of those compile-time
9226 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009227 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009228 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009229 * uint8 ciphersuite[2]; // defined by the standard
9230 * uint8 compression; // 0 or 1
9231 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009232 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009233 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009234 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009235 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9236 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9237 * case disabled: uint8_t peer_cert_digest_type;
9238 * opaque peer_cert_digest<0..2^8-1>;
9239 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009240 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009241 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009242 * uint8 mfl_code; // up to 255 according to standard
9243 * uint8 trunc_hmac; // 0 or 1
9244 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009245 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009246 * The order is the same as in the definition of the structure, except
9247 * verify_result is put before peer_cert so that all mandatory fields come
9248 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009249 */
9250int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9251 unsigned char *buf,
9252 size_t buf_len,
9253 size_t *olen )
9254{
9255 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009256 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009257#if defined(MBEDTLS_HAVE_TIME)
9258 uint64_t start;
9259#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009261#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009262 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009263#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009264#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009265
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009266 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009267 * Add version identifier
9268 */
9269
9270 used += sizeof( ssl_serialized_session_header );
9271
9272 if( used <= buf_len )
9273 {
9274 memcpy( p, ssl_serialized_session_header,
9275 sizeof( ssl_serialized_session_header ) );
9276 p += sizeof( ssl_serialized_session_header );
9277 }
9278
9279 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009280 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009281 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009282#if defined(MBEDTLS_HAVE_TIME)
9283 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009284
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009285 if( used <= buf_len )
9286 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009287 start = (uint64_t) session->start;
9288
9289 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9290 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9291 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9292 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9293 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9294 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9295 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9296 *p++ = (unsigned char)( ( start ) & 0xFF );
9297 }
9298#endif /* MBEDTLS_HAVE_TIME */
9299
9300 /*
9301 * Basic mandatory fields
9302 */
9303 used += 2 /* ciphersuite */
9304 + 1 /* compression */
9305 + 1 /* id_len */
9306 + sizeof( session->id )
9307 + sizeof( session->master )
9308 + 4; /* verify_result */
9309
9310 if( used <= buf_len )
9311 {
9312 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9313 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9314
9315 *p++ = (unsigned char)( session->compression & 0xFF );
9316
9317 *p++ = (unsigned char)( session->id_len & 0xFF );
9318 memcpy( p, session->id, 32 );
9319 p += 32;
9320
9321 memcpy( p, session->master, 48 );
9322 p += 48;
9323
9324 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9325 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9326 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9327 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009328 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009329
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009330 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009331 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009332 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009333#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009334#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009335 if( session->peer_cert == NULL )
9336 cert_len = 0;
9337 else
9338 cert_len = session->peer_cert->raw.len;
9339
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009340 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009341
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009342 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009343 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009344 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9345 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9346 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9347
9348 if( session->peer_cert != NULL )
9349 {
9350 memcpy( p, session->peer_cert->raw.p, cert_len );
9351 p += cert_len;
9352 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009353 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009354
Hanno Becker5882dd02019-06-06 16:25:57 +01009355#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009356 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009357 if( session->peer_cert_digest != NULL )
9358 {
9359 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9360 if( used <= buf_len )
9361 {
9362 *p++ = (unsigned char) session->peer_cert_digest_type;
9363 *p++ = (unsigned char) session->peer_cert_digest_len;
9364 memcpy( p, session->peer_cert_digest,
9365 session->peer_cert_digest_len );
9366 p += session->peer_cert_digest_len;
9367 }
9368 }
9369 else
9370 {
9371 used += 2;
9372 if( used <= buf_len )
9373 {
9374 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9375 *p++ = 0;
9376 }
9377 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009378#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009379#endif /* MBEDTLS_X509_CRT_PARSE_C */
9380
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009381 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009382 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009383 */
9384#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009385 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009386
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009387 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009388 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009389 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9390 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9391 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9392
9393 if( session->ticket != NULL )
9394 {
9395 memcpy( p, session->ticket, session->ticket_len );
9396 p += session->ticket_len;
9397 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009398
9399 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9400 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9401 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9402 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009403 }
9404#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9405
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009406 /*
9407 * Misc extension-related info
9408 */
9409#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9410 used += 1;
9411
9412 if( used <= buf_len )
9413 *p++ = session->mfl_code;
9414#endif
9415
9416#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9417 used += 1;
9418
9419 if( used <= buf_len )
9420 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9421#endif
9422
9423#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9424 used += 1;
9425
9426 if( used <= buf_len )
9427 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9428#endif
9429
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009430 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009431 *olen = used;
9432
9433 if( used > buf_len )
9434 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009435
9436 return( 0 );
9437}
9438
9439/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009440 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009441 *
9442 * This internal version is wrapped by a public function that cleans up in
9443 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009444 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009445static int ssl_session_load( mbedtls_ssl_session *session,
9446 const unsigned char *buf,
9447 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009448{
9449 const unsigned char *p = buf;
9450 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009451#if defined(MBEDTLS_HAVE_TIME)
9452 uint64_t start;
9453#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009455#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009456 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009457#endif
9458#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009459
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009460 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009461 * Check version identifier
9462 */
9463
9464 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009465 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009466
9467 if( memcmp( p, ssl_serialized_session_header,
9468 sizeof( ssl_serialized_session_header ) ) != 0 )
9469 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009470 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009471 }
9472 p += sizeof( ssl_serialized_session_header );
9473
9474 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009475 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009476 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009477#if defined(MBEDTLS_HAVE_TIME)
9478 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009479 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9480
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009481 start = ( (uint64_t) p[0] << 56 ) |
9482 ( (uint64_t) p[1] << 48 ) |
9483 ( (uint64_t) p[2] << 40 ) |
9484 ( (uint64_t) p[3] << 32 ) |
9485 ( (uint64_t) p[4] << 24 ) |
9486 ( (uint64_t) p[5] << 16 ) |
9487 ( (uint64_t) p[6] << 8 ) |
9488 ( (uint64_t) p[7] );
9489 p += 8;
9490
9491 session->start = (time_t) start;
9492#endif /* MBEDTLS_HAVE_TIME */
9493
9494 /*
9495 * Basic mandatory fields
9496 */
9497 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9499
9500 session->ciphersuite = ( p[0] << 8 ) | p[1];
9501 p += 2;
9502
9503 session->compression = *p++;
9504
9505 session->id_len = *p++;
9506 memcpy( session->id, p, 32 );
9507 p += 32;
9508
9509 memcpy( session->master, p, 48 );
9510 p += 48;
9511
9512 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9513 ( (uint32_t) p[1] << 16 ) |
9514 ( (uint32_t) p[2] << 8 ) |
9515 ( (uint32_t) p[3] );
9516 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009517
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009518 /* Immediately clear invalid pointer values that have been read, in case
9519 * we exit early before we replaced them with valid ones. */
9520#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009521#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009522 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009523#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009524 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009525#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009526#endif
9527#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9528 session->ticket = NULL;
9529#endif
9530
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009531 /*
9532 * Peer certificate
9533 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009534#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009535#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009536 if( 3 > (size_t)( end - p ) )
9537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9538
9539 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9540 p += 3;
9541
9542 if( cert_len == 0 )
9543 {
9544 session->peer_cert = NULL;
9545 }
9546 else
9547 {
9548 int ret;
9549
9550 if( cert_len > (size_t)( end - p ) )
9551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9552
9553 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9554
9555 if( session->peer_cert == NULL )
9556 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9557
9558 mbedtls_x509_crt_init( session->peer_cert );
9559
9560 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9561 p, cert_len ) ) != 0 )
9562 {
9563 mbedtls_x509_crt_free( session->peer_cert );
9564 mbedtls_free( session->peer_cert );
9565 session->peer_cert = NULL;
9566 return( ret );
9567 }
9568
9569 p += cert_len;
9570 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009571#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009572 /* Deserialize CRT digest from the end of the ticket. */
9573 if( 2 > (size_t)( end - p ) )
9574 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9575
9576 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9577 session->peer_cert_digest_len = (size_t) *p++;
9578
9579 if( session->peer_cert_digest_len != 0 )
9580 {
Hanno Becker2326d202019-06-06 14:54:55 +01009581 const mbedtls_md_info_t *md_info =
9582 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9583 if( md_info == NULL )
9584 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9585 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9586 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9587
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009588 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9589 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9590
9591 session->peer_cert_digest =
9592 mbedtls_calloc( 1, session->peer_cert_digest_len );
9593 if( session->peer_cert_digest == NULL )
9594 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9595
9596 memcpy( session->peer_cert_digest, p,
9597 session->peer_cert_digest_len );
9598 p += session->peer_cert_digest_len;
9599 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009600#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009601#endif /* MBEDTLS_X509_CRT_PARSE_C */
9602
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009603 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009604 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009605 */
9606#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9607 if( 3 > (size_t)( end - p ) )
9608 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9609
9610 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9611 p += 3;
9612
9613 if( session->ticket_len != 0 )
9614 {
9615 if( session->ticket_len > (size_t)( end - p ) )
9616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9617
9618 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9619 if( session->ticket == NULL )
9620 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9621
9622 memcpy( session->ticket, p, session->ticket_len );
9623 p += session->ticket_len;
9624 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009625
9626 if( 4 > (size_t)( end - p ) )
9627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9628
9629 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9630 ( (uint32_t) p[1] << 16 ) |
9631 ( (uint32_t) p[2] << 8 ) |
9632 ( (uint32_t) p[3] );
9633 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009634#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9635
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009636 /*
9637 * Misc extension-related info
9638 */
9639#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9640 if( 1 > (size_t)( end - p ) )
9641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9642
9643 session->mfl_code = *p++;
9644#endif
9645
9646#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9647 if( 1 > (size_t)( end - p ) )
9648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9649
9650 session->trunc_hmac = *p++;
9651#endif
9652
9653#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9654 if( 1 > (size_t)( end - p ) )
9655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9656
9657 session->encrypt_then_mac = *p++;
9658#endif
9659
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009660 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009661 if( p != end )
9662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9663
9664 return( 0 );
9665}
9666
9667/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009668 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009669 */
9670int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9671 const unsigned char *buf,
9672 size_t len )
9673{
9674 int ret = ssl_session_load( session, buf, len );
9675
9676 if( ret != 0 )
9677 mbedtls_ssl_session_free( session );
9678
9679 return( ret );
9680}
9681
9682/*
Paul Bakker1961b702013-01-25 14:49:24 +01009683 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009688
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#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009693 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009695#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009697 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009699#endif
9700
Paul Bakker1961b702013-01-25 14:49:24 +01009701 return( ret );
9702}
9703
9704/*
9705 * Perform the SSL handshake
9706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009708{
9709 int ret = 0;
9710
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009711 if( ssl == NULL || ssl->conf == NULL )
9712 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009719
9720 if( ret != 0 )
9721 break;
9722 }
9723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009725
9726 return( ret );
9727}
9728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729#if defined(MBEDTLS_SSL_RENEGOTIATION)
9730#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009731/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009732 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009735{
9736 int ret;
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009739
9740 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9742 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009743
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009744 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009745 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009747 return( ret );
9748 }
9749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009751
9752 return( 0 );
9753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009755
9756/*
9757 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009758 * - any side: calling mbedtls_ssl_renegotiate(),
9759 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9760 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009761 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009762 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009766{
9767 int ret;
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009770
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009771 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9772 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009773
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009774 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9775 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009777 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009779 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009780 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009781 ssl->handshake->out_msg_seq = 1;
9782 else
9783 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009784 }
9785#endif
9786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9788 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009790 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009793 return( ret );
9794 }
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009797
9798 return( 0 );
9799}
9800
9801/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009802 * Renegotiate current connection on client,
9803 * or request renegotiation on server
9804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009806{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009808
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009809 if( ssl == NULL || ssl->conf == NULL )
9810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009813 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009814 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009820
9821 /* Did we already try/start sending HelloRequest? */
9822 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009824
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009825 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009826 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009830 /*
9831 * On client, either start the renegotiation process or,
9832 * if already in progress, continue the handshake
9833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009838
9839 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009842 return( ret );
9843 }
9844 }
9845 else
9846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009850 return( ret );
9851 }
9852 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009854
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009855 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009856}
9857
9858/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009859 * Check record counters and renegotiate if they're above the limit.
9860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009862{
Andres AG2196c7f2016-12-15 17:01:16 +00009863 size_t ep_len = ssl_ep_len( ssl );
9864 int in_ctr_cmp;
9865 int out_ctr_cmp;
9866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9868 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009869 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009870 {
9871 return( 0 );
9872 }
9873
Andres AG2196c7f2016-12-15 17:01:16 +00009874 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9875 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009876 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009877 ssl->conf->renego_period + ep_len, 8 - ep_len );
9878
9879 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009880 {
9881 return( 0 );
9882 }
9883
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009888
9889/*
9890 * Receive application data decrypted from the SSL layer
9891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009893{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009894 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009895 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009896
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009897 if( ssl == NULL || ssl->conf == NULL )
9898 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009906 return( ret );
9907
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009908 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009910 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009911 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009912 return( ret );
9913 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009914 }
9915#endif
9916
Hanno Becker4a810fb2017-05-24 16:27:30 +01009917 /*
9918 * Check if renegotiation is necessary and/or handshake is
9919 * in process. If yes, perform/continue, and fall through
9920 * if an unexpected packet is received while the client
9921 * is waiting for the ServerHello.
9922 *
9923 * (There is no equivalent to the last condition on
9924 * the server-side as it is not treated as within
9925 * a handshake while waiting for the ClientHello
9926 * after a renegotiation request.)
9927 */
9928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009930 ret = ssl_check_ctr_renegotiate( ssl );
9931 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9932 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009935 return( ret );
9936 }
9937#endif
9938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009942 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9943 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009946 return( ret );
9947 }
9948 }
9949
Hanno Beckere41158b2017-10-23 13:30:32 +01009950 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009951 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009952 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009953 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009954 if( ssl->f_get_timer != NULL &&
9955 ssl->f_get_timer( ssl->p_timer ) == -1 )
9956 {
Hanno Becker1f835fa2019-06-13 10:14:59 +01009957 ssl_set_timer( ssl,
9958 mbedtls_ssl_conf_get_read_timeout( ssl->conf ) );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009959 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009960
Hanno Becker327c93b2018-08-15 13:56:18 +01009961 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009962 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009963 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9964 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009965
Hanno Becker4a810fb2017-05-24 16:27:30 +01009966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9967 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009968 }
9969
9970 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009972 {
9973 /*
9974 * OpenSSL sends empty messages to randomize the IV
9975 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009976 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009979 return( 0 );
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009982 return( ret );
9983 }
9984 }
9985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009989
Hanno Becker4a810fb2017-05-24 16:27:30 +01009990 /*
9991 * - For client-side, expect SERVER_HELLO_REQUEST.
9992 * - For server-side, expect CLIENT_HELLO.
9993 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9994 */
9995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009999 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010002
10003 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010005 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010006 {
10007 continue;
10008 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010009 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010010#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010011#if defined(MBEDTLS_SSL_PROTO_TLS)
10012 {
10013 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10014 }
10015#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010016 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010017#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010018
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010020 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010021 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010024
10025 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010027 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +010010028 {
10029 continue;
10030 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010031 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010032#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020010033#if defined(MBEDTLS_SSL_PROTO_TLS)
10034 {
10035 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
10036 }
10037#endif
Paul Bakker48916f92012-09-16 19:57:18 +000010038 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010039#endif /* MBEDTLS_SSL_SRV_C */
10040
Hanno Becker21df7f92017-10-17 11:03:26 +010010041#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010042 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010043 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10044 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
Hanno Beckerb0b2b672019-06-12 16:58:10 +010010045 mbedtls_ssl_conf_get_allow_legacy_renegotiation( ssl->conf ) ==
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010046 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10047 {
10048 /*
10049 * Accept renegotiation request
10050 */
Paul Bakker48916f92012-09-16 19:57:18 +000010051
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010052 /* DTLS clients need to know renego is server-initiated */
10053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010054 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010055 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10056 {
10057 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10058 }
10059#endif
10060 ret = ssl_start_renegotiation( ssl );
10061 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10062 ret != 0 )
10063 {
10064 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10065 return( ret );
10066 }
10067 }
10068 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010069#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010070 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010071 /*
10072 * Refuse renegotiation
10073 */
10074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077#if defined(MBEDTLS_SSL_PROTO_SSL3)
10078 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010079 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010080 /* SSLv3 does not have a "no_renegotiation" warning, so
10081 we send a fatal alert and abort the connection. */
10082 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10083 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10084 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010085 }
10086 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10088#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10089 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10090 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10093 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10094 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010095 {
10096 return( ret );
10097 }
Paul Bakker48916f92012-09-16 19:57:18 +000010098 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010099 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10101 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10104 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010105 }
Paul Bakker48916f92012-09-16 19:57:18 +000010106 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010107
Hanno Becker90333da2017-10-10 11:27:13 +010010108 /* At this point, we don't know whether the renegotiation has been
10109 * completed or not. The cases to consider are the following:
10110 * 1) The renegotiation is complete. In this case, no new record
10111 * has been read yet.
10112 * 2) The renegotiation is incomplete because the client received
10113 * an application data record while awaiting the ServerHello.
10114 * 3) The renegotiation is incomplete because the client received
10115 * a non-handshake, non-application data message while awaiting
10116 * the ServerHello.
10117 * In each of these case, looping will be the proper action:
10118 * - For 1), the next iteration will read a new record and check
10119 * if it's application data.
10120 * - For 2), the loop condition isn't satisfied as application data
10121 * is present, hence continue is the same as break
10122 * - For 3), the loop condition is satisfied and read_record
10123 * will re-deliver the message that was held back by the client
10124 * when expecting the ServerHello.
10125 */
10126 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010127 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010128#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010130 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010131 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010132 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010133 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010136 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010138 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010139 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010140 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10144 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010147 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010148 }
10149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10153 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010154 }
10155
10156 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010157
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010158 /* We're going to return something now, cancel timer,
10159 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010160 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010161 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010162
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010163#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010164 /* If we requested renego but received AppData, resend HelloRequest.
10165 * Do it now, after setting in_offt, to avoid taking this branch
10166 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010167#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010168 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010170 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010171 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010174 return( ret );
10175 }
10176 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010178#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010179 }
10180
10181 n = ( len < ssl->in_msglen )
10182 ? len : ssl->in_msglen;
10183
10184 memcpy( buf, ssl->in_offt, n );
10185 ssl->in_msglen -= n;
10186
10187 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010188 {
10189 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010191 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010192 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010194 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010195 /* more data available */
10196 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010197 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010200
Paul Bakker23986e52011-04-24 08:57:21 +000010201 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010202}
10203
10204/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010205 * Send application data to be encrypted by the SSL layer, taking care of max
10206 * fragment length and buffer size.
10207 *
10208 * According to RFC 5246 Section 6.2.1:
10209 *
10210 * Zero-length fragments of Application data MAY be sent as they are
10211 * potentially useful as a traffic analysis countermeasure.
10212 *
10213 * Therefore, it is possible that the input message length is 0 and the
10214 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010215 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010216static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010217 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010218{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010219 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10220 const size_t max_len = (size_t) ret;
10221
10222 if( ret < 0 )
10223 {
10224 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10225 return( ret );
10226 }
10227
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010228 if( len > max_len )
10229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010231 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010234 "maximum fragment length: %d > %d",
10235 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010237 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010238 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010239#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010240#if defined(MBEDTLS_SSL_PROTO_TLS)
10241 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010242 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010243 }
10244#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010245 }
Paul Bakker887bd502011-06-08 13:10:54 +000010246
Paul Bakker5121ce52009-01-03 21:22:43 +000010247 if( ssl->out_left != 0 )
10248 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010249 /*
10250 * The user has previously tried to send the data and
10251 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10252 * written. In this case, we expect the high-level write function
10253 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010255 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010258 return( ret );
10259 }
10260 }
Paul Bakker887bd502011-06-08 13:10:54 +000010261 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010262 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010263 /*
10264 * The user is trying to send a message the first time, so we need to
10265 * copy the data into the internal buffers and setup the data structure
10266 * to keep track of partial writes
10267 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010268 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010269 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010270 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010271
Hanno Becker67bc7c32018-08-06 11:33:50 +010010272 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010275 return( ret );
10276 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010277 }
10278
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010279 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010280}
10281
10282/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010283 * Write application data, doing 1/n-1 splitting if necessary.
10284 *
10285 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010286 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010287 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010290static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010291 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010292{
10293 int ret;
10294
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010295 if( ssl->conf->cbc_record_splitting ==
10296 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010297 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10299 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10300 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010301 {
10302 return( ssl_write_real( ssl, buf, len ) );
10303 }
10304
10305 if( ssl->split_done == 0 )
10306 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010307 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010308 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010309 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010310 }
10311
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010312 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10313 return( ret );
10314 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010315
10316 return( ret + 1 );
10317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010319
10320/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010321 * Write application data (public-facing wrapper)
10322 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010323int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010324{
10325 int ret;
10326
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010328
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010329 if( ssl == NULL || ssl->conf == NULL )
10330 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10331
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010332#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010333 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10334 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010335 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010336 return( ret );
10337 }
10338#endif
10339
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010340 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010341 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010342 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010343 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010345 return( ret );
10346 }
10347 }
10348
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010349#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010350 ret = ssl_write_split( ssl, buf, len );
10351#else
10352 ret = ssl_write_real( ssl, buf, len );
10353#endif
10354
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010356
10357 return( ret );
10358}
10359
10360/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010361 * Notify the peer that the connection is being closed
10362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010364{
10365 int ret;
10366
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010367 if( ssl == NULL || ssl->conf == NULL )
10368 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010371
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010372 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10378 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10379 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010382 return( ret );
10383 }
10384 }
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010387
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010388 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010389}
10390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010392{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010393 if( transform == NULL )
10394 return;
10395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010397 deflateEnd( &transform->ctx_deflate );
10398 inflateEnd( &transform->ctx_inflate );
10399#endif
10400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10402 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010403
Hanno Becker92231322018-01-03 15:32:51 +000010404#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 mbedtls_md_free( &transform->md_ctx_enc );
10406 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010407#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010408
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010409 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010410}
10411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412#if defined(MBEDTLS_X509_CRT_PARSE_C)
10413static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010416
10417 while( cur != NULL )
10418 {
10419 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010421 cur = next;
10422 }
10423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010425
Hanno Becker0271f962018-08-16 13:23:47 +010010426#if defined(MBEDTLS_SSL_PROTO_DTLS)
10427
10428static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10429{
10430 unsigned offset;
10431 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10432
10433 if( hs == NULL )
10434 return;
10435
Hanno Becker283f5ef2018-08-24 09:34:47 +010010436 ssl_free_buffered_record( ssl );
10437
Hanno Becker0271f962018-08-16 13:23:47 +010010438 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010439 ssl_buffering_free_slot( ssl, offset );
10440}
10441
10442static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10443 uint8_t slot )
10444{
10445 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10446 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010447
10448 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10449 return;
10450
Hanno Beckere605b192018-08-21 15:59:07 +010010451 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010452 {
Hanno Beckere605b192018-08-21 15:59:07 +010010453 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010454 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010455 mbedtls_free( hs_buf->data );
10456 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010457 }
10458}
10459
10460#endif /* MBEDTLS_SSL_PROTO_DTLS */
10461
Gilles Peskine9b562d52018-04-25 20:32:43 +020010462void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010463{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010464 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10465
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010466 if( handshake == NULL )
10467 return;
10468
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010469#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10470 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10471 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010472 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010473 handshake->async_in_progress = 0;
10474 }
10475#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10476
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010477#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10478 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10479 mbedtls_md5_free( &handshake->fin_md5 );
10480 mbedtls_sha1_free( &handshake->fin_sha1 );
10481#endif
10482#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10483#if defined(MBEDTLS_SHA256_C)
10484 mbedtls_sha256_free( &handshake->fin_sha256 );
10485#endif
10486#if defined(MBEDTLS_SHA512_C)
10487 mbedtls_sha512_free( &handshake->fin_sha512 );
10488#endif
10489#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491#if defined(MBEDTLS_DHM_C)
10492 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494#if defined(MBEDTLS_ECDH_C)
10495 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010496#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010497#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010498 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010499#if defined(MBEDTLS_SSL_CLI_C)
10500 mbedtls_free( handshake->ecjpake_cache );
10501 handshake->ecjpake_cache = NULL;
10502 handshake->ecjpake_cache_len = 0;
10503#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010504#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010505
Janos Follath4ae5c292016-02-10 11:27:43 +000010506#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10507 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010508 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010510#endif
10511
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010512#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10513 if( handshake->psk != NULL )
10514 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010515 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010516 mbedtls_free( handshake->psk );
10517 }
10518#endif
10519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010520#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10521 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010522 /*
10523 * Free only the linked list wrapper, not the keys themselves
10524 * since the belong to the SNI callback
10525 */
10526 if( handshake->sni_key_cert != NULL )
10527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010528 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010529
10530 while( cur != NULL )
10531 {
10532 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010534 cur = next;
10535 }
10536 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010537#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010538
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010539#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010540 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010541 if( handshake->ecrs_peer_cert != NULL )
10542 {
10543 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10544 mbedtls_free( handshake->ecrs_peer_cert );
10545 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010546#endif
10547
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010548#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10549 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10550 mbedtls_pk_free( &handshake->peer_pubkey );
10551#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010553#if defined(MBEDTLS_SSL_PROTO_DTLS)
10554 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010555 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010556 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010557#endif
10558
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010559 mbedtls_platform_zeroize( handshake,
10560 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010561}
10562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010563void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010564{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010565 if( session == NULL )
10566 return;
10567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010569 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010570#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010571
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010572#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010573 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010574#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010575
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010576 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010577}
10578
Paul Bakker5121ce52009-01-03 21:22:43 +000010579/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010580 * Serialize a full SSL context
10581 */
10582int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10583 unsigned char *buf,
10584 size_t buf_len,
10585 size_t *olen )
10586{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010587 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010588 (void) ssl;
10589
10590 if( buf != NULL )
10591 memset( buf, 0, buf_len );
10592
10593 *olen = 0;
10594
10595 return( 0 );
10596}
10597
10598/*
10599 * Deserialize a full SSL context
10600 */
10601int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10602 const unsigned char *buf,
10603 size_t len )
10604{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010605 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010606 (void) ssl;
10607 (void) buf;
10608 (void) len;
10609
10610 return( 0 );
10611}
10612
10613/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010614 * Free an SSL context
10615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010616void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010617{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010618 if( ssl == NULL )
10619 return;
10620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010622
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010623 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010624 {
Angus Grattond8213d02016-05-25 20:56:48 +100010625 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010627 }
10628
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010629 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010630 {
Angus Grattond8213d02016-05-25 20:56:48 +100010631 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010632 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010633 }
10634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010636 if( ssl->compress_buf != NULL )
10637 {
Angus Grattond8213d02016-05-25 20:56:48 +100010638 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010639 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010640 }
10641#endif
10642
Paul Bakker48916f92012-09-16 19:57:18 +000010643 if( ssl->transform )
10644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010645 mbedtls_ssl_transform_free( ssl->transform );
10646 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010647 }
10648
10649 if( ssl->handshake )
10650 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010651 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10653 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 mbedtls_free( ssl->handshake );
10656 mbedtls_free( ssl->transform_negotiate );
10657 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010658 }
10659
Paul Bakkerc0463502013-02-14 11:19:38 +010010660 if( ssl->session )
10661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010662 mbedtls_ssl_session_free( ssl->session );
10663 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010664 }
10665
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010666#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010667 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010668 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010669 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010671 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010674#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10675 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10678 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010679 }
10680#endif
10681
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010682#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010684#endif
10685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010687
Paul Bakker86f04f42013-02-14 11:20:09 +010010688 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010689 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010690}
10691
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010692/*
10693 * Initialze mbedtls_ssl_config
10694 */
10695void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10696{
10697 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010698
10699#if !defined(MBEDTLS_SSL_PROTO_TLS)
10700 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10701#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010702}
10703
Simon Butcherc97b6972015-12-27 23:48:17 +000010704#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010705static int ssl_preset_default_hashes[] = {
10706#if defined(MBEDTLS_SHA512_C)
10707 MBEDTLS_MD_SHA512,
10708 MBEDTLS_MD_SHA384,
10709#endif
10710#if defined(MBEDTLS_SHA256_C)
10711 MBEDTLS_MD_SHA256,
10712 MBEDTLS_MD_SHA224,
10713#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010714#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010715 MBEDTLS_MD_SHA1,
10716#endif
10717 MBEDTLS_MD_NONE
10718};
Simon Butcherc97b6972015-12-27 23:48:17 +000010719#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010720
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010721static int ssl_preset_suiteb_ciphersuites[] = {
10722 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10723 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10724 0
10725};
10726
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010727#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010728static int ssl_preset_suiteb_hashes[] = {
10729 MBEDTLS_MD_SHA256,
10730 MBEDTLS_MD_SHA384,
10731 MBEDTLS_MD_NONE
10732};
10733#endif
10734
10735#if defined(MBEDTLS_ECP_C)
10736static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10737 MBEDTLS_ECP_DP_SECP256R1,
10738 MBEDTLS_ECP_DP_SECP384R1,
10739 MBEDTLS_ECP_DP_NONE
10740};
10741#endif
10742
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010743/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010744 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010745 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010746int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010747 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010748{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010749#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010750 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010751#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010752
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010753 /* Use the functions here so that they are covered in tests,
10754 * but otherwise access member directly for efficiency */
10755 mbedtls_ssl_conf_endpoint( conf, endpoint );
10756 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010757
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010758 /*
10759 * Things that are common to all presets
10760 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010761#if defined(MBEDTLS_SSL_CLI_C)
10762 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10763 {
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010764#if !defined(MBEDTLS_SSL_CONF_AUTHMODE)
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010765 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Hanno Beckeracd4fc02019-06-12 16:40:50 +010010766#endif /* !MBEDTLS_SSL_CONF_AUTHMODE */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010767#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10768 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10769#endif
10770 }
10771#endif
10772
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010773#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010774 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010775#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010776
10777#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10778 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10779#endif
10780
10781#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010782#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010783 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010784#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10785#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010786 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010787 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010788#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010789#endif
10790
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010791#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10792 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10793#endif
10794
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010795#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010796 conf->f_cookie_write = ssl_cookie_write_dummy;
10797 conf->f_cookie_check = ssl_cookie_check_dummy;
10798#endif
10799
Hanno Becker7f376f42019-06-12 16:20:48 +010010800#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) && \
10801 !defined(MBEDTLS_SSL_CONF_ANTI_REPLAY)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010802 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10803#endif
10804
Janos Follath088ce432017-04-10 12:42:31 +010010805#if defined(MBEDTLS_SSL_SRV_C)
10806 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10807#endif
10808
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010809#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker1f835fa2019-06-13 10:14:59 +010010810#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010811 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
Hanno Becker1f835fa2019-06-13 10:14:59 +010010812#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MIN */
10813#if !defined(MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010814 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
Hanno Becker1f835fa2019-06-13 10:14:59 +010010815#endif /* !MBEDTLS_SSL_CONF_HS_TIMEOUT_MAX */
10816#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010817
10818#if defined(MBEDTLS_SSL_RENEGOTIATION)
10819 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010820 memset( conf->renego_period, 0x00, 2 );
10821 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010822#endif
10823
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010824#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10825 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10826 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010827 const unsigned char dhm_p[] =
10828 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10829 const unsigned char dhm_g[] =
10830 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10831
Hanno Beckera90658f2017-10-04 15:29:08 +010010832 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10833 dhm_p, sizeof( dhm_p ),
10834 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010835 {
10836 return( ret );
10837 }
10838 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010839#endif
10840
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010841 /*
10842 * Preset-specific defaults
10843 */
10844 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010845 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010846 /*
10847 * NSA Suite B
10848 */
10849 case MBEDTLS_SSL_PRESET_SUITEB:
10850 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10851 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10852 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10853 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10854
10855 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10856 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10857 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10858 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10859 ssl_preset_suiteb_ciphersuites;
10860
10861#if defined(MBEDTLS_X509_CRT_PARSE_C)
10862 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010863#endif
10864
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010865#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010866 conf->sig_hashes = ssl_preset_suiteb_hashes;
10867#endif
10868
10869#if defined(MBEDTLS_ECP_C)
10870 conf->curve_list = ssl_preset_suiteb_curves;
10871#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010872 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010873
10874 /*
10875 * Default
10876 */
10877 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010878 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10879 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10880 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10881 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10882 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10883 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10884 MBEDTLS_SSL_MIN_MINOR_VERSION :
10885 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010886 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10887 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10888
10889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010890 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010891 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10892#endif
10893
10894 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10895 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10896 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10897 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10898 mbedtls_ssl_list_ciphersuites();
10899
10900#if defined(MBEDTLS_X509_CRT_PARSE_C)
10901 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10902#endif
10903
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010904#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010905 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010906#endif
10907
10908#if defined(MBEDTLS_ECP_C)
10909 conf->curve_list = mbedtls_ecp_grp_id_list();
10910#endif
10911
10912#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10913 conf->dhm_min_bitlen = 1024;
10914#endif
10915 }
10916
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010917 return( 0 );
10918}
10919
10920/*
10921 * Free mbedtls_ssl_config
10922 */
10923void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10924{
10925#if defined(MBEDTLS_DHM_C)
10926 mbedtls_mpi_free( &conf->dhm_P );
10927 mbedtls_mpi_free( &conf->dhm_G );
10928#endif
10929
10930#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10931 if( conf->psk != NULL )
10932 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010933 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010934 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010935 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010936 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010937 }
10938
10939 if( conf->psk_identity != NULL )
10940 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010941 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010942 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010943 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010944 conf->psk_identity_len = 0;
10945 }
10946#endif
10947
10948#if defined(MBEDTLS_X509_CRT_PARSE_C)
10949 ssl_key_cert_free( conf->key_cert );
10950#endif
10951
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010952 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010953}
10954
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010955#if defined(MBEDTLS_PK_C) && \
10956 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010957/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962#if defined(MBEDTLS_RSA_C)
10963 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10964 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if defined(MBEDTLS_ECDSA_C)
10967 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10968 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010971}
10972
Hanno Becker7e5437a2017-04-28 17:15:26 +010010973unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10974{
10975 switch( type ) {
10976 case MBEDTLS_PK_RSA:
10977 return( MBEDTLS_SSL_SIG_RSA );
10978 case MBEDTLS_PK_ECDSA:
10979 case MBEDTLS_PK_ECKEY:
10980 return( MBEDTLS_SSL_SIG_ECDSA );
10981 default:
10982 return( MBEDTLS_SSL_SIG_ANON );
10983 }
10984}
10985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010987{
10988 switch( sig )
10989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990#if defined(MBEDTLS_RSA_C)
10991 case MBEDTLS_SSL_SIG_RSA:
10992 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010993#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994#if defined(MBEDTLS_ECDSA_C)
10995 case MBEDTLS_SSL_SIG_ECDSA:
10996 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010997#endif
10998 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010999 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011000 }
11001}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011002#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011003
Hanno Becker7e5437a2017-04-28 17:15:26 +010011004#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11005 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11006
11007/* Find an entry in a signature-hash set matching a given hash algorithm. */
11008mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11009 mbedtls_pk_type_t sig_alg )
11010{
11011 switch( sig_alg )
11012 {
11013 case MBEDTLS_PK_RSA:
11014 return( set->rsa );
11015 case MBEDTLS_PK_ECDSA:
11016 return( set->ecdsa );
11017 default:
11018 return( MBEDTLS_MD_NONE );
11019 }
11020}
11021
11022/* Add a signature-hash-pair to a signature-hash set */
11023void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11024 mbedtls_pk_type_t sig_alg,
11025 mbedtls_md_type_t md_alg )
11026{
11027 switch( sig_alg )
11028 {
11029 case MBEDTLS_PK_RSA:
11030 if( set->rsa == MBEDTLS_MD_NONE )
11031 set->rsa = md_alg;
11032 break;
11033
11034 case MBEDTLS_PK_ECDSA:
11035 if( set->ecdsa == MBEDTLS_MD_NONE )
11036 set->ecdsa = md_alg;
11037 break;
11038
11039 default:
11040 break;
11041 }
11042}
11043
11044/* Allow exactly one hash algorithm for each signature. */
11045void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11046 mbedtls_md_type_t md_alg )
11047{
11048 set->rsa = md_alg;
11049 set->ecdsa = md_alg;
11050}
11051
11052#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11053 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11054
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011055/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011056 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011059{
11060 switch( hash )
11061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062#if defined(MBEDTLS_MD5_C)
11063 case MBEDTLS_SSL_HASH_MD5:
11064 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011066#if defined(MBEDTLS_SHA1_C)
11067 case MBEDTLS_SSL_HASH_SHA1:
11068 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070#if defined(MBEDTLS_SHA256_C)
11071 case MBEDTLS_SSL_HASH_SHA224:
11072 return( MBEDTLS_MD_SHA224 );
11073 case MBEDTLS_SSL_HASH_SHA256:
11074 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076#if defined(MBEDTLS_SHA512_C)
11077 case MBEDTLS_SSL_HASH_SHA384:
11078 return( MBEDTLS_MD_SHA384 );
11079 case MBEDTLS_SSL_HASH_SHA512:
11080 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011081#endif
11082 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011084 }
11085}
11086
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011087/*
11088 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11089 */
11090unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11091{
11092 switch( md )
11093 {
11094#if defined(MBEDTLS_MD5_C)
11095 case MBEDTLS_MD_MD5:
11096 return( MBEDTLS_SSL_HASH_MD5 );
11097#endif
11098#if defined(MBEDTLS_SHA1_C)
11099 case MBEDTLS_MD_SHA1:
11100 return( MBEDTLS_SSL_HASH_SHA1 );
11101#endif
11102#if defined(MBEDTLS_SHA256_C)
11103 case MBEDTLS_MD_SHA224:
11104 return( MBEDTLS_SSL_HASH_SHA224 );
11105 case MBEDTLS_MD_SHA256:
11106 return( MBEDTLS_SSL_HASH_SHA256 );
11107#endif
11108#if defined(MBEDTLS_SHA512_C)
11109 case MBEDTLS_MD_SHA384:
11110 return( MBEDTLS_SSL_HASH_SHA384 );
11111 case MBEDTLS_MD_SHA512:
11112 return( MBEDTLS_SSL_HASH_SHA512 );
11113#endif
11114 default:
11115 return( MBEDTLS_SSL_HASH_NONE );
11116 }
11117}
11118
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011119#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011120/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011121 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011122 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011123 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011124int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011126 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011127
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011128 if( ssl->conf->curve_list == NULL )
11129 return( -1 );
11130
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011131 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011132 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011133 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011134
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011135 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011136}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011137#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011138
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011139#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011140/*
11141 * Check if a hash proposed by the peer is in our list.
11142 * Return 0 if we're willing to use it, -1 otherwise.
11143 */
11144int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11145 mbedtls_md_type_t md )
11146{
11147 const int *cur;
11148
11149 if( ssl->conf->sig_hashes == NULL )
11150 return( -1 );
11151
11152 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11153 if( *cur == (int) md )
11154 return( 0 );
11155
11156 return( -1 );
11157}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011158#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011160#if defined(MBEDTLS_X509_CRT_PARSE_C)
11161int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11162 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011163 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011164 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011165{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011166 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011167#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011168 int usage = 0;
11169#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011170#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011171 const char *ext_oid;
11172 size_t ext_len;
11173#endif
11174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11176 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011177 ((void) cert);
11178 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011179 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011180#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011182#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11183 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011184 {
11185 /* Server part of the key exchange */
11186 switch( ciphersuite->key_exchange )
11187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011188 case MBEDTLS_KEY_EXCHANGE_RSA:
11189 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011190 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011191 break;
11192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011193 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11194 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11195 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11196 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011197 break;
11198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011199 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11200 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011201 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011202 break;
11203
11204 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011205 case MBEDTLS_KEY_EXCHANGE_NONE:
11206 case MBEDTLS_KEY_EXCHANGE_PSK:
11207 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11208 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011209 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011210 usage = 0;
11211 }
11212 }
11213 else
11214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011215 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11216 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011217 }
11218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011219 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011220 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011221 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011222 ret = -1;
11223 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011224#else
11225 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011228#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11229 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011231 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11232 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011233 }
11234 else
11235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11237 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011238 }
11239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011240 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011241 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011242 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011243 ret = -1;
11244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011245#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011246
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011247 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011249#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011250
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011251/*
11252 * Convert version numbers to/from wire format
11253 * and, for DTLS, to/from TLS equivalent.
11254 *
11255 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011256 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011257 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11258 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011260void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011261 unsigned char ver[2] )
11262{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011263#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11264 ((void) transport);
11265#endif
11266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011267#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011268 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011270 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011271 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11272
11273 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11274 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11275 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011276 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011277#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011278#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011279 {
11280 ver[0] = (unsigned char) major;
11281 ver[1] = (unsigned char) minor;
11282 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011283#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011284}
11285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011286void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011287 const unsigned char ver[2] )
11288{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011289#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11290 ((void) transport);
11291#endif
11292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011294 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011295 {
11296 *major = 255 - ver[0] + 2;
11297 *minor = 255 - ver[1] + 1;
11298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011299 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011300 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11301 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011302 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011303#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011304#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011305 {
11306 *major = ver[0];
11307 *minor = ver[1];
11308 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011309#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011310}
11311
Simon Butcher99000142016-10-13 17:21:01 +010011312int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11313{
11314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11315 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11316 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11317
11318 switch( md )
11319 {
11320#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11321#if defined(MBEDTLS_MD5_C)
11322 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011323 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011324#endif
11325#if defined(MBEDTLS_SHA1_C)
11326 case MBEDTLS_SSL_HASH_SHA1:
11327 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11328 break;
11329#endif
11330#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11331#if defined(MBEDTLS_SHA512_C)
11332 case MBEDTLS_SSL_HASH_SHA384:
11333 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11334 break;
11335#endif
11336#if defined(MBEDTLS_SHA256_C)
11337 case MBEDTLS_SSL_HASH_SHA256:
11338 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11339 break;
11340#endif
11341 default:
11342 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11343 }
11344
11345 return 0;
11346#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11347 (void) ssl;
11348 (void) md;
11349
11350 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11351#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11352}
11353
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11355 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11356int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11357 unsigned char *output,
11358 unsigned char *data, size_t data_len )
11359{
11360 int ret = 0;
11361 mbedtls_md5_context mbedtls_md5;
11362 mbedtls_sha1_context mbedtls_sha1;
11363
11364 mbedtls_md5_init( &mbedtls_md5 );
11365 mbedtls_sha1_init( &mbedtls_sha1 );
11366
11367 /*
11368 * digitally-signed struct {
11369 * opaque md5_hash[16];
11370 * opaque sha_hash[20];
11371 * };
11372 *
11373 * md5_hash
11374 * MD5(ClientHello.random + ServerHello.random
11375 * + ServerParams);
11376 * sha_hash
11377 * SHA(ClientHello.random + ServerHello.random
11378 * + ServerParams);
11379 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011380 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011381 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011383 goto exit;
11384 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011385 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011386 ssl->handshake->randbytes, 64 ) ) != 0 )
11387 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011388 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011389 goto exit;
11390 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011391 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011392 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011393 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011394 goto exit;
11395 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011396 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011397 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011398 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011399 goto exit;
11400 }
11401
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011402 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011403 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011405 goto exit;
11406 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011407 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011408 ssl->handshake->randbytes, 64 ) ) != 0 )
11409 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011411 goto exit;
11412 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011413 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011414 data_len ) ) != 0 )
11415 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011417 goto exit;
11418 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011419 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011420 output + 16 ) ) != 0 )
11421 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011423 goto exit;
11424 }
11425
11426exit:
11427 mbedtls_md5_free( &mbedtls_md5 );
11428 mbedtls_sha1_free( &mbedtls_sha1 );
11429
11430 if( ret != 0 )
11431 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11432 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11433
11434 return( ret );
11435
11436}
11437#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11438 MBEDTLS_SSL_PROTO_TLS1_1 */
11439
11440#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11441 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11442int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011443 unsigned char *hash, size_t *hashlen,
11444 unsigned char *data, size_t data_len,
11445 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011446{
11447 int ret = 0;
11448 mbedtls_md_context_t ctx;
11449 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011450 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011451
11452 mbedtls_md_init( &ctx );
11453
11454 /*
11455 * digitally-signed struct {
11456 * opaque client_random[32];
11457 * opaque server_random[32];
11458 * ServerDHParams params;
11459 * };
11460 */
11461 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11462 {
11463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11464 goto exit;
11465 }
11466 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11467 {
11468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11469 goto exit;
11470 }
11471 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11472 {
11473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11474 goto exit;
11475 }
11476 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11477 {
11478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11479 goto exit;
11480 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011481 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011482 {
11483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11484 goto exit;
11485 }
11486
11487exit:
11488 mbedtls_md_free( &ctx );
11489
11490 if( ret != 0 )
11491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11492 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11493
11494 return( ret );
11495}
11496#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11497 MBEDTLS_SSL_PROTO_TLS1_2 */
11498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011499#endif /* MBEDTLS_SSL_TLS_C */