blob: fff20ff1b6c2bcbc19e04dc30633030bafe6261a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Hanno Beckerb5352f02019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010059static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010060
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010061/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010063{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020064#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010065 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020067
68#if defined(MBEDTLS_SSL_PROTO_DTLS)
69 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
70 return( 2 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020071 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020072#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020073#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 0 );
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +020075#endif
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010076}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Beckerf5970a02019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
112#define SSL_DONT_FORCE_FLUSH 0
113#define SSL_FORCE_FLUSH 1
114
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Hanno Beckera5a2b082019-05-15 14:03:01 +0100117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100118/* Top-level Connection ID API */
119
Hanno Beckere8eff9a2019-05-14 11:30:10 +0100120int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
121 size_t len,
122 int ignore_other_cid )
Hanno Beckereec2be92019-05-03 13:06:44 +0100123{
124 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
126
Hanno Becker791ec6b2019-05-14 11:45:26 +0100127 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
128 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
129 {
130 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
131 }
132
133 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckereec2be92019-05-03 13:06:44 +0100134 conf->cid_len = len;
135 return( 0 );
136}
137
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100138int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
139 int enable,
140 unsigned char const *own_cid,
141 size_t own_cid_len )
142{
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200143 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker78c43022019-05-03 14:38:32 +0100144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
Hanno Becker07489862019-04-25 16:01:49 +0100146 ssl->negotiate_cid = enable;
147 if( enable == MBEDTLS_SSL_CID_DISABLED )
148 {
149 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
150 return( 0 );
151 }
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckereec2be92019-05-03 13:06:44 +0100153 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Becker07489862019-04-25 16:01:49 +0100154
Hanno Beckereec2be92019-05-03 13:06:44 +0100155 if( own_cid_len != ssl->conf->cid_len )
Hanno Becker07489862019-04-25 16:01:49 +0100156 {
Hanno Beckereec2be92019-05-03 13:06:44 +0100157 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
158 (unsigned) own_cid_len,
159 (unsigned) ssl->conf->cid_len ) );
Hanno Becker07489862019-04-25 16:01:49 +0100160 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
161 }
162
163 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb4a56062019-04-30 14:07:31 +0100164 /* Truncation is not an issue here because
165 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
166 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Becker07489862019-04-25 16:01:49 +0100167
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100168 return( 0 );
169}
170
171int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
172 int *enabled,
173 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
174 size_t *peer_cid_len )
175{
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100176 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Becker2de89fa2019-04-26 17:08:02 +0100177
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +0200178 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) ||
Hanno Becker78c43022019-05-03 14:38:32 +0100179 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
180 {
Hanno Becker2de89fa2019-04-26 17:08:02 +0100181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker78c43022019-05-03 14:38:32 +0100182 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100183
Hanno Beckercb063f52019-05-03 12:54:52 +0100184 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
185 * were used, but client and server requested the empty CID.
186 * This is indistinguishable from not using the CID extension
187 * in the first place. */
Hanno Becker2de89fa2019-04-26 17:08:02 +0100188 if( ssl->transform_in->in_cid_len == 0 &&
189 ssl->transform_in->out_cid_len == 0 )
190 {
191 return( 0 );
192 }
193
Hanno Becker633d6042019-05-22 16:50:35 +0100194 if( peer_cid_len != NULL )
195 {
196 *peer_cid_len = ssl->transform_in->out_cid_len;
197 if( peer_cid != NULL )
198 {
199 memcpy( peer_cid, ssl->transform_in->out_cid,
200 ssl->transform_in->out_cid_len );
201 }
202 }
Hanno Becker2de89fa2019-04-26 17:08:02 +0100203
204 *enabled = MBEDTLS_SSL_CID_ENABLED;
205
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100206 return( 0 );
207}
Hanno Beckera5a2b082019-05-15 14:03:01 +0100208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb9e7dea2019-04-09 15:22:03 +0100209
Hanno Beckerd5847772018-08-28 10:09:23 +0100210/* Forward declarations for functions related to message buffering. */
211static void ssl_buffering_free( mbedtls_ssl_context *ssl );
212static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
213 uint8_t slot );
214static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
216static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
217static int ssl_buffer_message( mbedtls_ssl_context *ssl );
218static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100219static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100220
Hanno Beckera67dee22018-08-22 10:05:20 +0100221static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100222static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100223{
Hanno Becker11682cc2018-08-22 14:41:02 +0100224 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100225
226 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
230}
231
Hanno Becker67bc7c32018-08-06 11:33:50 +0100232static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
233{
Hanno Becker11682cc2018-08-22 14:41:02 +0100234 size_t const bytes_written = ssl->out_left;
235 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236
237 /* Double-check that the write-index hasn't gone
238 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 {
241 /* Should never happen... */
242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
243 }
244
245 return( (int) ( mtu - bytes_written ) );
246}
247
248static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
249{
250 int ret;
251 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400252 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100253
254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
255 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
256
257 if( max_len > mfl )
258 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100259
260 /* By the standard (RFC 6066 Sect. 4), the MFL extension
261 * only limits the maximum record payload size, so in theory
262 * we would be allowed to pack multiple records of payload size
263 * MFL into a single datagram. However, this would mean that there's
264 * no way to explicitly communicate MTU restrictions to the peer.
265 *
266 * The following reduction of max_len makes sure that we never
267 * write datagrams larger than MFL + Record Expansion Overhead.
268 */
269 if( max_len <= ssl->out_left )
270 return( 0 );
271
272 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100273#endif
274
275 ret = ssl_get_remaining_space_in_datagram( ssl );
276 if( ret < 0 )
277 return( ret );
278 remaining = (size_t) ret;
279
280 ret = mbedtls_ssl_get_record_expansion( ssl );
281 if( ret < 0 )
282 return( ret );
283 expansion = (size_t) ret;
284
285 if( remaining <= expansion )
286 return( 0 );
287
288 remaining -= expansion;
289 if( remaining >= max_len )
290 remaining = max_len;
291
292 return( (int) remaining );
293}
294
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295/*
296 * Double the retransmit timeout value, within the allowed range,
297 * returning -1 if the maximum value has already been reached.
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
301 uint32_t new_timeout;
302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200303 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304 return( -1 );
305
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200306 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
307 * in the following way: after the initial transmission and a first
308 * retransmission, back off to a temporary estimated MTU of 508 bytes.
309 * This value is guaranteed to be deliverable (if not guaranteed to be
310 * delivered) of any compliant IPv4 (and IPv6) network, and should work
311 * on most non-IP stacks too. */
312 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400313 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200314 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
316 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200318 new_timeout = 2 * ssl->handshake->retransmit_timeout;
319
320 /* Avoid arithmetic overflow and range overflow */
321 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200322 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200323 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200324 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200325 }
326
327 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 ssl->handshake->retransmit_timeout ) );
330
331 return( 0 );
332}
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200335{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200336 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 ssl->handshake->retransmit_timeout ) );
339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200343/*
344 * Convert max_fragment_length codes to length.
345 * RFC 6066 says:
346 * enum{
347 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
348 * } MaxFragmentLength;
349 * and we add 0 -> extension unused
350 */
Angus Grattond8213d02016-05-25 20:56:48 +1000351static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200352{
Angus Grattond8213d02016-05-25 20:56:48 +1000353 switch( mfl )
354 {
355 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
356 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
357 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
358 return 512;
359 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
360 return 1024;
361 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
362 return 2048;
363 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
364 return 4096;
365 default:
366 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
367 }
368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200370
Hanno Becker58fccf22019-02-06 14:30:46 +0000371int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
372 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerd5258fa2019-02-07 12:27:42 +0000378
379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200380 if( src->peer_cert != NULL )
381 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200382 int ret;
383
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200384 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200391 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 dst->peer_cert = NULL;
395 return( ret );
396 }
397 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100398#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000399 if( src->peer_cert_digest != NULL )
400 {
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000401 dst->peer_cert_digest =
Hanno Becker9d64b782019-02-25 10:06:59 +0000402 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000403 if( dst->peer_cert_digest == NULL )
404 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
405
406 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
407 src->peer_cert_digest_len );
408 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Becker9d64b782019-02-25 10:06:59 +0000409 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000410 }
Hanno Becker5882dd02019-06-06 16:25:57 +0100411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker9fb6e2e2019-02-05 17:00:50 +0000412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200414
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200415#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200416 if( src->ticket != NULL )
417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200418 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200419 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200421
422 memcpy( dst->ticket, src->ticket, src->ticket_len );
423 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200424#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
426 return( 0 );
427}
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
430int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200431 const unsigned char *key_enc, const unsigned char *key_dec,
432 size_t keylen,
433 const unsigned char *iv_enc, const unsigned char *iv_dec,
434 size_t ivlen,
435 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200436 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
438int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
441int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
442#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000443
Paul Bakker5121ce52009-01-03 21:22:43 +0000444/*
445 * Key material generation
446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200448static int ssl3_prf( const unsigned char *secret, size_t slen,
449 const char *label,
450 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000451 unsigned char *dstbuf, size_t dlen )
452{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100453 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200455 mbedtls_md5_context md5;
456 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 unsigned char padding[16];
458 unsigned char sha1sum[20];
459 ((void)label);
460
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200461 mbedtls_md5_init( &md5 );
462 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200463
Paul Bakker5f70b252012-09-13 14:23:06 +0000464 /*
465 * SSLv3:
466 * block =
467 * MD5( secret + SHA1( 'A' + secret + random ) ) +
468 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
469 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
470 * ...
471 */
472 for( i = 0; i < dlen / 16; i++ )
473 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200474 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000475
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100476 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100477 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100478 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100479 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100480 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100482 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100483 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100484 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100485 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000495 }
496
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200498 mbedtls_md5_free( &md5 );
499 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000500
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500501 mbedtls_platform_zeroize( padding, sizeof( padding ) );
502 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200509static int tls1_prf( const unsigned char *secret, size_t slen,
510 const char *label,
511 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000513{
Paul Bakker23986e52011-04-24 08:57:21 +0000514 size_t nb, hs;
515 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200516 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 unsigned char tmp[128];
518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528 hs = ( slen + 1 ) / 2;
529 S1 = secret;
530 S2 = secret + slen - hs;
531
532 nb = strlen( label );
533 memcpy( tmp + 20, label, nb );
534 memcpy( tmp + 20 + nb, random, rlen );
535 nb += rlen;
536
537 /*
538 * First compute P_md5(secret,label+random)[0..dlen]
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544 return( ret );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
547 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
548 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 for( i = 0; i < dlen; i += 16 )
551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_hmac_reset ( &md_ctx );
553 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
554 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_reset ( &md_ctx );
557 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
561
562 for( j = 0; j < k; j++ )
563 dstbuf[i + j] = h_i[j];
564 }
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 /*
569 * XOR out with P_sha1(secret,label+random)[0..dlen]
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100575 return( ret );
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
578 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
579 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581 for( i = 0; i < dlen; i += 20 )
582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
585 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_hmac_reset ( &md_ctx );
588 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
589 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
592
593 for( j = 0; j < k; j++ )
594 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
595 }
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100598
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500599 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
600 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602 return( 0 );
603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
607static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100608 const unsigned char *secret, size_t slen,
609 const char *label,
610 const unsigned char *random, size_t rlen,
611 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000612{
613 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100614 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000615 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
617 const mbedtls_md_info_t *md_info;
618 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100619 int ret;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100627
628 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000630
631 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100632 memcpy( tmp + md_len, label, nb );
633 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000634 nb += rlen;
635
636 /*
637 * Compute P_<hash>(secret, label + random)[0..dlen]
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
643 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
644 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100645
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100646 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_md_hmac_reset ( &md_ctx );
649 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
650 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_md_hmac_reset ( &md_ctx );
653 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
654 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000655
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100656 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000657
658 for( j = 0; j < k; j++ )
659 dstbuf[i + j] = h_i[j];
660 }
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100663
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500664 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
665 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000666
667 return( 0 );
668}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100671static int tls_prf_sha256( const unsigned char *secret, size_t slen,
672 const char *label,
673 const unsigned char *random, size_t rlen,
674 unsigned char *dstbuf, size_t dlen )
675{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100677 label, random, rlen, dstbuf, dlen ) );
678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200682static int tls_prf_sha384( const unsigned char *secret, size_t slen,
683 const char *label,
684 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000685 unsigned char *dstbuf, size_t dlen )
686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100688 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#endif /* MBEDTLS_SHA512_C */
691#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
696 defined(MBEDTLS_SSL_PROTO_TLS1_1)
697static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200698#endif
Paul Bakker380da532012-04-18 16:10:25 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200701static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200703#endif
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200706static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200708#endif
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
711#if defined(MBEDTLS_SHA256_C)
712static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200713static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200715#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_SHA512_C)
718static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200719static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200724/* Type for the TLS PRF */
725typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
726 const unsigned char *, size_t,
727 unsigned char *, size_t);
728
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200729/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200730 * Populate a transform structure with session keys and all the other
731 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200732 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200733 * Parameters:
734 * - [in/out]: transform: structure to populate
735 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200736 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200737 * - [in] ciphersuite
738 * - [in] master
739 * - [in] encrypt_then_mac
740 * - [in] trunc_hmac
741 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200742 * - [in] tls_prf: pointer to PRF to use for key derivation
743 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200744 * - [in] minor_ver: SSL/TLS minor version
745 * - [in] endpoint: client or server
746 * - [in] ssl: optionally used for:
747 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
748 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
749 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200750 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200752 int ciphersuite,
753 const unsigned char master[48],
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100754#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
756 int encrypt_then_mac,
757#endif
758#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
759 int trunc_hmac,
760#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100761#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200762#if defined(MBEDTLS_ZLIB_SUPPORT)
763 int compression,
764#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200765 ssl_tls_prf_t tls_prf,
766 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200767 int minor_ver,
768 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200769 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000770{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 unsigned char keyblk[256];
773 unsigned char *key1;
774 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100775 unsigned char *mac_enc;
776 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000777 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200778 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000779 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 const mbedtls_cipher_info_t *cipher_info;
782 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100783
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200784#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
785 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
786 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200787 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200788 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000789#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000790
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200791 /* Copy info about negotiated version and extensions */
Jaeden Amero2eaf2c72019-06-05 13:32:08 +0100792#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
793 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200794 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000795#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200796 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200798 /*
799 * Get various info structures
800 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200801 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200802 if( ciphersuite_info == NULL )
803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200805 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
807 }
808
Hanno Becker8759e162017-12-27 21:34:08 +0000809 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100810 if( cipher_info == NULL )
811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000813 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 }
816
Hanno Becker8759e162017-12-27 21:34:08 +0000817 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100818 if( md_info == NULL )
819 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000821 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100823 }
824
Hanno Beckera5a2b082019-05-15 14:03:01 +0100825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100826 /* Copy own and peer's CID if the use of the CID
827 * extension has been negotiated. */
828 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100831
Hanno Becker4932f9f2019-05-03 15:23:51 +0100832 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100833 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100834 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100835 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100836
837 transform->out_cid_len = ssl->handshake->peer_cid_len;
838 memcpy( transform->out_cid, ssl->handshake->peer_cid,
839 ssl->handshake->peer_cid_len );
840 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
841 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100842 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100843#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200846 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000847 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200848 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100852 return( ret );
853 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200856 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200857 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200858 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 /*
862 * Determine the appropriate key, IV and MAC length.
863 */
Paul Bakker68884e32013-01-07 18:20:04 +0100864
Hanno Beckere7f2df02017-12-27 08:17:40 +0000865 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200866
Hanno Beckerf1229442018-01-03 15:32:31 +0000867#if defined(MBEDTLS_GCM_C) || \
868 defined(MBEDTLS_CCM_C) || \
869 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200871 cipher_info->mode == MBEDTLS_MODE_CCM ||
872 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 {
Hanno Becker8759e162017-12-27 21:34:08 +0000874 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200875
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200876 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000877 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000878 transform->taglen =
879 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200880
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200881 /* All modes haves 96-bit IVs;
882 * GCM and CCM has 4 implicit and 8 explicit bytes
883 * ChachaPoly has all 12 bytes implicit
884 */
Paul Bakker68884e32013-01-07 18:20:04 +0100885 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200886 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
887 transform->fixed_ivlen = 12;
888 else
889 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200891 /* Minimum length of encrypted record */
892 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000893 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100894 }
895 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000896#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
897#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
898 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
899 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100900 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200901 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
903 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100907 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 mac_key_len = mbedtls_md_get_size( md_info );
911 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200914 /*
915 * If HMAC is to be truncated, we shall keep the leftmost bytes,
916 * (rfc 6066 page 13 or rfc 2104 section 4),
917 * so we only need to adjust the length here.
918 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200919 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000922
923#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
924 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000925 * HMAC implementation which also truncates the key
926 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000927 mac_key_len = transform->maclen;
928#endif
929 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200931
932 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100933 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000934
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200935 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 transform->minlen = transform->maclen;
938 else
Paul Bakker68884e32013-01-07 18:20:04 +0100939 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940 /*
941 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100942 * 1. if EtM is in use: one block plus MAC
943 * otherwise: * first multiple of blocklen greater than maclen
944 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200947 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100948 {
949 transform->minlen = transform->maclen
950 + cipher_info->block_size;
951 }
952 else
953#endif
954 {
955 transform->minlen = transform->maclen
956 + cipher_info->block_size
957 - transform->maclen % cipher_info->block_size;
958 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200961 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
962 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200963 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100964 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200965#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200967 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
968 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200969 {
970 transform->minlen += transform->ivlen;
971 }
972 else
973#endif
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200977 }
Paul Bakker68884e32013-01-07 18:20:04 +0100978 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000980 else
981#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
985 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000986
Hanno Beckere7f2df02017-12-27 08:17:40 +0000987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
988 (unsigned) keylen,
989 (unsigned) transform->minlen,
990 (unsigned) transform->ivlen,
991 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000992
993 /*
994 * Finally setup the cipher contexts, IVs and MAC secrets.
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200997 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000999 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker68884e32013-01-07 18:20:04 +01001002 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001004
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001005 /*
1006 * This is not used in TLS v1.1.
1007 */
Paul Bakker48916f92012-09-16 19:57:18 +00001008 iv_copy_len = ( transform->fixed_ivlen ) ?
1009 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001010 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1011 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001013 }
1014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_SSL_CLI_C */
1016#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001017 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001018 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001019 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker81c7b182017-11-09 18:39:33 +00001022 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001023 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001025 /*
1026 * This is not used in TLS v1.1.
1027 */
Paul Bakker48916f92012-09-16 19:57:18 +00001028 iv_copy_len = ( transform->fixed_ivlen ) ?
1029 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001030 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1031 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001034 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001039 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Hanno Becker92231322018-01-03 15:32:51 +00001041#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001043 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001044 {
Hanno Becker92231322018-01-03 15:32:51 +00001045 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001049 }
1050
Hanno Becker81c7b182017-11-09 18:39:33 +00001051 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1052 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 }
1054 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1056#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1057 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001058 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001059 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001060 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1061 For AEAD-based ciphersuites, there is nothing to do here. */
1062 if( mac_key_len != 0 )
1063 {
1064 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1065 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1066 }
Paul Bakker68884e32013-01-07 18:20:04 +01001067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068 else
1069#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1077 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001078 {
1079 int ret = 0;
1080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001082
Hanno Beckere7f2df02017-12-27 08:17:40 +00001083 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001084 transform->iv_enc, transform->iv_dec,
1085 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001086 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001087 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001091 }
1092 }
Hanno Becker92231322018-01-03 15:32:51 +00001093#else
1094 ((void) mac_dec);
1095 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001097
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1099 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001100 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001101 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001102 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001103 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001104 iv_copy_len );
1105 }
1106#endif
1107
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001112 return( ret );
1113 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001114
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 cipher_info ) ) != 0 )
1117 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001119 return( ret );
1120 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001123 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001127 return( ret );
1128 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001131 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001135 return( ret );
1136 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_CIPHER_MODE_CBC)
1139 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1142 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001145 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001146 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1149 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152 return( ret );
1153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001157 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001158
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001159 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001161 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakker48916f92012-09-16 19:57:18 +00001165 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1166 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001167
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001168 if( deflateInit( &transform->ctx_deflate,
1169 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001170 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174 }
1175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001177
Paul Bakker5121ce52009-01-03 21:22:43 +00001178 return( 0 );
1179}
1180
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001182 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001183 *
1184 * Inputs:
1185 * - SSL/TLS minor version
1186 * - hash associated with the ciphersuite (only used by TLS 1.2)
1187 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001188 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001189 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1190 */
1191static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1192 int minor_ver,
1193 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001194{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001195#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1196 (void) hash;
1197#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001199#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001201 {
1202 handshake->tls_prf = ssl3_prf;
1203 handshake->calc_verify = ssl_calc_verify_ssl;
1204 handshake->calc_finished = ssl_calc_finished_ssl;
1205 }
1206 else
1207#endif
1208#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001209 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001210 {
1211 handshake->tls_prf = tls1_prf;
1212 handshake->calc_verify = ssl_calc_verify_tls;
1213 handshake->calc_finished = ssl_calc_finished_tls;
1214 }
1215 else
1216#endif
1217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1218#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1220 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001221 {
1222 handshake->tls_prf = tls_prf_sha384;
1223 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1224 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1225 }
1226 else
1227#endif
1228#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001229 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001230 {
1231 handshake->tls_prf = tls_prf_sha256;
1232 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1233 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1234 }
1235 else
1236#endif
1237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1238 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001239 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1240 }
1241
1242 return( 0 );
1243}
1244
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001245/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001246 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001247 *
1248 * Parameters:
1249 * [in/out] handshake
1250 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1251 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252 * [out] master
1253 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001254 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001255static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001256 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001257 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001258{
1259 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260
1261#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001262 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001263 (void) ssl;
1264#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001265
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1269 return( 0 );
1270 }
1271
1272 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1273 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001274
1275#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckera49ec562019-06-11 14:47:55 +01001276 if( mbedtls_ssl_hs_get_extended_ms( handshake )
1277 == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001278 {
1279 unsigned char session_hash[48];
1280 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001281
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001282 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001283
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001284 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1285 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001286
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001288 "extended master secret",
1289 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001290 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001291 }
1292 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001293#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001294 {
1295 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1296 "master secret",
1297 handshake->randbytes, 64,
1298 master, 48 );
1299 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001300 if( ret != 0 )
1301 {
1302 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1303 return( ret );
1304 }
1305
1306 mbedtls_platform_zeroize( handshake->premaster,
1307 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001308
1309 return( 0 );
1310}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001311
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001312int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1313{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001314 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001315 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1316 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001317
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1319
1320 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001321 ret = ssl_set_handshake_prfs( ssl->handshake,
1322 ssl->minor_ver,
1323 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001324 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001325 {
1326 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001327 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001328 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001329
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001330 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001331 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001332 ssl->session_negotiate->master,
1333 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001334 if( ret != 0 )
1335 {
1336 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1337 return( ret );
1338 }
1339
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001340 /* Swap the client and server random values:
1341 * - MS derivation wanted client+server (RFC 5246 8.1)
1342 * - key derivation wants server+client (RFC 5246 6.3) */
1343 {
1344 unsigned char tmp[64];
1345 memcpy( tmp, ssl->handshake->randbytes, 64 );
1346 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1347 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1348 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1349 }
1350
1351 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001352 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001353 ssl->session_negotiate->ciphersuite,
1354 ssl->session_negotiate->master,
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001355#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001356#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1357 ssl->session_negotiate->encrypt_then_mac,
1358#endif
1359#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1360 ssl->session_negotiate->trunc_hmac,
1361#endif
Jaeden Amero2eaf2c72019-06-05 13:32:08 +01001362#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001363#if defined(MBEDTLS_ZLIB_SUPPORT)
1364 ssl->session_negotiate->compression,
1365#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001366 ssl->handshake->tls_prf,
1367 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001368 ssl->minor_ver,
1369 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001370 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001371 if( ret != 0 )
1372 {
1373 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1374 return( ret );
1375 }
1376
1377 /* We no longer need Server/ClientHello.random values */
1378 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1379 sizeof( ssl->handshake->randbytes ) );
1380
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001381 /* Allocate compression buffer */
1382#if defined(MBEDTLS_ZLIB_SUPPORT)
1383 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1384 ssl->compress_buf == NULL )
1385 {
1386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1387 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1388 if( ssl->compress_buf == NULL )
1389 {
1390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001391 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001392 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1393 }
1394 }
1395#endif
1396
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1398
1399 return( 0 );
1400}
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001403void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1404 unsigned char hash[36],
1405 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001406{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001407 mbedtls_md5_context md5;
1408 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 unsigned char pad_1[48];
1410 unsigned char pad_2[48];
1411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001414 mbedtls_md5_init( &md5 );
1415 mbedtls_sha1_init( &sha1 );
1416
1417 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1418 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Paul Bakker380da532012-04-18 16:10:25 +00001420 memset( pad_1, 0x36, 48 );
1421 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001423 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1424 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1425 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_md5_starts_ret( &md5 );
1428 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1429 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1430 mbedtls_md5_update_ret( &md5, hash, 16 );
1431 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001433 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1434 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1435 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001437 mbedtls_sha1_starts_ret( &sha1 );
1438 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1439 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1440 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1441 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001443 *hlen = 36;
1444
1445 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001447
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001448 mbedtls_md5_free( &md5 );
1449 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001450
Paul Bakker380da532012-04-18 16:10:25 +00001451 return;
1452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001456void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1457 unsigned char hash[36],
1458 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001459{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001460 mbedtls_md5_context md5;
1461 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001465 mbedtls_md5_init( &md5 );
1466 mbedtls_sha1_init( &sha1 );
1467
1468 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1469 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001471 mbedtls_md5_finish_ret( &md5, hash );
1472 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001473
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001474 *hlen = 36;
1475
1476 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001479 mbedtls_md5_free( &md5 );
1480 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001481
Paul Bakker380da532012-04-18 16:10:25 +00001482 return;
1483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1487#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001488void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1489 unsigned char hash[32],
1490 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001491{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001492 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001493
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001494 mbedtls_sha256_init( &sha256 );
1495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001497
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001498 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001499 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001501 *hlen = 32;
1502
1503 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001506 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001507
Paul Bakker380da532012-04-18 16:10:25 +00001508 return;
1509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001513void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1514 unsigned char hash[48],
1515 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001516{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001518
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001519 mbedtls_sha512_init( &sha512 );
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001522
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001523 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001524 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001525
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001526 *hlen = 48;
1527
1528 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001530
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001531 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001532
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 return;
1534}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_SHA512_C */
1536#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1539int 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 +02001540{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001541 unsigned char *p = ssl->handshake->premaster;
1542 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001543 const unsigned char *psk = ssl->conf->psk;
1544 size_t psk_len = ssl->conf->psk_len;
1545
1546 /* If the psk callback was called, use its result */
1547 if( ssl->handshake->psk != NULL )
1548 {
1549 psk = ssl->handshake->psk;
1550 psk_len = ssl->handshake->psk_len;
1551 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552
1553 /*
1554 * PMS = struct {
1555 * opaque other_secret<0..2^16-1>;
1556 * opaque psk<0..2^16-1>;
1557 * };
1558 * with "other_secret" depending on the particular key exchange
1559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1561 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001562 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001563 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001565
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001566 *(p++) = (unsigned char)( psk_len >> 8 );
1567 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001568
1569 if( end < p || (size_t)( end - p ) < psk_len )
1570 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1571
1572 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001573 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001574 }
1575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1577#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1578 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001579 {
1580 /*
1581 * other_secret already set by the ClientKeyExchange message,
1582 * and is 48 bytes long
1583 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001584 if( end - p < 2 )
1585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001587 *p++ = 0;
1588 *p++ = 48;
1589 p += 48;
1590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001597 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001598
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001599 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001601 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001602 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001605 return( ret );
1606 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001607 *(p++) = (unsigned char)( len >> 8 );
1608 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001609 p += len;
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001612 }
1613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1615#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1616 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_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é-Gonnardbd1ae242013-10-14 13:09:25 +02001619 size_t zlen;
1620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001622 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001623 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001626 return( ret );
1627 }
1628
1629 *(p++) = (unsigned char)( zlen >> 8 );
1630 *(p++) = (unsigned char)( zlen );
1631 p += zlen;
1632
Janos Follath3fbdada2018-08-15 10:26:53 +01001633 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1634 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001641 }
1642
1643 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001644 if( end - p < 2 )
1645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647 *(p++) = (unsigned char)( psk_len >> 8 );
1648 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001649
1650 if( end < p || (size_t)( end - p ) < psk_len )
1651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1652
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001653 memcpy( p, psk, psk_len );
1654 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655
1656 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1657
1658 return( 0 );
1659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001663/*
1664 * SSLv3.0 MAC functions
1665 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001666#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001667static void ssl_mac( mbedtls_md_context_t *md_ctx,
1668 const unsigned char *secret,
1669 const unsigned char *buf, size_t len,
1670 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001671 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
1673 unsigned char header[11];
1674 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001675 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1677 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001678
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001679 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001681 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001682 else
Paul Bakker68884e32013-01-07 18:20:04 +01001683 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 memcpy( header, ctr, 8 );
1686 header[ 8] = (unsigned char) type;
1687 header[ 9] = (unsigned char)( len >> 8 );
1688 header[10] = (unsigned char)( len );
1689
Paul Bakker68884e32013-01-07 18:20:04 +01001690 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 mbedtls_md_starts( md_ctx );
1692 mbedtls_md_update( md_ctx, secret, md_size );
1693 mbedtls_md_update( md_ctx, padding, padlen );
1694 mbedtls_md_update( md_ctx, header, 11 );
1695 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001696 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Paul Bakker68884e32013-01-07 18:20:04 +01001698 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_md_starts( md_ctx );
1700 mbedtls_md_update( md_ctx, secret, md_size );
1701 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001702 mbedtls_md_update( md_ctx, out, md_size );
1703 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001704}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001706
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001707/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001708 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001709#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001710 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1711 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1712 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1713/* This function makes sure every byte in the memory region is accessed
1714 * (in ascending addresses order) */
1715static void ssl_read_memory( unsigned char *p, size_t len )
1716{
1717 unsigned char acc = 0;
1718 volatile unsigned char force;
1719
1720 for( ; len != 0; p++, len-- )
1721 acc ^= *p;
1722
1723 force = acc;
1724 (void) force;
1725}
1726#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1727
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001728/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001730 */
Hanno Becker3307b532017-12-27 21:37:21 +00001731
Hanno Beckera5a2b082019-05-15 14:03:01 +01001732#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001733/* This functions transforms a DTLS plaintext fragment and a record content
1734 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001735 *
1736 * struct {
1737 * opaque content[DTLSPlaintext.length];
1738 * ContentType real_type;
1739 * uint8 zeros[length_of_padding];
1740 * } DTLSInnerPlaintext;
1741 *
1742 * Input:
1743 * - `content`: The beginning of the buffer holding the
1744 * plaintext to be wrapped.
1745 * - `*content_size`: The length of the plaintext in Bytes.
1746 * - `max_len`: The number of Bytes available starting from
1747 * `content`. This must be `>= *content_size`.
1748 * - `rec_type`: The desired record content type.
1749 *
1750 * Output:
1751 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1752 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1753 *
1754 * Returns:
1755 * - `0` on success.
1756 * - A negative error code if `max_len` didn't offer enough space
1757 * for the expansion.
1758 */
1759static int ssl_cid_build_inner_plaintext( unsigned char *content,
1760 size_t *content_size,
1761 size_t remaining,
1762 uint8_t rec_type )
1763{
1764 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001765 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1766 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1767 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001768
1769 /* Write real content type */
1770 if( remaining == 0 )
1771 return( -1 );
1772 content[ len ] = rec_type;
1773 len++;
1774 remaining--;
1775
1776 if( remaining < pad )
1777 return( -1 );
1778 memset( content + len, 0, pad );
1779 len += pad;
1780 remaining -= pad;
1781
1782 *content_size = len;
1783 return( 0 );
1784}
1785
Hanno Becker7dc25772019-05-20 15:08:01 +01001786/* This function parses a DTLSInnerPlaintext structure.
1787 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001788static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1789 size_t *content_size,
1790 uint8_t *rec_type )
1791{
1792 size_t remaining = *content_size;
1793
1794 /* Determine length of padding by skipping zeroes from the back. */
1795 do
1796 {
1797 if( remaining == 0 )
1798 return( -1 );
1799 remaining--;
1800 } while( content[ remaining ] == 0 );
1801
1802 *content_size = remaining;
1803 *rec_type = content[ remaining ];
1804
1805 return( 0 );
1806}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001807#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001808
Hanno Becker99abf512019-05-20 14:50:53 +01001809/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001810 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001811static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001812 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001813 mbedtls_record *rec )
1814{
Hanno Becker99abf512019-05-20 14:50:53 +01001815 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001816 *
1817 * additional_data = seq_num + TLSCompressed.type +
1818 * TLSCompressed.version + TLSCompressed.length;
1819 *
Hanno Becker99abf512019-05-20 14:50:53 +01001820 * For the CID extension, this is extended as follows
1821 * (quoting draft-ietf-tls-dtls-connection-id-05,
1822 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001823 *
1824 * additional_data = seq_num + DTLSPlaintext.type +
1825 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001826 * cid +
1827 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001828 * length_of_DTLSInnerPlaintext;
1829 */
1830
Hanno Becker3307b532017-12-27 21:37:21 +00001831 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1832 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001833 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001834
Hanno Beckera5a2b082019-05-15 14:03:01 +01001835#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001836 if( rec->cid_len != 0 )
1837 {
1838 memcpy( add_data + 11, rec->cid, rec->cid_len );
1839 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1840 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1841 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1842 *add_data_len = 13 + 1 + rec->cid_len;
1843 }
1844 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001845#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001846 {
1847 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1848 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1849 *add_data_len = 13;
1850 }
Hanno Becker3307b532017-12-27 21:37:21 +00001851}
1852
Hanno Becker611a83b2018-01-03 14:27:32 +00001853int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1854 mbedtls_ssl_transform *transform,
1855 mbedtls_record *rec,
1856 int (*f_rng)(void *, unsigned char *, size_t),
1857 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001860 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001861 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001862 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001863 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001864 size_t post_avail;
1865
1866 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001867#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001868 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001869 ((void) ssl);
1870#endif
1871
1872 /* The PRNG is used for dynamic IV generation that's used
1873 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1874#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1875 ( defined(MBEDTLS_AES_C) || \
1876 defined(MBEDTLS_ARIA_C) || \
1877 defined(MBEDTLS_CAMELLIA_C) ) && \
1878 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1879 ((void) f_rng);
1880 ((void) p_rng);
1881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Hanno Becker3307b532017-12-27 21:37:21 +00001885 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001886 {
Hanno Becker3307b532017-12-27 21:37:21 +00001887 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1888 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1889 }
Hanno Becker505089d2019-05-01 09:45:57 +01001890 if( rec == NULL
1891 || rec->buf == NULL
1892 || rec->buf_len < rec->data_offset
1893 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001894#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001895 || rec->cid_len != 0
1896#endif
1897 )
Hanno Becker3307b532017-12-27 21:37:21 +00001898 {
1899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001901 }
1902
Hanno Becker3307b532017-12-27 21:37:21 +00001903 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001904 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001906 data, rec->data_len );
1907
1908 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1909
1910 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1911 {
1912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1913 (unsigned) rec->data_len,
1914 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1916 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001917
Hanno Beckera5a2b082019-05-15 14:03:01 +01001918#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001919 /*
1920 * Add CID information
1921 */
1922 rec->cid_len = transform->out_cid_len;
1923 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1924 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001925
1926 if( rec->cid_len != 0 )
1927 {
1928 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001929 * Wrap plaintext into DTLSInnerPlaintext structure.
1930 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001931 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001932 * Note that this changes `rec->data_len`, and hence
1933 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001934 */
1935 if( ssl_cid_build_inner_plaintext( data,
1936 &rec->data_len,
1937 post_avail,
1938 rec->type ) != 0 )
1939 {
1940 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1941 }
1942
1943 rec->type = MBEDTLS_SSL_MSG_CID;
1944 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001945#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001946
Hanno Becker92c930f2019-04-29 17:31:37 +01001947 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1948
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001950 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001952#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 if( mode == MBEDTLS_MODE_STREAM ||
1954 ( mode == MBEDTLS_MODE_CBC
1955#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001957#endif
1958 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 {
Hanno Becker3307b532017-12-27 21:37:21 +00001960 if( post_avail < transform->maclen )
1961 {
1962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1963 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1964 }
1965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001967 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001968 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001969 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001970 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1971 data, rec->data_len, rec->ctr, rec->type, mac );
1972 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 }
1974 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1977 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001978 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001979 {
Hanno Becker992b6872017-11-09 18:57:39 +00001980 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1981
Hanno Beckere83efe62019-04-29 13:52:53 +01001982 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001983
Hanno Becker3307b532017-12-27 21:37:21 +00001984 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001985 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001986 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1987 data, rec->data_len );
1988 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1989 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1990
1991 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 }
1993 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001994#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001998 }
1999
Hanno Becker3307b532017-12-27 21:37:21 +00002000 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2001 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002002
Hanno Becker3307b532017-12-27 21:37:21 +00002003 rec->data_len += transform->maclen;
2004 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002005 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002006 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002007#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002009 /*
2010 * Encrypt
2011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2013 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002015 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002016 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002018 "including %d bytes of padding",
2019 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Hanno Becker3307b532017-12-27 21:37:21 +00002021 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2022 transform->iv_enc, transform->ivlen,
2023 data, rec->data_len,
2024 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002027 return( ret );
2028 }
2029
Hanno Becker3307b532017-12-27 21:37:21 +00002030 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2033 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 }
Paul Bakker68884e32013-01-07 18:20:04 +01002036 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002038
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002039#if defined(MBEDTLS_GCM_C) || \
2040 defined(MBEDTLS_CCM_C) || \
2041 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 mode == MBEDTLS_MODE_CCM ||
2044 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002045 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002046 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002047 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002048 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002049
Hanno Becker3307b532017-12-27 21:37:21 +00002050 /* Check that there's space for both the authentication tag
2051 * and the explicit IV before and after the record content. */
2052 if( post_avail < transform->taglen ||
2053 rec->data_offset < explicit_iv_len )
2054 {
2055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2056 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2057 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002058
Paul Bakker68884e32013-01-07 18:20:04 +01002059 /*
2060 * Generate IV
2061 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002062 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2063 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002064 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002065 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002066 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2067 explicit_iv_len );
2068 /* Prefix record content with explicit IV. */
2069 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002070 }
2071 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2072 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002073 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 unsigned char i;
2075
2076 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2077
2078 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002079 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002080 }
2081 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002082 {
2083 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002086 }
2087
Hanno Beckere83efe62019-04-29 13:52:53 +01002088 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002089
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002090 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2091 iv, transform->ivlen );
2092 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002093 data - explicit_iv_len, explicit_iv_len );
2094 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002095 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002097 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002098 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002099
Paul Bakker68884e32013-01-07 18:20:04 +01002100 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002101 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002102 */
Hanno Becker3307b532017-12-27 21:37:21 +00002103
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002104 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002105 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002106 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002107 data, rec->data_len, /* source */
2108 data, &rec->data_len, /* destination */
2109 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002112 return( ret );
2113 }
2114
Hanno Becker3307b532017-12-27 21:37:21 +00002115 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2116 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002117
Hanno Becker3307b532017-12-27 21:37:21 +00002118 rec->data_len += transform->taglen + explicit_iv_len;
2119 rec->data_offset -= explicit_iv_len;
2120 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2125#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002126 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002129 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002130 size_t padlen, i;
2131 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 /* Currently we're always using minimal padding
2134 * (up to 255 bytes would be allowed). */
2135 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2136 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 padlen = 0;
2138
Hanno Becker3307b532017-12-27 21:37:21 +00002139 /* Check there's enough space in the buffer for the padding. */
2140 if( post_avail < padlen + 1 )
2141 {
2142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2143 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2144 }
2145
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002147 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Hanno Becker3307b532017-12-27 21:37:21 +00002149 rec->data_len += padlen + 1;
2150 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002153 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002154 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2155 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002156 */
Hanno Becker3307b532017-12-27 21:37:21 +00002157 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158 {
Hanno Becker3307b532017-12-27 21:37:21 +00002159 if( f_rng == NULL )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2163 }
2164
2165 if( rec->data_offset < transform->ivlen )
2166 {
2167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2168 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2169 }
2170
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171 /*
2172 * Generate IV
2173 */
Hanno Becker3307b532017-12-27 21:37:21 +00002174 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002175 if( ret != 0 )
2176 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177
Hanno Becker3307b532017-12-27 21:37:21 +00002178 memcpy( data - transform->ivlen, transform->iv_enc,
2179 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002180
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002185 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002186 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002187 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002188
Hanno Becker3307b532017-12-27 21:37:21 +00002189 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2190 transform->iv_enc,
2191 transform->ivlen,
2192 data, rec->data_len,
2193 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002196 return( ret );
2197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002198
Hanno Becker3307b532017-12-27 21:37:21 +00002199 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002203 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002205#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002206 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002207 {
2208 /*
2209 * Save IV in SSL3 and TLS1
2210 */
Hanno Becker3307b532017-12-27 21:37:21 +00002211 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2212 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
Hanno Becker3307b532017-12-27 21:37:21 +00002214 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002215#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002216 {
2217 data -= transform->ivlen;
2218 rec->data_offset -= transform->ivlen;
2219 rec->data_len += transform->ivlen;
2220 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002223 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002224 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002225 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2226
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002227 /*
2228 * MAC(MAC_write_key, seq_num +
2229 * TLSCipherText.type +
2230 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002231 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002232 * IV + // except for TLS 1.0
2233 * ENC(content + padding + padding_length));
2234 */
Hanno Becker3307b532017-12-27 21:37:21 +00002235
2236 if( post_avail < transform->maclen)
2237 {
2238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2239 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2240 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002241
Hanno Beckere83efe62019-04-29 13:52:53 +01002242 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002245 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002246 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002247
Hanno Becker3307b532017-12-27 21:37:21 +00002248 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002249 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002250 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2251 data, rec->data_len );
2252 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2253 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002254
Hanno Becker3307b532017-12-27 21:37:21 +00002255 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002256
Hanno Becker3307b532017-12-27 21:37:21 +00002257 rec->data_len += transform->maclen;
2258 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002259 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002263 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002264#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002265 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002270
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 /* Make extra sure authentication was performed, exactly once */
2272 if( auth_done != 1 )
2273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002276 }
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
2280 return( 0 );
2281}
2282
Hanno Becker611a83b2018-01-03 14:27:32 +00002283int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2284 mbedtls_ssl_transform *transform,
2285 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002287 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002289 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002290#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002291 size_t padlen = 0, correct = 1;
2292#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002294 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002295 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002296
Hanno Becker611a83b2018-01-03 14:27:32 +00002297#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002298 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 ((void) ssl);
2300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002303 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002304 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2307 }
2308 if( rec == NULL ||
2309 rec->buf == NULL ||
2310 rec->buf_len < rec->data_offset ||
2311 rec->buf_len - rec->data_offset < rec->data_len )
2312 {
2313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002315 }
2316
Hanno Becker4c6876b2017-12-27 21:28:58 +00002317 data = rec->buf + rec->data_offset;
2318 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002319
Hanno Beckera5a2b082019-05-15 14:03:01 +01002320#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002321 /*
2322 * Match record's CID with incoming CID.
2323 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002324 if( rec->cid_len != transform->in_cid_len ||
2325 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2326 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002327 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002328 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002329#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2332 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002333 {
2334 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002335 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2336 transform->iv_dec,
2337 transform->ivlen,
2338 data, rec->data_len,
2339 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002342 return( ret );
2343 }
2344
Hanno Becker4c6876b2017-12-27 21:28:58 +00002345 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002352#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353#if defined(MBEDTLS_GCM_C) || \
2354 defined(MBEDTLS_CCM_C) || \
2355 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 mode == MBEDTLS_MODE_CCM ||
2358 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002359 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002360 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 /*
2364 * Compute and update sizes
2365 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002366 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002369 "+ taglen (%d)", rec->data_len,
2370 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002372 }
Paul Bakker68884e32013-01-07 18:20:04 +01002373
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 /*
2375 * Prepare IV
2376 */
2377 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2378 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002379 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002380 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002381 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002382
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002383 }
2384 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2385 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002386 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 unsigned char i;
2388
2389 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2390
2391 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002392 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002393 }
2394 else
2395 {
2396 /* Reminder if we ever add an AEAD mode with a different size */
2397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2398 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2399 }
2400
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data += explicit_iv_len;
2402 rec->data_offset += explicit_iv_len;
2403 rec->data_len -= explicit_iv_len + transform->taglen;
2404
Hanno Beckere83efe62019-04-29 13:52:53 +01002405 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002406 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002407 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002408
2409 memcpy( transform->iv_dec + transform->fixed_ivlen,
2410 data - explicit_iv_len, explicit_iv_len );
2411
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002412 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002413 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002414 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002415
Paul Bakker68884e32013-01-07 18:20:04 +01002416
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002417 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002418 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002420 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2421 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002422 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002423 data, rec->data_len,
2424 data, &olen,
2425 data + rec->data_len,
2426 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2431 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002432
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002433 return( ret );
2434 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002435 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436
Hanno Becker4c6876b2017-12-27 21:28:58 +00002437 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2440 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002441 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2445#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002446 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002449 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 /*
Paul Bakker45829992013-01-03 14:52:21 +01002452 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002455 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2456 {
2457 /* The ciphertext is prefixed with the CBC IV. */
2458 minlen += transform->ivlen;
2459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#endif
Paul Bakker45829992013-01-03 14:52:21 +01002461
Hanno Becker4c6876b2017-12-27 21:28:58 +00002462 /* Size considerations:
2463 *
2464 * - The CBC cipher text must not be empty and hence
2465 * at least of size transform->ivlen.
2466 *
2467 * Together with the potential IV-prefix, this explains
2468 * the first of the two checks below.
2469 *
2470 * - The record must contain a MAC, either in plain or
2471 * encrypted, depending on whether Encrypt-then-MAC
2472 * is used or not.
2473 * - If it is, the message contains the IV-prefix,
2474 * the CBC ciphertext, and the MAC.
2475 * - If it is not, the padded plaintext, and hence
2476 * the CBC ciphertext, has at least length maclen + 1
2477 * because there is at least the padding length byte.
2478 *
2479 * As the CBC ciphertext is not empty, both cases give the
2480 * lower bound minlen + maclen + 1 on the record size, which
2481 * we test for in the second check below.
2482 */
2483 if( rec->data_len < minlen + transform->ivlen ||
2484 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002487 "+ 1 ) ( + expl IV )", rec->data_len,
2488 transform->ivlen,
2489 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002491 }
2492
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493 /*
2494 * Authenticate before decrypt if enabled
2495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498 {
Hanno Becker992b6872017-11-09 18:57:39 +00002499 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002502
Hanno Becker4c6876b2017-12-27 21:28:58 +00002503 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2504 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505
Hanno Beckere83efe62019-04-29 13:52:53 +01002506 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002507
Hanno Beckere83efe62019-04-29 13:52:53 +01002508 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2509 add_data_len );
2510 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2511 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002512 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2513 data, rec->data_len );
2514 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2515 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002516
Hanno Becker4c6876b2017-12-27 21:28:58 +00002517 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2518 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002519 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002520 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521
Hanno Becker4c6876b2017-12-27 21:28:58 +00002522 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2523 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002531
2532 /*
2533 * Check length sanity
2534 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002535 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002538 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002540 }
2541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002543 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002544 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002545 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002546 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002547 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002548 /* This is safe because data_len >= minlen + maclen + 1 initially,
2549 * and at this point we have at most subtracted maclen (note that
2550 * minlen == transform->ivlen here). */
2551 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 data += transform->ivlen;
2554 rec->data_offset += transform->ivlen;
2555 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002556 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002558
Hanno Becker4c6876b2017-12-27 21:28:58 +00002559 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2560 transform->iv_dec, transform->ivlen,
2561 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002564 return( ret );
2565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002574 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002575 {
2576 /*
2577 * Save IV in SSL3 and TLS1
2578 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002579 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2580 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002582#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
Hanno Becker4c6876b2017-12-27 21:28:58 +00002584 /* Safe since data_len >= minlen + maclen + 1, so after having
2585 * subtracted at most minlen and maclen up to this point,
2586 * data_len > 0. */
2587 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002588
Hanno Becker4c6876b2017-12-27 21:28:58 +00002589 if( auth_done == 1 )
2590 {
2591 correct *= ( rec->data_len >= padlen + 1 );
2592 padlen *= ( rec->data_len >= padlen + 1 );
2593 }
2594 else
Paul Bakker45829992013-01-03 14:52:21 +01002595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002597 if( rec->data_len < transform->maclen + padlen + 1 )
2598 {
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2600 rec->data_len,
2601 transform->maclen,
2602 padlen + 1 ) );
2603 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002604#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002605
2606 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2607 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 padlen++;
2611
2612 /* Regardless of the validity of the padding,
2613 * we have data_len >= padlen here. */
2614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002618 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#if defined(MBEDTLS_SSL_DEBUG_ALL)
2621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 "should be no more than %d",
2623 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002624#endif
Paul Bakker45829992013-01-03 14:52:21 +01002625 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 }
2627 }
2628 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2630#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2631 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002632 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 /* The padding check involves a series of up to 256
2635 * consecutive memory reads at the end of the record
2636 * plaintext buffer. In order to hide the length and
2637 * validity of the padding, always perform exactly
2638 * `min(256,plaintext_len)` reads (but take into account
2639 * only the last `padlen` bytes for the padding check). */
2640 size_t pad_count = 0;
2641 size_t real_count = 0;
2642 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002643
Hanno Becker4c6876b2017-12-27 21:28:58 +00002644 /* Index of first padding byte; it has been ensured above
2645 * that the subtraction is safe. */
2646 size_t const padding_idx = rec->data_len - padlen;
2647 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2648 size_t const start_idx = rec->data_len - num_checks;
2649 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002650
Hanno Becker4c6876b2017-12-27 21:28:58 +00002651 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002652 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002653 real_count |= ( idx >= padding_idx );
2654 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002655 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002656 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002659 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002661#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002662 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002663 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2666 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002670 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002671
Hanno Becker4c6876b2017-12-27 21:28:58 +00002672 /* If the padding was found to be invalid, padlen == 0
2673 * and the subtraction is safe. If the padding was found valid,
2674 * padlen hasn't been changed and the previous assertion
2675 * data_len >= padlen still holds. */
2676 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002680 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002686#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002688 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002689#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
2691 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002692 * Authenticate if not done yet.
2693 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002695#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker992b6872017-11-09 18:57:39 +00002698 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002699
Hanno Becker4c6876b2017-12-27 21:28:58 +00002700 /* If the initial value of padlen was such that
2701 * data_len < maclen + padlen + 1, then padlen
2702 * got reset to 1, and the initial check
2703 * data_len >= minlen + maclen + 1
2704 * guarantees that at this point we still
2705 * have at least data_len >= maclen.
2706 *
2707 * If the initial value of padlen was such that
2708 * data_len >= maclen + padlen + 1, then we have
2709 * subtracted either padlen + 1 (if the padding was correct)
2710 * or 0 (if the padding was incorrect) since then,
2711 * hence data_len >= maclen in any case.
2712 */
2713 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002714
Hanno Beckere83efe62019-04-29 13:52:53 +01002715 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002717#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002718 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002720 ssl_mac( &transform->md_ctx_dec,
2721 transform->mac_dec,
2722 data, rec->data_len,
2723 rec->ctr, rec->type,
2724 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 }
2726 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2728#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2729 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002730 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002731 {
2732 /*
2733 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002734 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002735 *
2736 * Known timing attacks:
2737 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2738 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002739 * To compensate for different timings for the MAC calculation
2740 * depending on how much padding was removed (which is determined
2741 * by padlen), process extra_run more blocks through the hash
2742 * function.
2743 *
2744 * The formula in the paper is
2745 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2746 * where L1 is the size of the header plus the decrypted message
2747 * plus CBC padding and L2 is the size of the header plus the
2748 * decrypted message. This is for an underlying hash function
2749 * with 64-byte blocks.
2750 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2751 * correctly. We round down instead of up, so -56 is the correct
2752 * value for our calculations instead of -55.
2753 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002754 * Repeat the formula rather than defining a block_size variable.
2755 * This avoids requiring division by a variable at runtime
2756 * (which would be marginally less efficient and would require
2757 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002758 */
2759 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002760 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002761
2762 /*
2763 * The next two sizes are the minimum and maximum values of
2764 * in_msglen over all padlen values.
2765 *
2766 * They're independent of padlen, since we previously did
2767 * in_msglen -= padlen.
2768 *
2769 * Note that max_len + maclen is never more than the buffer
2770 * length, as we previously did in_msglen -= maclen too.
2771 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002772 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002773 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2774
Hanno Becker4c6876b2017-12-27 21:28:58 +00002775 memset( tmp, 0, sizeof( tmp ) );
2776
2777 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002779#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2780 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002781 case MBEDTLS_MD_MD5:
2782 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002783 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002784 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002785 extra_run =
2786 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2787 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002788 break;
2789#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002790#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002791 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002792 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002793 extra_run =
2794 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2795 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002796 break;
2797#endif
2798 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2801 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002802
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002803 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002804
Hanno Beckere83efe62019-04-29 13:52:53 +01002805 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2806 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002807 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2808 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002809 /* Make sure we access everything even when padlen > 0. This
2810 * makes the synchronisation requirements for just-in-time
2811 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002812 ssl_read_memory( data + rec->data_len, padlen );
2813 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002814
2815 /* Call mbedtls_md_process at least once due to cache attacks
2816 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002817 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002818 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002821
2822 /* Make sure we access all the memory that could contain the MAC,
2823 * before we check it in the next code block. This makes the
2824 * synchronisation requirements for just-in-time Prime+Probe
2825 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002826 ssl_read_memory( data + min_len,
2827 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002828 }
2829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2831 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002837#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002838 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2839 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Becker4c6876b2017-12-27 21:28:58 +00002842 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2843 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_DEBUG_ALL)
2846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002847#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002848 correct = 0;
2849 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002850 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002851 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002852
2853 /*
2854 * Finally check the correct flag
2855 */
2856 if( correct == 0 )
2857 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002858#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859
2860 /* Make extra sure authentication was performed, exactly once */
2861 if( auth_done != 1 )
2862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2864 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002866
Hanno Beckera5a2b082019-05-15 14:03:01 +01002867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002868 if( rec->cid_len != 0 )
2869 {
2870 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2871 &rec->type );
2872 if( ret != 0 )
2873 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2874 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002875#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
2879 return( 0 );
2880}
2881
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002882#undef MAC_NONE
2883#undef MAC_PLAINTEXT
2884#undef MAC_CIPHERTEXT
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887/*
2888 * Compression/decompression functions
2889 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002890static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891{
2892 int ret;
2893 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002894 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002896 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002900 if( len_pre == 0 )
2901 return( 0 );
2902
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 memcpy( msg_pre, ssl->out_msg, len_pre );
2904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002906 ssl->out_msglen ) );
2907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909 ssl->out_msg, ssl->out_msglen );
2910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2912 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2913 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002914 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915
Paul Bakker48916f92012-09-16 19:57:18 +00002916 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002917 if( ret != Z_OK )
2918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2920 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 }
2922
Angus Grattond8213d02016-05-25 20:56:48 +10002923 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002924 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927 ssl->out_msglen ) );
2928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002930 ssl->out_msg, ssl->out_msglen );
2931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002933
2934 return( 0 );
2935}
2936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938{
2939 int ret;
2940 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002941 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002942 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002943 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002946
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002947 if( len_pre == 0 )
2948 return( 0 );
2949
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 memcpy( msg_pre, ssl->in_msg, len_pre );
2951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2959 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2960 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002961 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002962 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Paul Bakker48916f92012-09-16 19:57:18 +00002964 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002965 if( ret != Z_OK )
2966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2968 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 }
2970
Angus Grattond8213d02016-05-25 20:56:48 +10002971 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002972 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after 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, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002978 ssl->in_msg, ssl->in_msglen );
2979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002981
2982 return( 0 );
2983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002984#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2987static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_PROTO_DTLS)
2990static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002991{
2992 /* If renegotiation is not enforced, retransmit until we would reach max
2993 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002994 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002995 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002996 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002997 unsigned char doublings = 1;
2998
2999 while( ratio != 0 )
3000 {
3001 ++doublings;
3002 ratio >>= 1;
3003 }
3004
3005 if( ++ssl->renego_records_seen > doublings )
3006 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003008 return( 0 );
3009 }
3010 }
3011
3012 return( ssl_write_hello_request( ssl ) );
3013}
3014#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003016
Paul Bakker5121ce52009-01-03 21:22:43 +00003017/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003018 * Fill the input message buffer by appending data to it.
3019 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003020 *
3021 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3022 * available (from this read and/or a previous one). Otherwise, an error code
3023 * is returned (possibly EOF or WANT_READ).
3024 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003025 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3026 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3027 * since we always read a whole datagram at once.
3028 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003029 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003030 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003033{
Paul Bakker23986e52011-04-24 08:57:21 +00003034 int ret;
3035 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003039 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003042 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003044 }
3045
Angus Grattond8213d02016-05-25 20:56:48 +10003046 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003050 }
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003053 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003055 uint32_t timeout;
3056
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003057 /* Just to be sure */
3058 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3059 {
3060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3061 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3062 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3063 }
3064
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003065 /*
3066 * The point is, we need to always read a full datagram at once, so we
3067 * sometimes read more then requested, and handle the additional data.
3068 * It could be the rest of the current record (while fetching the
3069 * header) and/or some other records in the same datagram.
3070 */
3071
3072 /*
3073 * Move to the next record in the already read datagram if applicable
3074 */
3075 if( ssl->next_record_offset != 0 )
3076 {
3077 if( ssl->in_left < ssl->next_record_offset )
3078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003079 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3080 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003081 }
3082
3083 ssl->in_left -= ssl->next_record_offset;
3084
3085 if( ssl->in_left != 0 )
3086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003088 ssl->next_record_offset ) );
3089 memmove( ssl->in_hdr,
3090 ssl->in_hdr + ssl->next_record_offset,
3091 ssl->in_left );
3092 }
3093
3094 ssl->next_record_offset = 0;
3095 }
3096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003099
3100 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003101 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003102 */
3103 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003106 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003107 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003108
3109 /*
3110 * A record can't be split accross datagrams. If we need to read but
3111 * are not at the beginning of a new record, the caller did something
3112 * wrong.
3113 */
3114 if( ssl->in_left != 0 )
3115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003118 }
3119
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 /*
3121 * Don't even try to read if time's out already.
3122 * This avoids by-passing the timer when repeatedly receiving messages
3123 * that will end up being dropped.
3124 */
3125 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003126 {
3127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003128 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003129 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003130 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003131 {
Angus Grattond8213d02016-05-25 20:56:48 +10003132 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003135 timeout = ssl->handshake->retransmit_timeout;
3136 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003137 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003140
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003141 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3143 timeout );
3144 else
3145 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003147 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003148
3149 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003151 }
3152
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003153 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003156 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003159 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003160 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003163 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003164 }
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003169 return( ret );
3170 }
3171
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003172 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003175 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003177 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003178 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003181 return( ret );
3182 }
3183
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003184 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003185 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003187 }
3188
Paul Bakker5121ce52009-01-03 21:22:43 +00003189 if( ret < 0 )
3190 return( ret );
3191
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003192 ssl->in_left = ret;
3193 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003194 MBEDTLS_SSL_TRANSPORT_ELSE
3195#endif /* MBEDTLS_SSL_PROTO_DTLS */
3196#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003199 ssl->in_left, nb_want ) );
3200
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003201 while( ssl->in_left < nb_want )
3202 {
3203 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003204
3205 if( ssl_check_timer( ssl ) != 0 )
3206 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3207 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003208 {
3209 if( ssl->f_recv_timeout != NULL )
3210 {
3211 ret = ssl->f_recv_timeout( ssl->p_bio,
3212 ssl->in_hdr + ssl->in_left, len,
3213 ssl->conf->read_timeout );
3214 }
3215 else
3216 {
3217 ret = ssl->f_recv( ssl->p_bio,
3218 ssl->in_hdr + ssl->in_left, len );
3219 }
3220 }
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é-Gonnard07617332015-06-24 23:00:03 +02003223 ssl->in_left, nb_want ) );
3224 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003225
3226 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003228
3229 if( ret < 0 )
3230 return( ret );
3231
mohammad160352aecb92018-03-28 23:41:40 -07003232 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003233 {
Darryl Green11999bb2018-03-13 15:22:58 +00003234 MBEDTLS_SSL_DEBUG_MSG( 1,
3235 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003236 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3238 }
3239
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003240 ssl->in_left += ret;
3241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003243#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246
3247 return( 0 );
3248}
3249
3250/*
3251 * Flush any data not yet written
3252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003254{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003255 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003256 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003259
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003260 if( ssl->f_send == NULL )
3261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003263 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003265 }
3266
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003267 /* Avoid incrementing counter if data is flushed */
3268 if( ssl->out_left == 0 )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003271 return( 0 );
3272 }
3273
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 while( ssl->out_left > 0 )
3275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003277 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003280 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003282 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 if( ret <= 0 )
3285 return( ret );
3286
mohammad160352aecb92018-03-28 23:41:40 -07003287 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003288 {
Darryl Green11999bb2018-03-13 15:22:58 +00003289 MBEDTLS_SSL_DEBUG_MSG( 1,
3290 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003291 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3293 }
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 ssl->out_left -= ret;
3296 }
3297
Hanno Becker2b1e3542018-08-06 11:19:13 +01003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003299 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003300 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003301 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003302 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003303 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003305#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003306 {
3307 ssl->out_hdr = ssl->out_buf + 8;
3308 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003309#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003310 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003313
3314 return( 0 );
3315}
3316
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003317/*
3318 * Functions to handle the DTLS retransmission state machine
3319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003321/*
3322 * Append current handshake message to current outgoing flight
3323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003325{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3328 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3329 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003330
3331 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003332 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003336 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003337 }
3338
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003339 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003340 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003343 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 }
3345
3346 /* Copy current handshake message with headers */
3347 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3348 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003349 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003350 msg->next = NULL;
3351
3352 /* Append to the current flight */
3353 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003354 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003355 else
3356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 while( cur->next != NULL )
3359 cur = cur->next;
3360 cur->next = msg;
3361 }
3362
Hanno Becker3b235902018-08-06 09:54:53 +01003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003364 return( 0 );
3365}
3366
3367/*
3368 * Free the current flight of handshake messages
3369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 mbedtls_ssl_flight_item *cur = flight;
3373 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003374
3375 while( cur != NULL )
3376 {
3377 next = cur->next;
3378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003379 mbedtls_free( cur->p );
3380 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003381
3382 cur = next;
3383 }
3384}
3385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3387static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003388#endif
3389
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003390/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003391 * Swap transform_out and out_ctr with the alternative ones
3392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003394{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003396 unsigned char tmp_out_ctr[8];
3397
3398 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 return;
3402 }
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003405
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003406 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407 tmp_transform = ssl->transform_out;
3408 ssl->transform_out = ssl->handshake->alt_transform_out;
3409 ssl->handshake->alt_transform_out = tmp_transform;
3410
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003411 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003412 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3413 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003414 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003415
3416 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003417 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3420 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3425 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003426 }
3427 }
3428#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003429}
3430
3431/*
3432 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003433 */
3434int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3435{
3436 int ret = 0;
3437
3438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3439
3440 ret = mbedtls_ssl_flight_transmit( ssl );
3441
3442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3443
3444 return( ret );
3445}
3446
3447/*
3448 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449 *
3450 * Need to remember the current message in case flush_output returns
3451 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003452 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003453 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003454int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003455{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003456 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003460 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462
3463 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003464 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003465 ssl_swap_epochs( ssl );
3466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003468 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003469
3470 while( ssl->handshake->cur_msg != NULL )
3471 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003472 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003473 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003474
Hanno Beckere1dcb032018-08-17 16:47:58 +01003475 int const is_finished =
3476 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3477 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3478
Hanno Becker04da1892018-08-14 13:22:10 +01003479 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3480 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3481
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 /* Swap epochs before sending Finished: we can't do it after
3483 * sending ChangeCipherSpec, in case write returns WANT_READ.
3484 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003485 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003486 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003488 ssl_swap_epochs( ssl );
3489 }
3490
Hanno Becker67bc7c32018-08-06 11:33:50 +01003491 ret = ssl_get_remaining_payload_in_datagram( ssl );
3492 if( ret < 0 )
3493 return( ret );
3494 max_frag_len = (size_t) ret;
3495
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003496 /* CCS is copied as is, while HS messages may need fragmentation */
3497 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3498 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003499 if( max_frag_len == 0 )
3500 {
3501 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3502 return( ret );
3503
3504 continue;
3505 }
3506
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003507 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003508 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003509 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003510
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003511 /* Update position inside current message */
3512 ssl->handshake->cur_msg_p += cur->len;
3513 }
3514 else
3515 {
3516 const unsigned char * const p = ssl->handshake->cur_msg_p;
3517 const size_t hs_len = cur->len - 12;
3518 const size_t frag_off = p - ( cur->p + 12 );
3519 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003520 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003521
Hanno Beckere1dcb032018-08-17 16:47:58 +01003522 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003523 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003524 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003525 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526
Hanno Becker67bc7c32018-08-06 11:33:50 +01003527 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3528 return( ret );
3529
3530 continue;
3531 }
3532 max_hs_frag_len = max_frag_len - 12;
3533
3534 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3535 max_hs_frag_len : rem_len;
3536
3537 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003538 {
3539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003540 (unsigned) cur_hs_frag_len,
3541 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003542 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003543
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003544 /* Messages are stored with handshake headers as if not fragmented,
3545 * copy beginning of headers then fill fragmentation fields.
3546 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3547 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003548
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003549 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3550 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3551 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3552
Hanno Becker67bc7c32018-08-06 11:33:50 +01003553 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3554 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3555 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003556
3557 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3558
Hanno Becker3f7b9732018-08-28 09:53:25 +01003559 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003560 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3561 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003562 ssl->out_msgtype = cur->type;
3563
3564 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003565 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003566 }
3567
3568 /* If done with the current message move to the next one if any */
3569 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3570 {
3571 if( cur->next != NULL )
3572 {
3573 ssl->handshake->cur_msg = cur->next;
3574 ssl->handshake->cur_msg_p = cur->next->p + 12;
3575 }
3576 else
3577 {
3578 ssl->handshake->cur_msg = NULL;
3579 ssl->handshake->cur_msg_p = NULL;
3580 }
3581 }
3582
3583 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003584 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 return( ret );
3588 }
3589 }
3590
Hanno Becker67bc7c32018-08-06 11:33:50 +01003591 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3592 return( ret );
3593
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003594 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3596 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003597 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003600 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3601 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003602
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604
3605 return( 0 );
3606}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003607
3608/*
3609 * To be called when the last message of an incoming flight is received.
3610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612{
3613 /* We won't need to resend that one any more */
3614 ssl_flight_free( ssl->handshake->flight );
3615 ssl->handshake->flight = NULL;
3616 ssl->handshake->cur_msg = NULL;
3617
3618 /* The next incoming flight will start with this msg_seq */
3619 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3620
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003621 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003622 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003623
Hanno Becker0271f962018-08-16 13:23:47 +01003624 /* Clear future message buffering structure. */
3625 ssl_buffering_free( ssl );
3626
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003627 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003628 ssl_set_timer( ssl, 0 );
3629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3631 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003634 }
3635 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003637}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003638
3639/*
3640 * To be called when the last message of an outgoing flight is send.
3641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003643{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003644 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003645 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3648 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003651 }
3652 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003656
Paul Bakker5121ce52009-01-03 21:22:43 +00003657/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003658 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660
3661/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003662 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003663 *
3664 * - fill in handshake headers
3665 * - update handshake checksum
3666 * - DTLS: save message for resending
3667 * - then pass to the record layer
3668 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003669 * DTLS: except for HelloRequest, messages are only queued, and will only be
3670 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003671 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003672 * Inputs:
3673 * - ssl->out_msglen: 4 + actual handshake message len
3674 * (4 is the size of handshake headers for TLS)
3675 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3676 * - ssl->out_msg + 4: the handshake message body
3677 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003678 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003679 * - ssl->out_msglen: the length of the record contents
3680 * (including handshake headers but excluding record headers)
3681 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003682 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003683int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003684{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003685 int ret;
3686 const size_t hs_len = ssl->out_msglen - 4;
3687 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003688
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3690
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 /*
3692 * Sanity checks
3693 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003694 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003695 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3696 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003697 /* In SSLv3, the client might send a NoCertificate alert. */
3698#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3699 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3700 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3701 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3702#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3703 {
3704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3706 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003708
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003709 /* Whenever we send anything different from a
3710 * HelloRequest we should be in a handshake - double check. */
3711 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3712 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003713 ssl->handshake == NULL )
3714 {
3715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3717 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003720 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003727#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003728
Hanno Beckerb50a2532018-08-06 11:52:54 +01003729 /* Double-check that we did not exceed the bounds
3730 * of the outgoing record buffer.
3731 * This should never fail as the various message
3732 * writing functions must obey the bounds of the
3733 * outgoing record buffer, but better be safe.
3734 *
3735 * Note: We deliberately do not check for the MTU or MFL here.
3736 */
3737 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3740 "size %u, maximum %u",
3741 (unsigned) ssl->out_msglen,
3742 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3744 }
3745
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003746 /*
3747 * Fill handshake headers
3748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003750 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003751 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3752 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3753 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003755 /*
3756 * DTLS has additional fields in the Handshake layer,
3757 * between the length field and the actual payload:
3758 * uint16 message_seq;
3759 * uint24 fragment_offset;
3760 * uint24 fragment_length;
3761 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003763 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003764 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003765 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003766 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003767 {
3768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3769 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003770 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003771 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3773 }
3774
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003775 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003776 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003777
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003778 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003779 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003780 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003781 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3782 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3783 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003784 }
3785 else
3786 {
3787 ssl->out_msg[4] = 0;
3788 ssl->out_msg[5] = 0;
3789 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003790
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003791 /* Handshake hashes are computed without fragmentation,
3792 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003793 memset( ssl->out_msg + 6, 0x00, 3 );
3794 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003795 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003796#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003797
Hanno Becker0207e532018-08-28 10:28:28 +01003798 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003799 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3800 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 }
3802
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003803 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003805 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003806 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3807 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003808 {
3809 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003812 return( ret );
3813 }
3814 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003815 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003817 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003818 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003819 {
3820 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3821 return( ret );
3822 }
3823 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003824
3825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3826
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003827 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003828}
3829
3830/*
3831 * Record layer functions
3832 */
3833
3834/*
3835 * Write current record.
3836 *
3837 * Uses:
3838 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3839 * - ssl->out_msglen: length of the record content (excl headers)
3840 * - ssl->out_msg: record content
3841 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003842int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003843{
3844 int ret, done = 0;
3845 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003846 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003847
3848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003851 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003853 {
3854 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003857 return( ret );
3858 }
3859
3860 len = ssl->out_msglen;
3861 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003862#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3865 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ret = mbedtls_ssl_hw_record_write( ssl );
3870 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3873 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003875
3876 if( ret == 0 )
3877 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003878 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003880 if( !done )
3881 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003882 unsigned i;
3883 size_t protected_record_size;
3884
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003885 /* Skip writing the record content type to after the encryption,
3886 * as it may change when using the CID extension. */
3887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003889 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003890
Hanno Becker19859472018-08-06 09:40:20 +01003891 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003892 ssl->out_len[0] = (unsigned char)( len >> 8 );
3893 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003894
Paul Bakker48916f92012-09-16 19:57:18 +00003895 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003896 {
Hanno Becker3307b532017-12-27 21:37:21 +00003897 mbedtls_record rec;
3898
3899 rec.buf = ssl->out_iv;
3900 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3901 ( ssl->out_iv - ssl->out_buf );
3902 rec.data_len = ssl->out_msglen;
3903 rec.data_offset = ssl->out_msg - rec.buf;
3904
3905 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3906 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3907 ssl->conf->transport, rec.ver );
3908 rec.type = ssl->out_msgtype;
3909
Hanno Beckera5a2b082019-05-15 14:03:01 +01003910#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003911 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003912 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003913#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003914
Hanno Becker611a83b2018-01-03 14:27:32 +00003915 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003916 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003919 return( ret );
3920 }
3921
Hanno Becker3307b532017-12-27 21:37:21 +00003922 if( rec.data_offset != 0 )
3923 {
3924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3926 }
3927
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003928 /* Update the record content type and CID. */
3929 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003930#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003931 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003932#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003933 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003934 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3935 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003936 }
3937
Hanno Becker43395762019-05-03 14:46:38 +01003938 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003939
3940#if defined(MBEDTLS_SSL_PROTO_DTLS)
3941 /* In case of DTLS, double-check that we don't exceed
3942 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003943 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003944 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003945 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003946 if( ret < 0 )
3947 return( ret );
3948
3949 if( protected_record_size > (size_t) ret )
3950 {
3951 /* Should never happen */
3952 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3953 }
3954 }
3955#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003956
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003957 /* Now write the potentially updated record content type. */
3958 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003961 "version = [%d:%d], msglen = %d",
3962 ssl->out_hdr[0], ssl->out_hdr[1],
3963 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003965 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003966 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003967
3968 ssl->out_left += protected_record_size;
3969 ssl->out_hdr += protected_record_size;
3970 ssl_update_out_pointers( ssl, ssl->transform_out );
3971
Hanno Becker04484622018-08-06 09:49:38 +01003972 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3973 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3974 break;
3975
3976 /* The loop goes to its end iff the counter is wrapping */
3977 if( i == ssl_ep_len( ssl ) )
3978 {
3979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3980 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3981 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 }
3983
Hanno Becker67bc7c32018-08-06 11:33:50 +01003984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003985 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003986 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003987 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003988 size_t remaining;
3989 ret = ssl_get_remaining_payload_in_datagram( ssl );
3990 if( ret < 0 )
3991 {
3992 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3993 ret );
3994 return( ret );
3995 }
3996
3997 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003999 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004000 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004001 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 else
4003 {
Hanno Becker513815a2018-08-20 11:56:09 +01004004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004005 }
4006 }
4007#endif /* MBEDTLS_SSL_PROTO_DTLS */
4008
4009 if( ( flush == SSL_FORCE_FLUSH ) &&
4010 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004012 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 return( ret );
4014 }
4015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004017
4018 return( 0 );
4019}
4020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004021#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004022
4023static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4024{
4025 if( ssl->in_msglen < ssl->in_hslen ||
4026 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4027 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4028 {
4029 return( 1 );
4030 }
4031 return( 0 );
4032}
Hanno Becker44650b72018-08-16 12:51:11 +01004033
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004034static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004035{
4036 return( ( ssl->in_msg[9] << 16 ) |
4037 ( ssl->in_msg[10] << 8 ) |
4038 ssl->in_msg[11] );
4039}
4040
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004041static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004042{
4043 return( ( ssl->in_msg[6] << 16 ) |
4044 ( ssl->in_msg[7] << 8 ) |
4045 ssl->in_msg[8] );
4046}
4047
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004048static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004049{
4050 uint32_t msg_len, frag_off, frag_len;
4051
4052 msg_len = ssl_get_hs_total_len( ssl );
4053 frag_off = ssl_get_hs_frag_off( ssl );
4054 frag_len = ssl_get_hs_frag_len( ssl );
4055
4056 if( frag_off > msg_len )
4057 return( -1 );
4058
4059 if( frag_len > msg_len - frag_off )
4060 return( -1 );
4061
4062 if( frag_len + 12 > ssl->in_msglen )
4063 return( -1 );
4064
4065 return( 0 );
4066}
4067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004068/*
4069 * Mark bits in bitmask (used for DTLS HS reassembly)
4070 */
4071static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4072{
4073 unsigned int start_bits, end_bits;
4074
4075 start_bits = 8 - ( offset % 8 );
4076 if( start_bits != 8 )
4077 {
4078 size_t first_byte_idx = offset / 8;
4079
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004080 /* Special case */
4081 if( len <= start_bits )
4082 {
4083 for( ; len != 0; len-- )
4084 mask[first_byte_idx] |= 1 << ( start_bits - len );
4085
4086 /* Avoid potential issues with offset or len becoming invalid */
4087 return;
4088 }
4089
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004090 offset += start_bits; /* Now offset % 8 == 0 */
4091 len -= start_bits;
4092
4093 for( ; start_bits != 0; start_bits-- )
4094 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4095 }
4096
4097 end_bits = len % 8;
4098 if( end_bits != 0 )
4099 {
4100 size_t last_byte_idx = ( offset + len ) / 8;
4101
4102 len -= end_bits; /* Now len % 8 == 0 */
4103
4104 for( ; end_bits != 0; end_bits-- )
4105 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4106 }
4107
4108 memset( mask + offset / 8, 0xFF, len / 8 );
4109}
4110
4111/*
4112 * Check that bitmask is full
4113 */
4114static int ssl_bitmask_check( unsigned char *mask, size_t len )
4115{
4116 size_t i;
4117
4118 for( i = 0; i < len / 8; i++ )
4119 if( mask[i] != 0xFF )
4120 return( -1 );
4121
4122 for( i = 0; i < len % 8; i++ )
4123 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4124 return( -1 );
4125
4126 return( 0 );
4127}
4128
Hanno Becker56e205e2018-08-16 09:06:12 +01004129/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004130static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004131 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004132{
Hanno Becker56e205e2018-08-16 09:06:12 +01004133 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004134
Hanno Becker56e205e2018-08-16 09:06:12 +01004135 alloc_len = 12; /* Handshake header */
4136 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004137
Hanno Beckerd07df862018-08-16 09:14:58 +01004138 if( add_bitmap )
4139 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004140
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004141 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004142}
Hanno Becker56e205e2018-08-16 09:06:12 +01004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004145
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004146static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004147{
4148 return( ( ssl->in_msg[1] << 16 ) |
4149 ( ssl->in_msg[2] << 8 ) |
4150 ssl->in_msg[3] );
4151}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004152
Simon Butcher99000142016-10-13 17:21:01 +01004153int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004154{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004158 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004160 }
4161
Hanno Becker12555c62018-08-16 12:47:53 +01004162 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004165 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004166 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004169 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004170 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004171 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004172 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004173
Hanno Becker44650b72018-08-16 12:51:11 +01004174 if( ssl_check_hs_header( ssl ) != 0 )
4175 {
4176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4177 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4178 }
4179
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004180 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004181 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4182 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4183 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4184 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004185 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004186 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4187 {
4188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4189 recv_msg_seq,
4190 ssl->handshake->in_msg_seq ) );
4191 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4192 }
4193
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004194 /* Retransmit only on last message from previous flight, to avoid
4195 * too many retransmissions.
4196 * Besides, No sane server ever retransmits HelloVerifyRequest */
4197 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004201 "message_seq = %d, start_of_flight = %d",
4202 recv_msg_seq,
4203 ssl->handshake->in_flight_start_seq ) );
4204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004205 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 return( ret );
4209 }
4210 }
4211 else
4212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004214 "message_seq = %d, expected = %d",
4215 recv_msg_seq,
4216 ssl->handshake->in_msg_seq ) );
4217 }
4218
Hanno Becker90333da2017-10-10 11:27:13 +01004219 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004220 }
4221 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004222
Hanno Becker6d97ef52018-08-16 13:09:04 +01004223 /* Message reassembly is handled alongside buffering of future
4224 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004225 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004226 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004227 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004230 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004231 }
4232 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004233 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004235#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004236 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004237 /* With TLS we don't handle fragmentation (for now) */
4238 if( ssl->in_msglen < ssl->in_hslen )
4239 {
4240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4241 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4242 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004243 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004244#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004245
Simon Butcher99000142016-10-13 17:21:01 +01004246 return( 0 );
4247}
4248
4249void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4250{
Hanno Becker0271f962018-08-16 13:23:47 +01004251 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004252
Hanno Becker0271f962018-08-16 13:23:47 +01004253 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004254 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004255 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004256 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004257
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004258 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004260 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004261 ssl->handshake != NULL )
4262 {
Hanno Becker0271f962018-08-16 13:23:47 +01004263 unsigned offset;
4264 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004265
Hanno Becker0271f962018-08-16 13:23:47 +01004266 /* Increment handshake sequence number */
4267 hs->in_msg_seq++;
4268
4269 /*
4270 * Clear up handshake buffering and reassembly structure.
4271 */
4272
4273 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004274 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004275
4276 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004277 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4278 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004279 offset++, hs_buf++ )
4280 {
4281 *hs_buf = *(hs_buf + 1);
4282 }
4283
4284 /* Create a fresh last entry */
4285 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004286 }
4287#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004288}
4289
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004290/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004291 * DTLS anti-replay: RFC 6347 4.1.2.6
4292 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004293 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4294 * Bit n is set iff record number in_window_top - n has been seen.
4295 *
4296 * Usually, in_window_top is the last record number seen and the lsb of
4297 * in_window is set. The only exception is the initial state (record number 0
4298 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004300#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4301static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004302{
4303 ssl->in_window_top = 0;
4304 ssl->in_window = 0;
4305}
4306
4307static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4308{
4309 return( ( (uint64_t) buf[0] << 40 ) |
4310 ( (uint64_t) buf[1] << 32 ) |
4311 ( (uint64_t) buf[2] << 24 ) |
4312 ( (uint64_t) buf[3] << 16 ) |
4313 ( (uint64_t) buf[4] << 8 ) |
4314 ( (uint64_t) buf[5] ) );
4315}
4316
4317/*
4318 * Return 0 if sequence number is acceptable, -1 otherwise
4319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004320int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004321{
4322 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4323 uint64_t bit;
4324
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004325 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004326 return( 0 );
4327
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004328 if( rec_seqnum > ssl->in_window_top )
4329 return( 0 );
4330
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004331 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004332
4333 if( bit >= 64 )
4334 return( -1 );
4335
4336 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4337 return( -1 );
4338
4339 return( 0 );
4340}
4341
4342/*
4343 * Update replay window on new validated record
4344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346{
4347 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4348
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004349 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004350 return;
4351
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004352 if( rec_seqnum > ssl->in_window_top )
4353 {
4354 /* Update window_top and the contents of the window */
4355 uint64_t shift = rec_seqnum - ssl->in_window_top;
4356
4357 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004358 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004359 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004360 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004362 ssl->in_window |= 1;
4363 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004364
4365 ssl->in_window_top = rec_seqnum;
4366 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004367 else
4368 {
4369 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004370 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371
4372 if( bit < 64 ) /* Always true, but be extra sure */
4373 ssl->in_window |= (uint64_t) 1 << bit;
4374 }
4375}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004376#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004377
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004378#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004379/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004380static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4381
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004382/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004383 * Without any SSL context, check if a datagram looks like a ClientHello with
4384 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004385 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004386 *
4387 * - if cookie is valid, return 0
4388 * - if ClientHello looks superficially valid but cookie is not,
4389 * fill obuf and set olen, then
4390 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4391 * - otherwise return a specific error code
4392 */
4393static int ssl_check_dtls_clihlo_cookie(
4394 mbedtls_ssl_cookie_write_t *f_cookie_write,
4395 mbedtls_ssl_cookie_check_t *f_cookie_check,
4396 void *p_cookie,
4397 const unsigned char *cli_id, size_t cli_id_len,
4398 const unsigned char *in, size_t in_len,
4399 unsigned char *obuf, size_t buf_len, size_t *olen )
4400{
4401 size_t sid_len, cookie_len;
4402 unsigned char *p;
4403
4404 if( f_cookie_write == NULL || f_cookie_check == NULL )
4405 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4406
4407 /*
4408 * Structure of ClientHello with record and handshake headers,
4409 * and expected values. We don't need to check a lot, more checks will be
4410 * done when actually parsing the ClientHello - skipping those checks
4411 * avoids code duplication and does not make cookie forging any easier.
4412 *
4413 * 0-0 ContentType type; copied, must be handshake
4414 * 1-2 ProtocolVersion version; copied
4415 * 3-4 uint16 epoch; copied, must be 0
4416 * 5-10 uint48 sequence_number; copied
4417 * 11-12 uint16 length; (ignored)
4418 *
4419 * 13-13 HandshakeType msg_type; (ignored)
4420 * 14-16 uint24 length; (ignored)
4421 * 17-18 uint16 message_seq; copied
4422 * 19-21 uint24 fragment_offset; copied, must be 0
4423 * 22-24 uint24 fragment_length; (ignored)
4424 *
4425 * 25-26 ProtocolVersion client_version; (ignored)
4426 * 27-58 Random random; (ignored)
4427 * 59-xx SessionID session_id; 1 byte len + sid_len content
4428 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4429 * ...
4430 *
4431 * Minimum length is 61 bytes.
4432 */
4433 if( in_len < 61 ||
4434 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4435 in[3] != 0 || in[4] != 0 ||
4436 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4437 {
4438 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4439 }
4440
4441 sid_len = in[59];
4442 if( sid_len > in_len - 61 )
4443 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4444
4445 cookie_len = in[60 + sid_len];
4446 if( cookie_len > in_len - 60 )
4447 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4448
4449 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4450 cli_id, cli_id_len ) == 0 )
4451 {
4452 /* Valid cookie */
4453 return( 0 );
4454 }
4455
4456 /*
4457 * If we get here, we've got an invalid cookie, let's prepare HVR.
4458 *
4459 * 0-0 ContentType type; copied
4460 * 1-2 ProtocolVersion version; copied
4461 * 3-4 uint16 epoch; copied
4462 * 5-10 uint48 sequence_number; copied
4463 * 11-12 uint16 length; olen - 13
4464 *
4465 * 13-13 HandshakeType msg_type; hello_verify_request
4466 * 14-16 uint24 length; olen - 25
4467 * 17-18 uint16 message_seq; copied
4468 * 19-21 uint24 fragment_offset; copied
4469 * 22-24 uint24 fragment_length; olen - 25
4470 *
4471 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4472 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4473 *
4474 * Minimum length is 28.
4475 */
4476 if( buf_len < 28 )
4477 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4478
4479 /* Copy most fields and adapt others */
4480 memcpy( obuf, in, 25 );
4481 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4482 obuf[25] = 0xfe;
4483 obuf[26] = 0xff;
4484
4485 /* Generate and write actual cookie */
4486 p = obuf + 28;
4487 if( f_cookie_write( p_cookie,
4488 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4489 {
4490 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4491 }
4492
4493 *olen = p - obuf;
4494
4495 /* Go back and fill length fields */
4496 obuf[27] = (unsigned char)( *olen - 28 );
4497
4498 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4499 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4500 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4501
4502 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4503 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4504
4505 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4506}
4507
4508/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 * Handle possible client reconnect with the same UDP quadruplet
4510 * (RFC 6347 Section 4.2.8).
4511 *
4512 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4513 * that looks like a ClientHello.
4514 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004515 * - if the input looks like a ClientHello without cookies,
4516 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004518 * - if the input looks like a ClientHello with a valid cookie,
4519 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004520 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004521 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004522 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004524 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4525 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004526 */
4527static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4528{
4529 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004530 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 ret = ssl_check_dtls_clihlo_cookie(
4533 ssl->conf->f_cookie_write,
4534 ssl->conf->f_cookie_check,
4535 ssl->conf->p_cookie,
4536 ssl->cli_id, ssl->cli_id_len,
4537 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004538 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004539
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004540 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4541
4542 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004543 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004544 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004545 * If the error is permanent we'll catch it later,
4546 * if it's not, then hopefully it'll work next time. */
4547 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4548
4549 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004550 }
4551
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004552 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004553 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004554 /* Got a valid cookie, partially reset context */
4555 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4556 {
4557 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4558 return( ret );
4559 }
4560
4561 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004562 }
4563
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004564 return( ret );
4565}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004566#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004567
Hanno Becker46483f12019-05-03 13:25:54 +01004568static int ssl_check_record_type( uint8_t record_type )
4569{
4570 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4571 record_type != MBEDTLS_SSL_MSG_ALERT &&
4572 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4573 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4574 {
4575 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4576 }
4577
4578 return( 0 );
4579}
4580
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004581/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004582 * ContentType type;
4583 * ProtocolVersion version;
4584 * uint16 epoch; // DTLS only
4585 * uint48 sequence_number; // DTLS only
4586 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004587 *
4588 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004589 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004590 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4591 *
4592 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004593 * 1. proceed with the record if this function returns 0
4594 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4595 * 3. return CLIENT_RECONNECT if this function return that value
4596 * 4. drop the whole datagram if this function returns anything else.
4597 * Point 2 is needed when the peer is resending, and we have already received
4598 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004600static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004601{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004602 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004603 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004604
Hanno Becker8b09b732019-05-08 12:03:28 +01004605 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004606
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004608 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004610 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004611#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004612 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004613 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4614 ssl->conf->cid_len != 0 )
4615 {
4616 /* Shift pointers to account for record header including CID
4617 * struct {
4618 * ContentType special_type = tls12_cid;
4619 * ProtocolVersion version;
4620 * uint16 epoch;
4621 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004622 * opaque cid[cid_length]; // Additional field compared to
4623 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004624 * uint16 length;
4625 * opaque enc_content[DTLSCiphertext.length];
4626 * } DTLSCiphertext;
4627 */
4628
4629 /* So far, we only support static CID lengths
4630 * fixed in the configuration. */
4631 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4632 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4633 }
4634 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004636 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004639
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004640#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004641 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004642 * Section 4.1.2.7, that is, send alert only with TLS */
4643 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4644 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004645 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4646 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004647 }
4648#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004651 }
4652
4653 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004654 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
4659
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004660 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4663 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004664 }
4665
Hanno Becker8b09b732019-05-08 12:03:28 +01004666 /* Now that the total length of the record header is known, ensure
4667 * that the current datagram is large enough to hold it.
4668 * This would fail, for example, if we received a datagram of
4669 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4670 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4671 if( ret != 0 )
4672 {
4673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4674 return( ret );
4675 }
4676 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4677
4678 /* Parse and validate record length
4679 * This must happen after the CID parsing because
4680 * its position in the record header depends on
4681 * the presence of a CID. */
4682
4683 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004684 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004685 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4688 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004689 }
4690
Hanno Becker8b09b732019-05-08 12:03:28 +01004691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004692 "version = [%d:%d], msglen = %d",
4693 ssl->in_msgtype,
4694 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004695
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004696 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004697 * DTLS-related tests.
4698 * Check epoch before checking length constraint because
4699 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4700 * message gets duplicated before the corresponding Finished message,
4701 * the second ChangeCipherSpec should be discarded because it belongs
4702 * to an old epoch, but not because its length is shorter than
4703 * the minimum record length for packets using the new record transform.
4704 * Note that these two kinds of failures are handled differently,
4705 * as an unexpected record is silently skipped but an invalid
4706 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004707 */
4708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004709 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004710 {
4711 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4712
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004713 /* Check epoch (and sequence number) with DTLS */
4714 if( rec_epoch != ssl->in_epoch )
4715 {
4716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4717 "expected %d, received %d",
4718 ssl->in_epoch, rec_epoch ) );
4719
4720#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4721 /*
4722 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4723 * access the first byte of record content (handshake type), as we
4724 * have an active transform (possibly iv_len != 0), so use the
4725 * fact that the record header len is 13 instead.
4726 */
4727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4728 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4729 rec_epoch == 0 &&
4730 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4731 ssl->in_left > 13 &&
4732 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4733 {
4734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4735 "from the same port" ) );
4736 return( ssl_handle_possible_reconnect( ssl ) );
4737 }
4738 else
4739#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004740 {
4741 /* Consider buffering the record. */
4742 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4743 {
4744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4745 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4746 }
4747
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004748 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004749 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004750 }
4751
4752#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4753 /* Replay detection only works for the current epoch */
4754 if( rec_epoch == ssl->in_epoch &&
4755 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4756 {
4757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4758 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4759 }
4760#endif
4761 }
4762#endif /* MBEDTLS_SSL_PROTO_DTLS */
4763
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764
4765 /* Check length against bounds of the current transform and version */
4766 if( ssl->transform_in == NULL )
4767 {
4768 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004769 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004770 {
4771 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4772 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4773 }
4774 }
4775 else
4776 {
4777 if( ssl->in_msglen < ssl->transform_in->minlen )
4778 {
4779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4780 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4781 }
4782
4783#if defined(MBEDTLS_SSL_PROTO_SSL3)
4784 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004785 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004786 {
4787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4788 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4789 }
4790#endif
4791#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4792 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4793 /*
4794 * TLS encrypted messages can have up to 256 bytes of padding
4795 */
4796 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4797 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004798 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004799 {
4800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4801 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4802 }
4803#endif
4804 }
4805
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806 return( 0 );
4807}
Paul Bakker5121ce52009-01-03 21:22:43 +00004808
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004809/*
4810 * If applicable, decrypt (and decompress) record content
4811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004813{
4814 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004817 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004819#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4820 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 ret = mbedtls_ssl_hw_record_read( ssl );
4825 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4828 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004829 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004830
4831 if( ret == 0 )
4832 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004833 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004835 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004836 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004837 mbedtls_record rec;
4838
4839 rec.buf = ssl->in_iv;
4840 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4841 - ( ssl->in_iv - ssl->in_buf );
4842 rec.data_len = ssl->in_msglen;
4843 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004844#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004845 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004846 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004847#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004848
4849 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4850 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4851 ssl->conf->transport, rec.ver );
4852 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004853 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4854 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004857
Hanno Beckera5a2b082019-05-15 14:03:01 +01004858#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4860 ssl->conf->ignore_unexpected_cid
4861 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4862 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004864 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004865 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004866#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004867
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 return( ret );
4869 }
4870
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004871 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004872 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004873 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4874 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004875 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004876
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004877 /* The record content type may change during decryption,
4878 * so re-read it. */
4879 ssl->in_msgtype = rec.type;
4880 /* Also update the input buffer, because unfortunately
4881 * the server-side ssl_parse_client_hello() reparses the
4882 * record header when receiving a ClientHello initiating
4883 * a renegotiation. */
4884 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004885 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004886 ssl->in_msglen = rec.data_len;
4887 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4888 ssl->in_len[1] = (unsigned char)( rec.data_len );
4889
Paul Bakker5121ce52009-01-03 21:22:43 +00004890 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4891 ssl->in_msg, ssl->in_msglen );
4892
Hanno Beckera5a2b082019-05-15 14:03:01 +01004893#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004894 /* We have already checked the record content type
4895 * in ssl_parse_record_header(), failing or silently
4896 * dropping the record in the case of an unknown type.
4897 *
4898 * Since with the use of CIDs, the record content type
4899 * might change during decryption, re-check the record
4900 * content type, but treat a failure as fatal this time. */
4901 if( ssl_check_record_type( ssl->in_msgtype ) )
4902 {
4903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4905 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004907
Angus Grattond8213d02016-05-25 20:56:48 +10004908 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4911 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004913 else if( ssl->in_msglen == 0 )
4914 {
4915#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4916 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4917 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4918 {
4919 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4921 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4922 }
4923#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4924
4925 ssl->nb_zero++;
4926
4927 /*
4928 * Three or more empty messages may be a DoS attack
4929 * (excessive CPU consumption).
4930 */
4931 if( ssl->nb_zero > 3 )
4932 {
4933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004934 "messages, possible DoS attack" ) );
4935 /* Treat the records as if they were not properly authenticated,
4936 * thereby failing the connection if we see more than allowed
4937 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004938 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4939 }
4940 }
4941 else
4942 ssl->nb_zero = 0;
4943
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004944 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4945#if defined(MBEDTLS_SSL_PROTO_TLS)
4946 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004947 {
4948 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004949 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004950 if( ++ssl->in_ctr[i - 1] != 0 )
4951 break;
4952
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004953 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004954 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004955 {
4956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4957 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4958 }
4959 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004960#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004961
Paul Bakker5121ce52009-01-03 21:22:43 +00004962 }
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004965 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004966 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004967 {
4968 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004971 return( ret );
4972 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004977 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004980 }
4981#endif
4982
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004983 return( 0 );
4984}
4985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004986static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004987
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004988/*
4989 * Read a record.
4990 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004991 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4992 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4993 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004994 */
Hanno Becker1097b342018-08-15 14:09:41 +01004995
4996/* Helper functions for mbedtls_ssl_read_record(). */
4997static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004998static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4999static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005000
Hanno Becker327c93b2018-08-15 13:56:18 +01005001int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005002 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005003{
5004 int ret;
5005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005007
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005008 if( ssl->keep_current_message == 0 )
5009 {
5010 do {
Simon Butcher99000142016-10-13 17:21:01 +01005011
Hanno Becker26994592018-08-15 14:14:59 +01005012 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005013 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005014 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005015
Hanno Beckere74d5562018-08-15 14:26:08 +01005016 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005017 {
Hanno Becker40f50842018-08-15 14:48:01 +01005018#if defined(MBEDTLS_SSL_PROTO_DTLS)
5019 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005020
Hanno Becker40f50842018-08-15 14:48:01 +01005021 /* We only check for buffered messages if the
5022 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005023 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005024 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005025 {
Hanno Becker40f50842018-08-15 14:48:01 +01005026 if( ssl_load_buffered_message( ssl ) == 0 )
5027 have_buffered = 1;
5028 }
5029
5030 if( have_buffered == 0 )
5031#endif /* MBEDTLS_SSL_PROTO_DTLS */
5032 {
5033 ret = ssl_get_next_record( ssl );
5034 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5035 continue;
5036
5037 if( ret != 0 )
5038 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005039 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005040 return( ret );
5041 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005042 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005043 }
5044
5045 ret = mbedtls_ssl_handle_message_type( ssl );
5046
Hanno Becker40f50842018-08-15 14:48:01 +01005047#if defined(MBEDTLS_SSL_PROTO_DTLS)
5048 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5049 {
5050 /* Buffer future message */
5051 ret = ssl_buffer_message( ssl );
5052 if( ret != 0 )
5053 return( ret );
5054
5055 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5056 }
5057#endif /* MBEDTLS_SSL_PROTO_DTLS */
5058
Hanno Becker90333da2017-10-10 11:27:13 +01005059 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5060 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005061
5062 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005063 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005064 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005065 return( ret );
5066 }
5067
Hanno Becker327c93b2018-08-15 13:56:18 +01005068 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005069 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005070 {
5071 mbedtls_ssl_update_handshake_status( ssl );
5072 }
Simon Butcher99000142016-10-13 17:21:01 +01005073 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005074 else
Simon Butcher99000142016-10-13 17:21:01 +01005075 {
Hanno Becker02f59072018-08-15 14:00:24 +01005076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005077 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005078 }
5079
5080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5081
5082 return( 0 );
5083}
5084
Hanno Becker40f50842018-08-15 14:48:01 +01005085#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005086static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005087{
Hanno Becker40f50842018-08-15 14:48:01 +01005088 if( ssl->in_left > ssl->next_record_offset )
5089 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005090
Hanno Becker40f50842018-08-15 14:48:01 +01005091 return( 0 );
5092}
5093
5094static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5095{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005096 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005097 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005098 int ret = 0;
5099
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005100 if( hs == NULL )
5101 return( -1 );
5102
Hanno Beckere00ae372018-08-20 09:39:42 +01005103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5104
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5106 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5107 {
5108 /* Check if we have seen a ChangeCipherSpec before.
5109 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005110 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005111 {
5112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5113 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005114 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005115 }
5116
Hanno Becker39b8bc92018-08-28 17:17:13 +01005117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005118 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5119 ssl->in_msglen = 1;
5120 ssl->in_msg[0] = 1;
5121
5122 /* As long as they are equal, the exact value doesn't matter. */
5123 ssl->in_left = 0;
5124 ssl->next_record_offset = 0;
5125
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005126 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005127 goto exit;
5128 }
Hanno Becker37f95322018-08-16 13:55:32 +01005129
Hanno Beckerb8f50142018-08-28 10:01:34 +01005130#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005131 /* Debug only */
5132 {
5133 unsigned offset;
5134 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5135 {
5136 hs_buf = &hs->buffering.hs[offset];
5137 if( hs_buf->is_valid == 1 )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5140 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005141 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005142 }
5143 }
5144 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005145#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005146
5147 /* Check if we have buffered and/or fully reassembled the
5148 * next handshake message. */
5149 hs_buf = &hs->buffering.hs[0];
5150 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5151 {
5152 /* Synthesize a record containing the buffered HS message. */
5153 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5154 ( hs_buf->data[2] << 8 ) |
5155 hs_buf->data[3];
5156
5157 /* Double-check that we haven't accidentally buffered
5158 * a message that doesn't fit into the input buffer. */
5159 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5160 {
5161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5163 }
5164
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5166 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5167 hs_buf->data, msg_len + 12 );
5168
5169 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5170 ssl->in_hslen = msg_len + 12;
5171 ssl->in_msglen = msg_len + 12;
5172 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5173
5174 ret = 0;
5175 goto exit;
5176 }
5177 else
5178 {
5179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5180 hs->in_msg_seq ) );
5181 }
5182
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005183 ret = -1;
5184
5185exit:
5186
5187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5188 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005189}
5190
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5192 size_t desired )
5193{
5194 int offset;
5195 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5197 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005198
Hanno Becker01315ea2018-08-21 17:22:17 +01005199 /* Get rid of future records epoch first, if such exist. */
5200 ssl_free_buffered_record( ssl );
5201
5202 /* Check if we have enough space available now. */
5203 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5204 hs->buffering.total_bytes_buffered ) )
5205 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005207 return( 0 );
5208 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005209
Hanno Becker4f432ad2018-08-28 10:02:32 +01005210 /* We don't have enough space to buffer the next expected handshake
5211 * message. Remove buffers used for future messages to gain space,
5212 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005213 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5214 offset >= 0; offset-- )
5215 {
5216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5217 offset ) );
5218
Hanno Beckerb309b922018-08-23 13:18:05 +01005219 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005220
5221 /* Check if we have enough space available now. */
5222 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5223 hs->buffering.total_bytes_buffered ) )
5224 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005226 return( 0 );
5227 }
5228 }
5229
5230 return( -1 );
5231}
5232
Hanno Becker40f50842018-08-15 14:48:01 +01005233static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5234{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005235 int ret = 0;
5236 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5237
5238 if( hs == NULL )
5239 return( 0 );
5240
5241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5242
5243 switch( ssl->in_msgtype )
5244 {
5245 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005247
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005248 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005249 break;
5250
5251 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005252 {
5253 unsigned recv_msg_seq_offset;
5254 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5255 mbedtls_ssl_hs_buffer *hs_buf;
5256 size_t msg_len = ssl->in_hslen - 12;
5257
5258 /* We should never receive an old handshake
5259 * message - double-check nonetheless. */
5260 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5261 {
5262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5264 }
5265
5266 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5267 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5268 {
5269 /* Silently ignore -- message too far in the future */
5270 MBEDTLS_SSL_DEBUG_MSG( 2,
5271 ( "Ignore future HS message with sequence number %u, "
5272 "buffering window %u - %u",
5273 recv_msg_seq, ssl->handshake->in_msg_seq,
5274 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5275
5276 goto exit;
5277 }
5278
5279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5280 recv_msg_seq, recv_msg_seq_offset ) );
5281
5282 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5283
5284 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005285 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005286 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005287 size_t reassembly_buf_sz;
5288
Hanno Becker37f95322018-08-16 13:55:32 +01005289 hs_buf->is_fragmented =
5290 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5291
5292 /* We copy the message back into the input buffer
5293 * after reassembly, so check that it's not too large.
5294 * This is an implementation-specific limitation
5295 * and not one from the standard, hence it is not
5296 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005297 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005298 {
5299 /* Ignore message */
5300 goto exit;
5301 }
5302
Hanno Beckere0b150f2018-08-21 15:51:03 +01005303 /* Check if we have enough space to buffer the message. */
5304 if( hs->buffering.total_bytes_buffered >
5305 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5306 {
5307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5309 }
5310
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005311 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5312 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005313
5314 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5315 hs->buffering.total_bytes_buffered ) )
5316 {
5317 if( recv_msg_seq_offset > 0 )
5318 {
5319 /* If we can't buffer a future message because
5320 * of space limitations -- ignore. */
5321 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",
5322 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5323 (unsigned) hs->buffering.total_bytes_buffered ) );
5324 goto exit;
5325 }
Hanno Beckere1801392018-08-21 16:51:05 +01005326 else
5327 {
5328 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",
5329 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5330 (unsigned) hs->buffering.total_bytes_buffered ) );
5331 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332
Hanno Beckera02b0b42018-08-21 17:20:27 +01005333 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005334 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005335 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",
5336 (unsigned) msg_len,
5337 (unsigned) reassembly_buf_sz,
5338 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005339 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005340 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5341 goto exit;
5342 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005343 }
5344
5345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5346 msg_len ) );
5347
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005348 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5349 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005350 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005351 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005352 goto exit;
5353 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005354 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005355
5356 /* Prepare final header: copy msg_type, length and message_seq,
5357 * then add standardised fragment_offset and fragment_length */
5358 memcpy( hs_buf->data, ssl->in_msg, 6 );
5359 memset( hs_buf->data + 6, 0, 3 );
5360 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5361
5362 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005363
5364 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005365 }
5366 else
5367 {
5368 /* Make sure msg_type and length are consistent */
5369 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5370 {
5371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5372 /* Ignore */
5373 goto exit;
5374 }
5375 }
5376
Hanno Becker4422bbb2018-08-20 09:40:19 +01005377 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005378 {
5379 size_t frag_len, frag_off;
5380 unsigned char * const msg = hs_buf->data + 12;
5381
5382 /*
5383 * Check and copy current fragment
5384 */
5385
5386 /* Validation of header fields already done in
5387 * mbedtls_ssl_prepare_handshake_record(). */
5388 frag_off = ssl_get_hs_frag_off( ssl );
5389 frag_len = ssl_get_hs_frag_len( ssl );
5390
5391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5392 frag_off, frag_len ) );
5393 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5394
5395 if( hs_buf->is_fragmented )
5396 {
5397 unsigned char * const bitmask = msg + msg_len;
5398 ssl_bitmask_set( bitmask, frag_off, frag_len );
5399 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5400 msg_len ) == 0 );
5401 }
5402 else
5403 {
5404 hs_buf->is_complete = 1;
5405 }
5406
5407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5408 hs_buf->is_complete ? "" : "not yet " ) );
5409 }
5410
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005411 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005412 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005413
5414 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005415 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005416 break;
5417 }
5418
5419exit:
5420
5421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5422 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005423}
5424#endif /* MBEDTLS_SSL_PROTO_DTLS */
5425
Hanno Becker1097b342018-08-15 14:09:41 +01005426static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005427{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005428 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005429 * Consume last content-layer message and potentially
5430 * update in_msglen which keeps track of the contents'
5431 * consumption state.
5432 *
5433 * (1) Handshake messages:
5434 * Remove last handshake message, move content
5435 * and adapt in_msglen.
5436 *
5437 * (2) Alert messages:
5438 * Consume whole record content, in_msglen = 0.
5439 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005440 * (3) Change cipher spec:
5441 * Consume whole record content, in_msglen = 0.
5442 *
5443 * (4) Application data:
5444 * Don't do anything - the record layer provides
5445 * the application data as a stream transport
5446 * and consumes through mbedtls_ssl_read only.
5447 *
5448 */
5449
5450 /* Case (1): Handshake messages */
5451 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005452 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005453 /* Hard assertion to be sure that no application data
5454 * is in flight, as corrupting ssl->in_msglen during
5455 * ssl->in_offt != NULL is fatal. */
5456 if( ssl->in_offt != NULL )
5457 {
5458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5460 }
5461
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005462 /*
5463 * Get next Handshake message in the current record
5464 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005465
Hanno Becker4a810fb2017-05-24 16:27:30 +01005466 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005467 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005468 * current handshake content: If DTLS handshake
5469 * fragmentation is used, that's the fragment
5470 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005471 * size here is faulty and should be changed at
5472 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005473 * (2) While it doesn't seem to cause problems, one
5474 * has to be very careful not to assume that in_hslen
5475 * is always <= in_msglen in a sensible communication.
5476 * Again, it's wrong for DTLS handshake fragmentation.
5477 * The following check is therefore mandatory, and
5478 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005479 * Additionally, ssl->in_hslen might be arbitrarily out of
5480 * bounds after handling a DTLS message with an unexpected
5481 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005482 */
5483 if( ssl->in_hslen < ssl->in_msglen )
5484 {
5485 ssl->in_msglen -= ssl->in_hslen;
5486 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5487 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005488
Hanno Becker4a810fb2017-05-24 16:27:30 +01005489 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5490 ssl->in_msg, ssl->in_msglen );
5491 }
5492 else
5493 {
5494 ssl->in_msglen = 0;
5495 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005496
Hanno Becker4a810fb2017-05-24 16:27:30 +01005497 ssl->in_hslen = 0;
5498 }
5499 /* Case (4): Application data */
5500 else if( ssl->in_offt != NULL )
5501 {
5502 return( 0 );
5503 }
5504 /* Everything else (CCS & Alerts) */
5505 else
5506 {
5507 ssl->in_msglen = 0;
5508 }
5509
Hanno Becker1097b342018-08-15 14:09:41 +01005510 return( 0 );
5511}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005512
Hanno Beckere74d5562018-08-15 14:26:08 +01005513static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5514{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005515 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005516 return( 1 );
5517
5518 return( 0 );
5519}
5520
Hanno Becker5f066e72018-08-16 14:56:31 +01005521#if defined(MBEDTLS_SSL_PROTO_DTLS)
5522
5523static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5524{
5525 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5526 if( hs == NULL )
5527 return;
5528
Hanno Becker01315ea2018-08-21 17:22:17 +01005529 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005530 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005531 hs->buffering.total_bytes_buffered -=
5532 hs->buffering.future_record.len;
5533
5534 mbedtls_free( hs->buffering.future_record.data );
5535 hs->buffering.future_record.data = NULL;
5536 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005537}
5538
5539static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5540{
5541 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5542 unsigned char * rec;
5543 size_t rec_len;
5544 unsigned rec_epoch;
5545
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005546 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005547 return( 0 );
5548
5549 if( hs == NULL )
5550 return( 0 );
5551
Hanno Becker5f066e72018-08-16 14:56:31 +01005552 rec = hs->buffering.future_record.data;
5553 rec_len = hs->buffering.future_record.len;
5554 rec_epoch = hs->buffering.future_record.epoch;
5555
5556 if( rec == NULL )
5557 return( 0 );
5558
Hanno Becker4cb782d2018-08-20 11:19:05 +01005559 /* Only consider loading future records if the
5560 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005561 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005562 return( 0 );
5563
Hanno Becker5f066e72018-08-16 14:56:31 +01005564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5565
5566 if( rec_epoch != ssl->in_epoch )
5567 {
5568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5569 goto exit;
5570 }
5571
5572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5573
5574 /* Double-check that the record is not too large */
5575 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5576 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5577 {
5578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5580 }
5581
5582 memcpy( ssl->in_hdr, rec, rec_len );
5583 ssl->in_left = rec_len;
5584 ssl->next_record_offset = 0;
5585
5586 ssl_free_buffered_record( ssl );
5587
5588exit:
5589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5590 return( 0 );
5591}
5592
5593static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5594{
5595 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5596 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005597 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005598
5599 /* Don't buffer future records outside handshakes. */
5600 if( hs == NULL )
5601 return( 0 );
5602
5603 /* Only buffer handshake records (we are only interested
5604 * in Finished messages). */
5605 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5606 return( 0 );
5607
5608 /* Don't buffer more than one future epoch record. */
5609 if( hs->buffering.future_record.data != NULL )
5610 return( 0 );
5611
Hanno Becker01315ea2018-08-21 17:22:17 +01005612 /* Don't buffer record if there's not enough buffering space remaining. */
5613 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5614 hs->buffering.total_bytes_buffered ) )
5615 {
5616 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",
5617 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5618 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005619 return( 0 );
5620 }
5621
Hanno Becker5f066e72018-08-16 14:56:31 +01005622 /* Buffer record */
5623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5624 ssl->in_epoch + 1 ) );
5625 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5626 rec_hdr_len + ssl->in_msglen );
5627
5628 /* ssl_parse_record_header() only considers records
5629 * of the next epoch as candidates for buffering. */
5630 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005631 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005632
5633 hs->buffering.future_record.data =
5634 mbedtls_calloc( 1, hs->buffering.future_record.len );
5635 if( hs->buffering.future_record.data == NULL )
5636 {
5637 /* If we run out of RAM trying to buffer a
5638 * record from the next epoch, just ignore. */
5639 return( 0 );
5640 }
5641
Hanno Becker01315ea2018-08-21 17:22:17 +01005642 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005643
Hanno Becker01315ea2018-08-21 17:22:17 +01005644 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005645 return( 0 );
5646}
5647
5648#endif /* MBEDTLS_SSL_PROTO_DTLS */
5649
Hanno Beckere74d5562018-08-15 14:26:08 +01005650static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005651{
5652 int ret;
5653
Hanno Becker5f066e72018-08-16 14:56:31 +01005654#if defined(MBEDTLS_SSL_PROTO_DTLS)
5655 /* We might have buffered a future record; if so,
5656 * and if the epoch matches now, load it.
5657 * On success, this call will set ssl->in_left to
5658 * the length of the buffered record, so that
5659 * the calls to ssl_fetch_input() below will
5660 * essentially be no-ops. */
5661 ret = ssl_load_buffered_record( ssl );
5662 if( ret != 0 )
5663 return( ret );
5664#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005665
Hanno Becker8b09b732019-05-08 12:03:28 +01005666 /* Reset in pointers to default state for TLS/DTLS records,
5667 * assuming no CID and no offset between record content and
5668 * record plaintext. */
5669 ssl_update_in_pointers( ssl );
5670
5671 /* Ensure that we have enough space available for the default form
5672 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5673 * with no space for CIDs counted in). */
5674 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5675 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678 return( ret );
5679 }
5680
5681 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005684 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005685 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005686 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005687 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5688 {
5689 ret = ssl_buffer_future_record( ssl );
5690 if( ret != 0 )
5691 return( ret );
5692
5693 /* Fall through to handling of unexpected records */
5694 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5695 }
5696
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005697 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5698 {
5699 /* Skip unexpected record (but not whole datagram) */
5700 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005701 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005702
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5704 "(header)" ) );
5705 }
5706 else
5707 {
5708 /* Skip invalid record and the rest of the datagram */
5709 ssl->next_record_offset = 0;
5710 ssl->in_left = 0;
5711
5712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5713 "(header)" ) );
5714 }
5715
5716 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005717 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005718 }
5719#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005720 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005721 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722
5723 /*
5724 * Read and optionally decrypt the message contents
5725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005726 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005727 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005730 return( ret );
5731 }
5732
5733 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005735 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005736 {
Hanno Becker43395762019-05-03 14:46:38 +01005737 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005738 if( ssl->next_record_offset < ssl->in_left )
5739 {
5740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5741 }
5742 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005743 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005745#if defined(MBEDTLS_SSL_PROTO_TLS)
5746 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005747 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005748 }
5749#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005750
5751 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005754 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005755 {
5756 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005757 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005758 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005759 /* Except when waiting for Finished as a bad mac here
5760 * probably means something went wrong in the handshake
5761 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5762 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5763 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5764 {
5765#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5766 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5767 {
5768 mbedtls_ssl_send_alert_message( ssl,
5769 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5770 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5771 }
5772#endif
5773 return( ret );
5774 }
5775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005776#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005777 if( ssl->conf->badmac_limit != 0 &&
5778 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5781 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005782 }
5783#endif
5784
Hanno Becker4a810fb2017-05-24 16:27:30 +01005785 /* As above, invalid records cause
5786 * dismissal of the whole datagram. */
5787
5788 ssl->next_record_offset = 0;
5789 ssl->in_left = 0;
5790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005792 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005793 }
5794
5795 return( ret );
5796 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005797 MBEDTLS_SSL_TRANSPORT_ELSE
5798#endif /* MBEDTLS_SSL_PROTO_DTLS */
5799#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005800 {
5801 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5803 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005805 mbedtls_ssl_send_alert_message( ssl,
5806 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5807 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005808 }
5809#endif
5810 return( ret );
5811 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005812#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005813 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005814
Simon Butcher99000142016-10-13 17:21:01 +01005815 return( 0 );
5816}
5817
5818int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5819{
5820 int ret;
5821
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005822 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005823 * Handle particular types of records
5824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 {
Simon Butcher99000142016-10-13 17:21:01 +01005827 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5828 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005829 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005830 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 }
5832
Hanno Beckere678eaa2018-08-21 14:57:46 +01005833 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005834 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005835 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005836 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5838 ssl->in_msglen ) );
5839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005840 }
5841
Hanno Beckere678eaa2018-08-21 14:57:46 +01005842 if( ssl->in_msg[0] != 1 )
5843 {
5844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5845 ssl->in_msg[0] ) );
5846 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5847 }
5848
5849#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005850 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005851 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5852 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5853 {
5854 if( ssl->handshake == NULL )
5855 {
5856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5857 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5858 }
5859
5860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5861 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5862 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005863#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005864 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005868 if( ssl->in_msglen != 2 )
5869 {
5870 /* Note: Standard allows for more than one 2 byte alert
5871 to be packed in a single message, but Mbed TLS doesn't
5872 currently support this. */
5873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5874 ssl->in_msglen ) );
5875 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5876 }
5877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 ssl->in_msg[0], ssl->in_msg[1] ) );
5880
5881 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005882 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005884 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005887 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 }
5890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005891 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5892 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5895 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005897
5898#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5899 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5900 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5901 {
Hanno Becker90333da2017-10-10 11:27:13 +01005902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005903 /* Will be handled when trying to parse ServerHello */
5904 return( 0 );
5905 }
5906#endif
5907
5908#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5910 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5911 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5912 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5913 {
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5915 /* Will be handled in mbedtls_ssl_parse_certificate() */
5916 return( 0 );
5917 }
5918#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5919
5920 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005921 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 }
5923
Hanno Beckerc76c6192017-06-06 10:03:17 +01005924#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005925 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005927 /* Drop unexpected ApplicationData records,
5928 * except at the beginning of renegotiations */
5929 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5930 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5931#if defined(MBEDTLS_SSL_RENEGOTIATION)
5932 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5933 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005934#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005935 )
5936 {
5937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5938 return( MBEDTLS_ERR_SSL_NON_FATAL );
5939 }
5940
5941 if( ssl->handshake != NULL &&
5942 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5943 {
5944 ssl_handshake_wrapup_free_hs_transform( ssl );
5945 }
5946 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005947#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005948
Paul Bakker5121ce52009-01-03 21:22:43 +00005949 return( 0 );
5950}
5951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005952int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005953{
5954 int ret;
5955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5957 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5958 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005959 {
5960 return( ret );
5961 }
5962
5963 return( 0 );
5964}
5965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005967 unsigned char level,
5968 unsigned char message )
5969{
5970 int ret;
5971
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005972 if( ssl == NULL || ssl->conf == NULL )
5973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005976 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005979 ssl->out_msglen = 2;
5980 ssl->out_msg[0] = level;
5981 ssl->out_msg[1] = message;
5982
Hanno Becker67bc7c32018-08-06 11:33:50 +01005983 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005986 return( ret );
5987 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005989
5990 return( 0 );
5991}
5992
Hanno Becker17572472019-02-08 07:19:04 +00005993#if defined(MBEDTLS_X509_CRT_PARSE_C)
5994static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5995{
5996#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5997 if( session->peer_cert != NULL )
5998 {
5999 mbedtls_x509_crt_free( session->peer_cert );
6000 mbedtls_free( session->peer_cert );
6001 session->peer_cert = NULL;
6002 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006003#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker17572472019-02-08 07:19:04 +00006004 if( session->peer_cert_digest != NULL )
6005 {
6006 /* Zeroization is not necessary. */
6007 mbedtls_free( session->peer_cert_digest );
6008 session->peer_cert_digest = NULL;
6009 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6010 session->peer_cert_digest_len = 0;
6011 }
Hanno Becker5882dd02019-06-06 16:25:57 +01006012#else
6013 ((void) session);
6014#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker17572472019-02-08 07:19:04 +00006015}
6016#endif /* MBEDTLS_X509_CRT_PARSE_C */
6017
Paul Bakker5121ce52009-01-03 21:22:43 +00006018/*
6019 * Handshake functions
6020 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006021#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006022/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006024{
Hanno Becker8759e162017-12-27 21:34:08 +00006025 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006028
Hanno Becker5097cba2019-02-05 13:36:46 +00006029 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006032 ssl->state++;
6033 return( 0 );
6034 }
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006038}
6039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006041{
Hanno Becker8759e162017-12-27 21:34:08 +00006042 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006045
Hanno Becker5097cba2019-02-05 13:36:46 +00006046 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006049 ssl->state++;
6050 return( 0 );
6051 }
6052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6054 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006055}
Gilles Peskinef9828522017-05-03 12:28:43 +02006056
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006057#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006058/* Some certificate support -> implement write and parse */
6059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006060int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006062 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006063 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006068
Hanno Becker5097cba2019-02-05 13:36:46 +00006069 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006072 ssl->state++;
6073 return( 0 );
6074 }
6075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006076#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006077 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
6079 if( ssl->client_auth == 0 )
6080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082 ssl->state++;
6083 return( 0 );
6084 }
6085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006086#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006087 /*
6088 * If using SSLv3 and got no cert, send an Alert message
6089 * (otherwise an empty Certificate message will be sent).
6090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6092 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 {
6094 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006095 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6096 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6097 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006100 goto write_msg;
6101 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006103 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104#endif /* MBEDTLS_SSL_CLI_C */
6105#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006106 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6111 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 }
6113 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006116 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006117
6118 /*
6119 * 0 . 0 handshake type
6120 * 1 . 3 handshake length
6121 * 4 . 6 length of all certs
6122 * 7 . 9 length of cert. 1
6123 * 10 . n-1 peer certificate
6124 * n . n+2 length of cert. 2
6125 * n+3 . ... upper level cert, etc.
6126 */
6127 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129
Paul Bakker29087132010-03-21 21:03:34 +00006130 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
6132 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006133 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006136 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 }
6139
6140 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6141 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6142 ssl->out_msg[i + 2] = (unsigned char)( n );
6143
6144 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6145 i += n; crt = crt->next;
6146 }
6147
6148 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6149 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6150 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6151
6152 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6154 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006155
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006156#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006157write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006159
6160 ssl->state++;
6161
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006162 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006163 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 return( ret );
6166 }
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006169
Paul Bakkered27a042013-04-18 22:46:23 +02006170 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006171}
6172
Hanno Becker285ff0c2019-01-31 07:44:03 +00006173#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006174
6175#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006176static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6177 unsigned char *crt_buf,
6178 size_t crt_buf_len )
6179{
6180 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6181
6182 if( peer_crt == NULL )
6183 return( -1 );
6184
6185 if( peer_crt->raw.len != crt_buf_len )
6186 return( -1 );
6187
Hanno Becker68b856d2019-02-08 14:00:04 +00006188 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006189}
Hanno Becker5882dd02019-06-06 16:25:57 +01006190#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Beckerdf759382019-02-05 17:02:46 +00006191static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6192 unsigned char *crt_buf,
6193 size_t crt_buf_len )
6194{
6195 int ret;
6196 unsigned char const * const peer_cert_digest =
6197 ssl->session->peer_cert_digest;
6198 mbedtls_md_type_t const peer_cert_digest_type =
6199 ssl->session->peer_cert_digest_type;
6200 mbedtls_md_info_t const * const digest_info =
6201 mbedtls_md_info_from_type( peer_cert_digest_type );
6202 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6203 size_t digest_len;
6204
6205 if( peer_cert_digest == NULL || digest_info == NULL )
6206 return( -1 );
6207
6208 digest_len = mbedtls_md_get_size( digest_info );
6209 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6210 return( -1 );
6211
6212 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6213 if( ret != 0 )
6214 return( -1 );
6215
6216 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6217}
Hanno Becker5882dd02019-06-06 16:25:57 +01006218#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006219#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006220
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006221/*
6222 * Once the certificate message is read, parse it into a cert chain and
6223 * perform basic checks, but leave actual verification to the caller
6224 */
Hanno Becker35e41772019-02-05 15:37:23 +00006225static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6226 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006227{
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006228 int ret;
Hanno Becker35e41772019-02-05 15:37:23 +00006229#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6230 int crt_cnt=0;
6231#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006232 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006233 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006238 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6239 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006241 }
6242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006243 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6244 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006247 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6248 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 }
6251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006253
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006257 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006259 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006263 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6264 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 }
6267
Hanno Becker33c3dc82019-01-30 14:46:46 +00006268 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6269 i += 3;
6270
Hanno Becker33c3dc82019-01-30 14:46:46 +00006271 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006272 while( i < ssl->in_hslen )
6273 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006274 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006275 if ( i + 3 > ssl->in_hslen ) {
6276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006277 mbedtls_ssl_send_alert_message( ssl,
6278 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6279 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006280 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6281 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006282 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6283 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006284 if( ssl->in_msg[i] != 0 )
6285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006287 mbedtls_ssl_send_alert_message( ssl,
6288 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6289 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 }
6292
Hanno Becker33c3dc82019-01-30 14:46:46 +00006293 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006294 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6295 | (unsigned int) ssl->in_msg[i + 2];
6296 i += 3;
6297
6298 if( n < 128 || i + n > ssl->in_hslen )
6299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006301 mbedtls_ssl_send_alert_message( ssl,
6302 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6303 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006305 }
6306
Hanno Becker33c3dc82019-01-30 14:46:46 +00006307 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006308#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6309 if( crt_cnt++ == 0 &&
6310 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6311 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006312 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006313 /* During client-side renegotiation, check that the server's
6314 * end-CRTs hasn't changed compared to the initial handshake,
6315 * mitigating the triple handshake attack. On success, reuse
6316 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6318 if( ssl_check_peer_crt_unchanged( ssl,
6319 &ssl->in_msg[i],
6320 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006321 {
Hanno Becker35e41772019-02-05 15:37:23 +00006322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6323 mbedtls_ssl_send_alert_message( ssl,
6324 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6325 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6326 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006327 }
Hanno Becker35e41772019-02-05 15:37:23 +00006328
6329 /* Now we can safely free the original chain. */
6330 ssl_clear_peer_cert( ssl->session );
6331 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006332#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6333
Hanno Becker33c3dc82019-01-30 14:46:46 +00006334 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006335#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006336 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006337#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006338 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006339 * it in-place from the input buffer instead of making a copy. */
6340 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6341#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006342 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006343 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006344 case 0: /*ok*/
6345 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6346 /* Ignore certificate with an unknown algorithm: maybe a
6347 prior certificate was already trusted. */
6348 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006349
Hanno Becker33c3dc82019-01-30 14:46:46 +00006350 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6351 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6352 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006353
Hanno Becker33c3dc82019-01-30 14:46:46 +00006354 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6355 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6356 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006357
Hanno Becker33c3dc82019-01-30 14:46:46 +00006358 default:
6359 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6360 crt_parse_der_failed:
6361 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6362 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6363 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 }
6365
6366 i += n;
6367 }
6368
Hanno Becker35e41772019-02-05 15:37:23 +00006369 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006370 return( 0 );
6371}
6372
Hanno Beckerb8a08572019-02-05 12:49:06 +00006373#if defined(MBEDTLS_SSL_SRV_C)
6374static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6375{
6376 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6377 return( -1 );
6378
6379#if defined(MBEDTLS_SSL_PROTO_SSL3)
6380 /*
6381 * Check if the client sent an empty certificate
6382 */
6383 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6384 {
6385 if( ssl->in_msglen == 2 &&
6386 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6387 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6388 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6389 {
6390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6391 return( 0 );
6392 }
6393
6394 return( -1 );
6395 }
6396#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6397
6398#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6399 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6400 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6401 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6402 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6403 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6404 {
6405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6406 return( 0 );
6407 }
6408
Hanno Beckerb8a08572019-02-05 12:49:06 +00006409#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6410 MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker17daaa52019-06-18 12:31:45 +01006411
6412 return( -1 );
Hanno Beckerb8a08572019-02-05 12:49:06 +00006413}
6414#endif /* MBEDTLS_SSL_SRV_C */
6415
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006416/* Check if a certificate message is expected.
6417 * Return either
6418 * - SSL_CERTIFICATE_EXPECTED, or
6419 * - SSL_CERTIFICATE_SKIP
6420 * indicating whether a Certificate message is expected or not.
6421 */
6422#define SSL_CERTIFICATE_EXPECTED 0
6423#define SSL_CERTIFICATE_SKIP 1
6424static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6425 int authmode )
6426{
6427 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6428 ssl->handshake->ciphersuite_info;
6429
6430 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6431 return( SSL_CERTIFICATE_SKIP );
6432
6433#if defined(MBEDTLS_SSL_SRV_C)
6434 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6435 {
6436 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6437 return( SSL_CERTIFICATE_SKIP );
6438
6439 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6440 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006441 ssl->session_negotiate->verify_result =
6442 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6443 return( SSL_CERTIFICATE_SKIP );
6444 }
6445 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006446#else
6447 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006448#endif /* MBEDTLS_SSL_SRV_C */
6449
6450 return( SSL_CERTIFICATE_EXPECTED );
6451}
6452
Hanno Becker3cf50612019-02-05 14:36:34 +00006453static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6454 int authmode,
6455 mbedtls_x509_crt *chain,
6456 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006457{
Hanno Becker613d4902019-02-05 13:11:17 +00006458 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006459 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6460 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006461 mbedtls_x509_crt *ca_chain;
6462 mbedtls_x509_crl *ca_crl;
6463
6464 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6465 return( 0 );
6466
6467#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6468 if( ssl->handshake->sni_ca_chain != NULL )
6469 {
6470 ca_chain = ssl->handshake->sni_ca_chain;
6471 ca_crl = ssl->handshake->sni_ca_crl;
6472 }
6473 else
6474#endif
6475 {
6476 ca_chain = ssl->conf->ca_chain;
6477 ca_crl = ssl->conf->ca_crl;
6478 }
6479
6480 /*
6481 * Main check: verify certificate
6482 */
6483 ret = mbedtls_x509_crt_verify_restartable(
6484 chain,
6485 ca_chain, ca_crl,
6486 ssl->conf->cert_profile,
6487 ssl->hostname,
6488 &ssl->session_negotiate->verify_result,
6489 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6490
6491 if( ret != 0 )
6492 {
6493 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6494 }
6495
6496#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6497 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6498 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6499#endif
6500
6501 /*
6502 * Secondary checks: always done, but change 'ret' only if it was 0
6503 */
6504
6505#if defined(MBEDTLS_ECP_C)
6506 {
6507 const mbedtls_pk_context *pk = &chain->pk;
6508
6509 /* If certificate uses an EC key, make sure the curve is OK */
6510 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6511 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6512 {
6513 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6514
6515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6516 if( ret == 0 )
6517 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6518 }
6519 }
6520#endif /* MBEDTLS_ECP_C */
6521
6522 if( mbedtls_ssl_check_cert_usage( chain,
6523 ciphersuite_info,
6524 ! ssl->conf->endpoint,
6525 &ssl->session_negotiate->verify_result ) != 0 )
6526 {
6527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6528 if( ret == 0 )
6529 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6530 }
6531
6532 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6533 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6534 * with details encoded in the verification flags. All other kinds
6535 * of error codes, including those from the user provided f_vrfy
6536 * functions, are treated as fatal and lead to a failure of
6537 * ssl_parse_certificate even if verification was optional. */
6538 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6539 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6540 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6541 {
6542 ret = 0;
6543 }
6544
6545 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6546 {
6547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6548 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6549 }
6550
6551 if( ret != 0 )
6552 {
6553 uint8_t alert;
6554
6555 /* The certificate may have been rejected for several reasons.
6556 Pick one and send the corresponding alert. Which alert to send
6557 may be a subject of debate in some cases. */
6558 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6559 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6560 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6561 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6563 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6565 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6567 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6569 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6571 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6573 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6574 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6575 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6576 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6577 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6578 else
6579 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6580 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6581 alert );
6582 }
6583
6584#if defined(MBEDTLS_DEBUG_C)
6585 if( ssl->session_negotiate->verify_result != 0 )
6586 {
6587 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6588 ssl->session_negotiate->verify_result ) );
6589 }
6590 else
6591 {
6592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6593 }
6594#endif /* MBEDTLS_DEBUG_C */
6595
6596 return( ret );
6597}
6598
Hanno Becker34106f62019-02-08 14:59:05 +00006599#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker5882dd02019-06-06 16:25:57 +01006600
6601#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006602static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6603 unsigned char *start, size_t len )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006604{
6605 int ret;
Hanno Becker34106f62019-02-08 14:59:05 +00006606 /* Remember digest of the peer's end-CRT. */
6607 ssl->session_negotiate->peer_cert_digest =
6608 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6609 if( ssl->session_negotiate->peer_cert_digest == NULL )
6610 {
6611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6612 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6613 mbedtls_ssl_send_alert_message( ssl,
6614 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6615 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6616
6617 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6618 }
6619
6620 ret = mbedtls_md( mbedtls_md_info_from_type(
6621 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6622 start, len,
6623 ssl->session_negotiate->peer_cert_digest );
6624
6625 ssl->session_negotiate->peer_cert_digest_type =
6626 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6627 ssl->session_negotiate->peer_cert_digest_len =
6628 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6629
6630 return( ret );
6631}
Hanno Becker5882dd02019-06-06 16:25:57 +01006632#endif /* MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker34106f62019-02-08 14:59:05 +00006633
6634static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6635 unsigned char *start, size_t len )
6636{
6637 unsigned char *end = start + len;
6638 int ret;
6639
6640 /* Make a copy of the peer's raw public key. */
6641 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6642 ret = mbedtls_pk_parse_subpubkey( &start, end,
6643 &ssl->handshake->peer_pubkey );
6644 if( ret != 0 )
6645 {
6646 /* We should have parsed the public key before. */
6647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6648 }
6649
6650 return( 0 );
6651}
6652#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6653
Hanno Becker3cf50612019-02-05 14:36:34 +00006654int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6655{
6656 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006657 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006658#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6659 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6660 ? ssl->handshake->sni_authmode
6661 : ssl->conf->authmode;
6662#else
6663 const int authmode = ssl->conf->authmode;
6664#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006665 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006666 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006667
6668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6669
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006670 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6671 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006672 {
6673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006674 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006675 }
6676
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006677#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6678 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006679 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006680 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006681 chain = ssl->handshake->ecrs_peer_cert;
6682 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006683 goto crt_verify;
6684 }
6685#endif
6686
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006687 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006688 {
6689 /* mbedtls_ssl_read_record may have sent an alert already. We
6690 let it decide whether to alert. */
6691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006692 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006693 }
6694
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006695#if defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerb8a08572019-02-05 12:49:06 +00006696 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6697 {
6698 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006699
Hanno Beckerb8a08572019-02-05 12:49:06 +00006700 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006701 ret = 0;
6702 else
6703 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006704
Hanno Becker613d4902019-02-05 13:11:17 +00006705 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006706 }
Hanno Beckerb8a08572019-02-05 12:49:06 +00006707#endif /* MBEDTLS_SSL_SRV_C */
6708
Hanno Becker35e41772019-02-05 15:37:23 +00006709 /* Clear existing peer CRT structure in case we tried to
6710 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006711 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006712
6713 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6714 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006715 {
6716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6717 sizeof( mbedtls_x509_crt ) ) );
6718 mbedtls_ssl_send_alert_message( ssl,
6719 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6720 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006721
Hanno Beckere4aeb762019-02-05 17:19:52 +00006722 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6723 goto exit;
6724 }
6725 mbedtls_x509_crt_init( chain );
6726
6727 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006728 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006729 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006730
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006731#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6732 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006733 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006734
6735crt_verify:
6736 if( ssl->handshake->ecrs_enabled)
6737 rs_ctx = &ssl->handshake->ecrs_ctx;
6738#endif
6739
Hanno Becker3cf50612019-02-05 14:36:34 +00006740 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006741 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006742 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006743 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006744
Hanno Becker3008d282019-02-05 17:02:28 +00006745#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakker5121ce52009-01-03 21:22:43 +00006746 {
Hanno Becker5882dd02019-06-06 16:25:57 +01006747 size_t pk_len;
6748 unsigned char *pk_start;
Paul Bakker5121ce52009-01-03 21:22:43 +00006749
Hanno Becker34106f62019-02-08 14:59:05 +00006750 /* We parse the CRT chain without copying, so
6751 * these pointers point into the input buffer,
6752 * and are hence still valid after freeing the
6753 * CRT chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006754
Hanno Becker5882dd02019-06-06 16:25:57 +01006755#if defined(MBEDTLS_SSL_RENEGOTIATION)
6756 unsigned char *crt_start;
6757 size_t crt_len;
6758
Hanno Becker34106f62019-02-08 14:59:05 +00006759 crt_start = chain->raw.p;
6760 crt_len = chain->raw.len;
Hanno Becker5882dd02019-06-06 16:25:57 +01006761#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006762
Hanno Becker34106f62019-02-08 14:59:05 +00006763 pk_start = chain->pk_raw.p;
6764 pk_len = chain->pk_raw.len;
6765
6766 /* Free the CRT structures before computing
6767 * digest and copying the peer's public key. */
6768 mbedtls_x509_crt_free( chain );
6769 mbedtls_free( chain );
6770 chain = NULL;
6771
Hanno Becker5882dd02019-06-06 16:25:57 +01006772#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker34106f62019-02-08 14:59:05 +00006773 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006774 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006775 goto exit;
Hanno Becker5882dd02019-06-06 16:25:57 +01006776#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006777
Hanno Becker34106f62019-02-08 14:59:05 +00006778 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006779 if( ret != 0 )
Hanno Becker34106f62019-02-08 14:59:05 +00006780 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006781 }
Hanno Becker34106f62019-02-08 14:59:05 +00006782#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6783 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006784 ssl->session_negotiate->peer_cert = chain;
6785 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006786#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789
Hanno Becker613d4902019-02-05 13:11:17 +00006790exit:
6791
Hanno Beckere4aeb762019-02-05 17:19:52 +00006792 if( ret == 0 )
6793 ssl->state++;
6794
6795#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6796 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6797 {
6798 ssl->handshake->ecrs_peer_cert = chain;
6799 chain = NULL;
6800 }
6801#endif
6802
6803 if( chain != NULL )
6804 {
6805 mbedtls_x509_crt_free( chain );
6806 mbedtls_free( chain );
6807 }
6808
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 return( ret );
6810}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006811#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006814{
6815 int ret;
6816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006819 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006820 ssl->out_msglen = 1;
6821 ssl->out_msg[0] = 1;
6822
Paul Bakker5121ce52009-01-03 21:22:43 +00006823 ssl->state++;
6824
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006825 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 return( ret );
6829 }
6830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006832
6833 return( 0 );
6834}
6835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006836int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006837{
6838 int ret;
6839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006841
Hanno Becker327c93b2018-08-15 13:56:18 +01006842 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 return( ret );
6846 }
6847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6852 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006853 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006854 }
6855
Hanno Beckere678eaa2018-08-21 14:57:46 +01006856 /* CCS records are only accepted if they have length 1 and content '1',
6857 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006858
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006859 /*
6860 * Switch to our negotiated transform and session parameters for inbound
6861 * data.
6862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006864 ssl->transform_in = ssl->transform_negotiate;
6865 ssl->session_in = ssl->session_negotiate;
6866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006868 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006870#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006871 ssl_dtls_replay_reset( ssl );
6872#endif
6873
6874 /* Increment epoch */
6875 if( ++ssl->in_epoch == 0 )
6876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006878 /* This is highly unlikely to happen for legitimate reasons, so
6879 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006880 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006881 }
6882 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006883 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006884#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006885#if defined(MBEDTLS_SSL_PROTO_TLS)
6886 {
6887 memset( ssl->in_ctr, 0, 8 );
6888 }
6889#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006890
Hanno Beckerf5970a02019-05-08 09:38:41 +01006891 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6894 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006896 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006898 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006899 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6900 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006901 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006902 }
6903 }
6904#endif
6905
Paul Bakker5121ce52009-01-03 21:22:43 +00006906 ssl->state++;
6907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006909
6910 return( 0 );
6911}
6912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006913void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6914 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006915{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006916 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6919 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6920 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006921 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006922 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006923#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006924#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6925#if defined(MBEDTLS_SHA512_C)
6926 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006927 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6928 else
6929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if defined(MBEDTLS_SHA256_C)
6931 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006932 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006933 else
6934#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006935#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006938 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006939 }
Paul Bakker380da532012-04-18 16:10:25 +00006940}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006942void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006943{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006944#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6945 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006946 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6947 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006948#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006949#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6950#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006951 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006953#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006954 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006956#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006957}
6958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006959static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006960 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006961{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006962#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6963 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006964 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6965 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006967#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6968#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006969 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006970#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006972 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006974#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006975}
6976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6978 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6979static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006980 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006981{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006982 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6983 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006984}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006985#endif
Paul Bakker380da532012-04-18 16:10:25 +00006986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006987#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6988#if defined(MBEDTLS_SHA256_C)
6989static void ssl_update_checksum_sha256( 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{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006992 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006993}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006994#endif
Paul Bakker380da532012-04-18 16:10:25 +00006995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006996#if defined(MBEDTLS_SHA512_C)
6997static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006998 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006999{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007000 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007001}
Paul Bakker769075d2012-11-24 11:26:46 +01007002#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007005#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007006static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007007 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007008{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007009 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007010 mbedtls_md5_context md5;
7011 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007012
Paul Bakker5121ce52009-01-03 21:22:43 +00007013 unsigned char padbuf[48];
7014 unsigned char md5sum[16];
7015 unsigned char sha1sum[20];
7016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007017 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007018 if( !session )
7019 session = ssl->session;
7020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007022
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007023 mbedtls_md5_init( &md5 );
7024 mbedtls_sha1_init( &sha1 );
7025
7026 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7027 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
7029 /*
7030 * SSLv3:
7031 * hash =
7032 * MD5( master + pad2 +
7033 * MD5( handshake + sender + master + pad1 ) )
7034 * + SHA1( master + pad2 +
7035 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007036 */
7037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007039 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7040 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007043#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007044 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7045 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007046#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007048 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007049 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007050
Paul Bakker1ef83d62012-04-11 12:09:53 +00007051 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007052
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007053 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7054 mbedtls_md5_update_ret( &md5, session->master, 48 );
7055 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7056 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007058 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7059 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7060 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7061 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
Paul Bakker1ef83d62012-04-11 12:09:53 +00007063 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007065 mbedtls_md5_starts_ret( &md5 );
7066 mbedtls_md5_update_ret( &md5, session->master, 48 );
7067 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7068 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7069 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007071 mbedtls_sha1_starts_ret( &sha1 );
7072 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7073 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7074 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7075 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007077 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007078
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007079 mbedtls_md5_free( &md5 );
7080 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007081
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007082 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7083 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7084 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007091static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007093{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007094 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007095 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007096 mbedtls_md5_context md5;
7097 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007098 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007101 if( !session )
7102 session = ssl->session;
7103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007106 mbedtls_md5_init( &md5 );
7107 mbedtls_sha1_init( &sha1 );
7108
7109 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7110 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112 /*
7113 * TLSv1:
7114 * hash = PRF( master, finished_label,
7115 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7116 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007119 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7120 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007121#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007124 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7125 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007129 ? "client finished"
7130 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007131
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007132 mbedtls_md5_finish_ret( &md5, padbuf );
7133 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007134
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007135 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007136 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007139
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007140 mbedtls_md5_free( &md5 );
7141 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007142
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007143 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7150#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007151static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007153{
7154 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007155 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007156 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007157 unsigned char padbuf[32];
7158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007160 if( !session )
7161 session = ssl->session;
7162
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007163 mbedtls_sha256_init( &sha256 );
7164
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007166
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007167 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007168
7169 /*
7170 * TLSv1.2:
7171 * hash = PRF( master, finished_label,
7172 * Hash( handshake ) )[0.11]
7173 */
7174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175#if !defined(MBEDTLS_SHA256_ALT)
7176 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007177 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007178#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007181 ? "client finished"
7182 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007183
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007184 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007185
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007186 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007187 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007190
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007191 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007193 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007199#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007200static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007202{
7203 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007204 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007205 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007206 unsigned char padbuf[48];
7207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007209 if( !session )
7210 session = ssl->session;
7211
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007212 mbedtls_sha512_init( &sha512 );
7213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007215
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007216 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007217
7218 /*
7219 * TLSv1.2:
7220 * hash = PRF( master, finished_label,
7221 * Hash( handshake ) )[0.11]
7222 */
7223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007225 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7226 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007227#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007230 ? "client finished"
7231 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007232
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007234
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007235 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007236 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007239
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007240 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007241
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007242 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007245}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#endif /* MBEDTLS_SHA512_C */
7247#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007252
7253 /*
7254 * Free our handshake params
7255 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007256 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007258 ssl->handshake = NULL;
7259
7260 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007261 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007262 */
7263 if( ssl->transform )
7264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 mbedtls_ssl_transform_free( ssl->transform );
7266 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007267 }
7268 ssl->transform = ssl->transform_negotiate;
7269 ssl->transform_negotiate = NULL;
7270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007272}
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007275{
7276 int resume = ssl->handshake->resume;
7277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280#if defined(MBEDTLS_SSL_RENEGOTIATION)
7281 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007284 ssl->renego_records_seen = 0;
7285 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007286#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007287
7288 /*
7289 * Free the previous session and switch in the current one
7290 */
Paul Bakker0a597072012-09-25 21:55:46 +00007291 if( ssl->session )
7292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007294 /* RFC 7366 3.1: keep the EtM state */
7295 ssl->session_negotiate->encrypt_then_mac =
7296 ssl->session->encrypt_then_mac;
7297#endif
7298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299 mbedtls_ssl_session_free( ssl->session );
7300 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007301 }
7302 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007303 ssl->session_negotiate = NULL;
7304
Paul Bakker0a597072012-09-25 21:55:46 +00007305 /*
7306 * Add cache entry
7307 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007308 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007309 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007310 resume == 0 )
7311 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007312 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007314 }
Paul Bakker0a597072012-09-25 21:55:46 +00007315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007317 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007318 ssl->handshake->flight != NULL )
7319 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007320 /* Cancel handshake timer */
7321 ssl_set_timer( ssl, 0 );
7322
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007323 /* Keep last flight around in case we need to resend it:
7324 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007326 }
7327 else
7328#endif
7329 ssl_handshake_wrapup_free_hs_transform( ssl );
7330
Paul Bakker48916f92012-09-16 19:57:18 +00007331 ssl->state++;
7332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007334}
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007337{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007338 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007341
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007342 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007343
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007344 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007345
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007346 /*
7347 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7348 * may define some other value. Currently (early 2016), no defined
7349 * ciphersuite does this (and this is unlikely to change as activity has
7350 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007355 ssl->verify_data_len = hash_len;
7356 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007357#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007358
Paul Bakker5121ce52009-01-03 21:22:43 +00007359 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007360 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7361 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
7363 /*
7364 * In case of session resuming, invert the client and server
7365 * ChangeCipherSpec messages order.
7366 */
Paul Bakker0a597072012-09-25 21:55:46 +00007367 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007371 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007372#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007374 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007376#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007377 }
7378 else
7379 ssl->state++;
7380
Paul Bakker48916f92012-09-16 19:57:18 +00007381 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007382 * Switch to our negotiated transform and session parameters for outbound
7383 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007388 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007389 {
7390 unsigned char i;
7391
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007392 /* Remember current epoch settings for resending */
7393 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007394 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007395
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007396 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007397 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007398
7399 /* Increment epoch */
7400 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007401 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007402 break;
7403
7404 /* The loop goes to its end iff the counter is wrapping */
7405 if( i == 0 )
7406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7408 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007409 }
7410 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007411 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007413#if defined(MBEDTLS_SSL_PROTO_TLS)
7414 {
7415 memset( ssl->cur_out_ctr, 0, 8 );
7416 }
7417#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007418
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007419 ssl->transform_out = ssl->transform_negotiate;
7420 ssl->session_out = ssl->session_negotiate;
7421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7423 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7428 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007429 }
7430 }
7431#endif
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007434 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007436#endif
7437
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007438 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007439 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 return( ret );
7442 }
7443
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007445 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007446 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7447 {
7448 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7449 return( ret );
7450 }
7451#endif
7452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007454
7455 return( 0 );
7456}
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007459#define SSL_MAX_HASH_LEN 36
7460#else
7461#define SSL_MAX_HASH_LEN 12
7462#endif
7463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007465{
Paul Bakker23986e52011-04-24 08:57:21 +00007466 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007467 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007468 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007471
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007472 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
Hanno Becker327c93b2018-08-15 13:56:18 +01007474 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007477 return( ret );
7478 }
7479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007483 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7484 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007486 }
7487
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007488 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489#if defined(MBEDTLS_SSL_PROTO_SSL3)
7490 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007491 hash_len = 36;
7492 else
7493#endif
7494 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7497 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007500 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7501 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503 }
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007506 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007512 }
7513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007515 ssl->verify_data_len = hash_len;
7516 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007517#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007518
Paul Bakker0a597072012-09-25 21:55:46 +00007519 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007522 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007526 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007529 }
7530 else
7531 ssl->state++;
7532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007534 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007536#endif
7537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007539
7540 return( 0 );
7541}
7542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007543static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007547#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7548 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7549 mbedtls_md5_init( &handshake->fin_md5 );
7550 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007551 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7552 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7555#if defined(MBEDTLS_SHA256_C)
7556 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007557 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007558#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559#if defined(MBEDTLS_SHA512_C)
7560 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007562#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007564
7565 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007566
7567#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7568 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7569 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7570#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_DHM_C)
7573 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007575#if defined(MBEDTLS_ECDH_C)
7576 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007577#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007578#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007579 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007580#if defined(MBEDTLS_SSL_CLI_C)
7581 handshake->ecjpake_cache = NULL;
7582 handshake->ecjpake_cache_len = 0;
7583#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007584#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007585
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007586#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007587 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007588#endif
7589
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007590#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7591 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7592#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007593
7594#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7595 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7596 mbedtls_pk_init( &handshake->peer_pubkey );
7597#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007598}
7599
Hanno Becker611a83b2018-01-03 14:27:32 +00007600void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7605 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007606
Hanno Becker92231322018-01-03 15:32:51 +00007607#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_md_init( &transform->md_ctx_enc );
7609 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007610#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007611}
7612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007613void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007614{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007616}
7617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007619{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007620 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007621 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007623 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007625 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007626 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007627
7628 /*
7629 * Either the pointers are now NULL or cleared properly and can be freed.
7630 * Now allocate missing structures.
7631 */
7632 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007633 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007634 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007635 }
Paul Bakker48916f92012-09-16 19:57:18 +00007636
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007637 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007638 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007639 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007640 }
Paul Bakker48916f92012-09-16 19:57:18 +00007641
Paul Bakker82788fb2014-10-20 13:59:19 +02007642 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007643 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007644 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007645 }
Paul Bakker48916f92012-09-16 19:57:18 +00007646
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007647 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007648 if( ssl->handshake == NULL ||
7649 ssl->transform_negotiate == NULL ||
7650 ssl->session_negotiate == NULL )
7651 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 mbedtls_free( ssl->handshake );
7655 mbedtls_free( ssl->transform_negotiate );
7656 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007657
7658 ssl->handshake = NULL;
7659 ssl->transform_negotiate = NULL;
7660 ssl->session_negotiate = NULL;
7661
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007662 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007663 }
7664
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007665 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007667 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007668 ssl_handshake_params_init( ssl->handshake );
7669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007671 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007672 {
7673 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007674
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007675 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7676 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7677 else
7678 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007679
7680 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007681 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007682#endif
7683
Paul Bakker48916f92012-09-16 19:57:18 +00007684 return( 0 );
7685}
7686
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007687#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007688/* Dummy cookie callbacks for defaults */
7689static int ssl_cookie_write_dummy( void *ctx,
7690 unsigned char **p, unsigned char *end,
7691 const unsigned char *cli_id, size_t cli_id_len )
7692{
7693 ((void) ctx);
7694 ((void) p);
7695 ((void) end);
7696 ((void) cli_id);
7697 ((void) cli_id_len);
7698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007700}
7701
7702static int ssl_cookie_check_dummy( void *ctx,
7703 const unsigned char *cookie, size_t cookie_len,
7704 const unsigned char *cli_id, size_t cli_id_len )
7705{
7706 ((void) ctx);
7707 ((void) cookie);
7708 ((void) cookie_len);
7709 ((void) cli_id);
7710 ((void) cli_id_len);
7711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007713}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007714#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007715
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007716/* Once ssl->out_hdr as the address of the beginning of the
7717 * next outgoing record is set, deduce the other pointers.
7718 *
7719 * Note: For TLS, we save the implicit record sequence number
7720 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7721 * and the caller has to make sure there's space for this.
7722 */
7723
7724static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7725 mbedtls_ssl_transform *transform )
7726{
7727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007728 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007729 {
7730 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007731#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007732 ssl->out_cid = ssl->out_ctr + 8;
7733 ssl->out_len = ssl->out_cid;
7734 if( transform != NULL )
7735 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007736#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007737 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007738#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007739 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007740 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007741 MBEDTLS_SSL_TRANSPORT_ELSE
7742#endif /* MBEDTLS_SSL_PROTO_DTLS */
7743#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007744 {
7745 ssl->out_ctr = ssl->out_hdr - 8;
7746 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007747#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007748 ssl->out_cid = ssl->out_len;
7749#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007750 ssl->out_iv = ssl->out_hdr + 5;
7751 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007752#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007753
7754 /* Adjust out_msg to make space for explicit IV, if used. */
7755 if( transform != NULL &&
7756 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7757 {
7758 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7759 }
7760 else
7761 ssl->out_msg = ssl->out_iv;
7762}
7763
7764/* Once ssl->in_hdr as the address of the beginning of the
7765 * next incoming record is set, deduce the other pointers.
7766 *
7767 * Note: For TLS, we save the implicit record sequence number
7768 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7769 * and the caller has to make sure there's space for this.
7770 */
7771
Hanno Beckerf5970a02019-05-08 09:38:41 +01007772static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007773{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007774 /* This function sets the pointers to match the case
7775 * of unprotected TLS/DTLS records, with both ssl->in_iv
7776 * and ssl->in_msg pointing to the beginning of the record
7777 * content.
7778 *
7779 * When decrypting a protected record, ssl->in_msg
7780 * will be shifted to point to the beginning of the
7781 * record plaintext.
7782 */
7783
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007784#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007785 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007786 {
Hanno Becker70e79282019-05-03 14:34:53 +01007787 /* This sets the header pointers to match records
7788 * without CID. When we receive a record containing
7789 * a CID, the fields are shifted accordingly in
7790 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007791 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007792#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007793 ssl->in_cid = ssl->in_ctr + 8;
7794 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007795#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007796 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007797#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007798 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007799 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007800 MBEDTLS_SSL_TRANSPORT_ELSE
7801#endif /* MBEDTLS_SSL_PROTO_DTLS */
7802#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007803 {
7804 ssl->in_ctr = ssl->in_hdr - 8;
7805 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007807 ssl->in_cid = ssl->in_len;
7808#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007809 ssl->in_iv = ssl->in_hdr + 5;
7810 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007811#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007812
Hanno Beckerf5970a02019-05-08 09:38:41 +01007813 /* This will be adjusted at record decryption time. */
7814 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007815}
7816
Paul Bakker5121ce52009-01-03 21:22:43 +00007817/*
7818 * Initialize an SSL context
7819 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007820void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7821{
7822 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7823}
7824
7825/*
7826 * Setup an SSL context
7827 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007828
7829static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7830{
7831 /* Set the incoming and outgoing record pointers. */
7832#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007833 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007834 {
7835 ssl->out_hdr = ssl->out_buf;
7836 ssl->in_hdr = ssl->in_buf;
7837 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007838 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007839#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007840#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007841 {
7842 ssl->out_hdr = ssl->out_buf + 8;
7843 ssl->in_hdr = ssl->in_buf + 8;
7844 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007845#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007846
7847 /* Derive other internal pointers. */
7848 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007849 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007850}
7851
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007852int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007853 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007854{
Paul Bakker48916f92012-09-16 19:57:18 +00007855 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007856
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007857 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007858
7859 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007860 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007861 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007862
7863 /* Set to NULL in case of an error condition */
7864 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007865
Angus Grattond8213d02016-05-25 20:56:48 +10007866 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7867 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 {
Angus Grattond8213d02016-05-25 20:56:48 +10007869 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007870 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007871 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007872 }
7873
7874 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7875 if( ssl->out_buf == NULL )
7876 {
7877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007878 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007879 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007880 }
7881
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007882 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007883
Paul Bakker48916f92012-09-16 19:57:18 +00007884 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007885 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007886
7887 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007888
7889error:
7890 mbedtls_free( ssl->in_buf );
7891 mbedtls_free( ssl->out_buf );
7892
7893 ssl->conf = NULL;
7894
7895 ssl->in_buf = NULL;
7896 ssl->out_buf = NULL;
7897
7898 ssl->in_hdr = NULL;
7899 ssl->in_ctr = NULL;
7900 ssl->in_len = NULL;
7901 ssl->in_iv = NULL;
7902 ssl->in_msg = NULL;
7903
7904 ssl->out_hdr = NULL;
7905 ssl->out_ctr = NULL;
7906 ssl->out_len = NULL;
7907 ssl->out_iv = NULL;
7908 ssl->out_msg = NULL;
7909
k-stachowiak9f7798e2018-07-31 16:52:32 +02007910 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007911}
7912
7913/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007914 * Reset an initialized and used SSL context for re-use while retaining
7915 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007916 *
7917 * If partial is non-zero, keep data in the input buffer and client ID.
7918 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007919 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007920static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007921{
Paul Bakker48916f92012-09-16 19:57:18 +00007922 int ret;
7923
Hanno Becker7e772132018-08-10 12:38:21 +01007924#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7925 !defined(MBEDTLS_SSL_SRV_C)
7926 ((void) partial);
7927#endif
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007930
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007931 /* Cancel any possibly running timer */
7932 ssl_set_timer( ssl, 0 );
7933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_RENEGOTIATION)
7935 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007936 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007937
7938 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7940 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007941#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007942 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007943
Paul Bakker7eb013f2011-10-06 12:37:39 +00007944 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007945 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007946
7947 ssl->in_msgtype = 0;
7948 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007950 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007951 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007952#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007953#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007954 ssl_dtls_replay_reset( ssl );
7955#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007956
7957 ssl->in_hslen = 0;
7958 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007959
7960 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007961
7962 ssl->out_msgtype = 0;
7963 ssl->out_msglen = 0;
7964 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7966 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007967 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007968#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007969
Hanno Becker19859472018-08-06 09:40:20 +01007970 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7971
Paul Bakker48916f92012-09-16 19:57:18 +00007972 ssl->transform_in = NULL;
7973 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007974
Hanno Becker78640902018-08-13 16:35:15 +01007975 ssl->session_in = NULL;
7976 ssl->session_out = NULL;
7977
Angus Grattond8213d02016-05-25 20:56:48 +10007978 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007979
7980#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007981 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007982#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7983 {
7984 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007985 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007986 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7989 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007990 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7992 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7995 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007996 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007997 }
7998#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007999
Paul Bakker48916f92012-09-16 19:57:18 +00008000 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002 mbedtls_ssl_transform_free( ssl->transform );
8003 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008004 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008005 }
Paul Bakker48916f92012-09-16 19:57:18 +00008006
Paul Bakkerc0463502013-02-14 11:19:38 +01008007 if( ssl->session )
8008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_ssl_session_free( ssl->session );
8010 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008011 ssl->session = NULL;
8012 }
8013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008014#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008015 ssl->alpn_chosen = NULL;
8016#endif
8017
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008018#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008019#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008020 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008021#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008022 {
8023 mbedtls_free( ssl->cli_id );
8024 ssl->cli_id = NULL;
8025 ssl->cli_id_len = 0;
8026 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008027#endif
8028
Paul Bakker48916f92012-09-16 19:57:18 +00008029 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8030 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008031
8032 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008033}
8034
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008035/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008036 * Reset an initialized and used SSL context for re-use while retaining
8037 * all application-set variables, function pointers and data.
8038 */
8039int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8040{
8041 return( ssl_session_reset_int( ssl, 0 ) );
8042}
8043
8044/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008045 * SSL set accessors
8046 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008047void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008049 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008050}
8051
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008052void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008053{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008054 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008055}
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008058void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008059{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008060 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008061}
8062#endif
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008065void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008066{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008067 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008068}
8069#endif
8070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008072
Hanno Becker1841b0a2018-08-24 11:13:57 +01008073void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8074 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008075{
8076 ssl->disable_datagram_packing = !allow_packing;
8077}
8078
8079void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8080 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008081{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008082 conf->hs_timeout_min = min;
8083 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008084}
8085#endif
8086
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008087void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008089 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008090}
8091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008092#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008093void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008094 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008095 void *p_vrfy )
8096{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008097 conf->f_vrfy = f_vrfy;
8098 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008100#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008102void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008103 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008104 void *p_rng )
8105{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008106 conf->f_rng = f_rng;
8107 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008108}
8109
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008110void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008111 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008112 void *p_dbg )
8113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008114 conf->f_dbg = f_dbg;
8115 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008116}
8117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008118void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008119 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008120 mbedtls_ssl_send_t *f_send,
8121 mbedtls_ssl_recv_t *f_recv,
8122 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008123{
8124 ssl->p_bio = p_bio;
8125 ssl->f_send = f_send;
8126 ssl->f_recv = f_recv;
8127 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008128}
8129
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008130#if defined(MBEDTLS_SSL_PROTO_DTLS)
8131void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8132{
8133 ssl->mtu = mtu;
8134}
8135#endif
8136
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008137void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008138{
8139 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008140}
8141
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008142void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8143 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008144 mbedtls_ssl_set_timer_t *f_set_timer,
8145 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008146{
8147 ssl->p_timer = p_timer;
8148 ssl->f_set_timer = f_set_timer;
8149 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008150
8151 /* Make sure we start with no timer running */
8152 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008153}
8154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008155#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008156void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008157 void *p_cache,
8158 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8159 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008160{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008161 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008162 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008163 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167#if defined(MBEDTLS_SSL_CLI_C)
8168int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008169{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008170 int ret;
8171
8172 if( ssl == NULL ||
8173 session == NULL ||
8174 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008175 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008178 }
8179
Hanno Becker58fccf22019-02-06 14:30:46 +00008180 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8181 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008182 return( ret );
8183
Paul Bakker0a597072012-09-25 21:55:46 +00008184 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008185
8186 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008189
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008190void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008191 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008192{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008193 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8194 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8195 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8196 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008197}
8198
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008199void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008200 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008201 int major, int minor )
8202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008203 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008204 return;
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008207 return;
8208
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008209 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008210}
8211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008212#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008213void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008214 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008215{
8216 conf->cert_profile = profile;
8217}
8218
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008219/* Append a new keycert entry to a (possibly empty) list */
8220static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8221 mbedtls_x509_crt *cert,
8222 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008223{
niisato8ee24222018-06-25 19:05:48 +09008224 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008225
niisato8ee24222018-06-25 19:05:48 +09008226 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8227 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008228 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008229
niisato8ee24222018-06-25 19:05:48 +09008230 new_cert->cert = cert;
8231 new_cert->key = key;
8232 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008233
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008234 /* Update head is the list was null, else add to the end */
8235 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008236 {
niisato8ee24222018-06-25 19:05:48 +09008237 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008238 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008239 else
8240 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008241 mbedtls_ssl_key_cert *cur = *head;
8242 while( cur->next != NULL )
8243 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008244 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008245 }
8246
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008247 return( 0 );
8248}
8249
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008250int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008251 mbedtls_x509_crt *own_cert,
8252 mbedtls_pk_context *pk_key )
8253{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008254 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008255}
8256
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008257void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008258 mbedtls_x509_crt *ca_chain,
8259 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008260{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008261 conf->ca_chain = ca_chain;
8262 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008265
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008266#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8267int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8268 mbedtls_x509_crt *own_cert,
8269 mbedtls_pk_context *pk_key )
8270{
8271 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8272 own_cert, pk_key ) );
8273}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008274
8275void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8276 mbedtls_x509_crt *ca_chain,
8277 mbedtls_x509_crl *ca_crl )
8278{
8279 ssl->handshake->sni_ca_chain = ca_chain;
8280 ssl->handshake->sni_ca_crl = ca_crl;
8281}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008282
8283void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8284 int authmode )
8285{
8286 ssl->handshake->sni_authmode = authmode;
8287}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008288#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8289
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008290#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008291/*
8292 * Set EC J-PAKE password for current handshake
8293 */
8294int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8295 const unsigned char *pw,
8296 size_t pw_len )
8297{
8298 mbedtls_ecjpake_role role;
8299
Janos Follath8eb64132016-06-03 15:40:57 +01008300 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8302
8303 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8304 role = MBEDTLS_ECJPAKE_SERVER;
8305 else
8306 role = MBEDTLS_ECJPAKE_CLIENT;
8307
8308 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8309 role,
8310 MBEDTLS_MD_SHA256,
8311 MBEDTLS_ECP_DP_SECP256R1,
8312 pw, pw_len ) );
8313}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008314#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008317int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008318 const unsigned char *psk, size_t psk_len,
8319 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008320{
Paul Bakker6db455e2013-09-18 17:29:31 +02008321 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8325 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008326
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008327 /* Identity len will be encoded on two bytes */
8328 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008329 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008330 {
8331 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8332 }
8333
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008334 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008335 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008336 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008337
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008338 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008339 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008340 conf->psk_len = 0;
8341 }
8342 if( conf->psk_identity != NULL )
8343 {
8344 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008345 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008346 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008347 }
8348
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008349 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8350 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008351 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008352 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008353 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008354 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008355 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008356 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008357 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008358
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008359 conf->psk_len = psk_len;
8360 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008361
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008362 memcpy( conf->psk, psk, conf->psk_len );
8363 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008364
8365 return( 0 );
8366}
8367
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008368int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8369 const unsigned char *psk, size_t psk_len )
8370{
8371 if( psk == NULL || ssl->handshake == NULL )
8372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8373
8374 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8375 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8376
8377 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008378 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008379 mbedtls_platform_zeroize( ssl->handshake->psk,
8380 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008381 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008382 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008383 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008384
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008385 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008387
8388 ssl->handshake->psk_len = psk_len;
8389 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8390
8391 return( 0 );
8392}
8393
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008394void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008396 size_t),
8397 void *p_psk )
8398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008399 conf->f_psk = f_psk;
8400 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008401}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008402#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008403
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008404#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008405
8406#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008407int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008408{
8409 int ret;
8410
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008411 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8412 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8413 {
8414 mbedtls_mpi_free( &conf->dhm_P );
8415 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008416 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008418
8419 return( 0 );
8420}
Hanno Becker470a8c42017-10-04 15:28:46 +01008421#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008422
Hanno Beckera90658f2017-10-04 15:29:08 +01008423int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8424 const unsigned char *dhm_P, size_t P_len,
8425 const unsigned char *dhm_G, size_t G_len )
8426{
8427 int ret;
8428
8429 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8430 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8431 {
8432 mbedtls_mpi_free( &conf->dhm_P );
8433 mbedtls_mpi_free( &conf->dhm_G );
8434 return( ret );
8435 }
8436
8437 return( 0 );
8438}
Paul Bakker5121ce52009-01-03 21:22:43 +00008439
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008440int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008441{
8442 int ret;
8443
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008444 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8445 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8446 {
8447 mbedtls_mpi_free( &conf->dhm_P );
8448 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008449 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008450 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008451
8452 return( 0 );
8453}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008454#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008455
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008456#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8457/*
8458 * Set the minimum length for Diffie-Hellman parameters
8459 */
8460void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8461 unsigned int bitlen )
8462{
8463 conf->dhm_min_bitlen = bitlen;
8464}
8465#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8466
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008467#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008468/*
8469 * Set allowed/preferred hashes for handshake signatures
8470 */
8471void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8472 const int *hashes )
8473{
8474 conf->sig_hashes = hashes;
8475}
Hanno Becker947194e2017-04-07 13:25:49 +01008476#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008477
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008478#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008479/*
8480 * Set the allowed elliptic curves
8481 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008482void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008483 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008484{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008485 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008486}
Hanno Becker947194e2017-04-07 13:25:49 +01008487#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008488
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008489#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008490int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008491{
Hanno Becker947194e2017-04-07 13:25:49 +01008492 /* Initialize to suppress unnecessary compiler warning */
8493 size_t hostname_len = 0;
8494
8495 /* Check if new hostname is valid before
8496 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008497 if( hostname != NULL )
8498 {
8499 hostname_len = strlen( hostname );
8500
8501 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8502 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8503 }
8504
8505 /* Now it's clear that we will overwrite the old hostname,
8506 * so we can free it safely */
8507
8508 if( ssl->hostname != NULL )
8509 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008510 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008511 mbedtls_free( ssl->hostname );
8512 }
8513
8514 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008515
Paul Bakker5121ce52009-01-03 21:22:43 +00008516 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008517 {
8518 ssl->hostname = NULL;
8519 }
8520 else
8521 {
8522 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008523 if( ssl->hostname == NULL )
8524 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008525
Hanno Becker947194e2017-04-07 13:25:49 +01008526 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008527
Hanno Becker947194e2017-04-07 13:25:49 +01008528 ssl->hostname[hostname_len] = '\0';
8529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008530
8531 return( 0 );
8532}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008533#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008534
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008535#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008536void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008538 const unsigned char *, size_t),
8539 void *p_sni )
8540{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008541 conf->f_sni = f_sni;
8542 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008544#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008547int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008548{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008549 size_t cur_len, tot_len;
8550 const char **p;
8551
8552 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008553 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8554 * MUST NOT be truncated."
8555 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008556 */
8557 tot_len = 0;
8558 for( p = protos; *p != NULL; p++ )
8559 {
8560 cur_len = strlen( *p );
8561 tot_len += cur_len;
8562
8563 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008565 }
8566
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008567 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008568
8569 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008570}
8571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008572const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008573{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008574 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008576#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008577
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008578void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008580 conf->max_major_ver = major;
8581 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008582}
8583
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008584void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008585{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008586 conf->min_major_ver = major;
8587 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008588}
8589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008590#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008591void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008592{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008593 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008594}
8595#endif
8596
Janos Follath088ce432017-04-10 12:42:31 +01008597#if defined(MBEDTLS_SSL_SRV_C)
8598void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8599 char cert_req_ca_list )
8600{
8601 conf->cert_req_ca_list = cert_req_ca_list;
8602}
8603#endif
8604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008607{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008608 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008609}
8610#endif
8611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008612#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckerf765ce62019-06-21 13:17:14 +01008613#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008616 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008617}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008618#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
8619#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008620void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008621 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008622{
8623 conf->enforce_extended_master_secret = ems_enf;
8624}
Hanno Beckerf765ce62019-06-21 13:17:14 +01008625#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Hanno Beckeraabbb582019-06-11 13:43:27 +01008626#endif /* !MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008627
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008628#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008629void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008631 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008632}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008633#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008636int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008637{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008639 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008641 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008642 }
8643
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008644 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008645
8646 return( 0 );
8647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008648#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008650#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008651void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008652{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008653 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008654}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008658void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008659{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008660 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008661}
8662#endif
8663
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008664void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008665{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008666 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008667}
8668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008670void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008671{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008672 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008673}
8674
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008675void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008676{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008677 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008678}
8679
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008680void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008681 const unsigned char period[8] )
8682{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008683 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008688#if defined(MBEDTLS_SSL_CLI_C)
8689void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008690{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008691 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008692}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008693#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008694
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008695#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008696void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8697 mbedtls_ssl_ticket_write_t *f_ticket_write,
8698 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8699 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008700{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008701 conf->f_ticket_write = f_ticket_write;
8702 conf->f_ticket_parse = f_ticket_parse;
8703 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008704}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008707
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008708#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8709void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8710 mbedtls_ssl_export_keys_t *f_export_keys,
8711 void *p_export_keys )
8712{
8713 conf->f_export_keys = f_export_keys;
8714 conf->p_export_keys = p_export_keys;
8715}
8716#endif
8717
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008718#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008719void mbedtls_ssl_conf_async_private_cb(
8720 mbedtls_ssl_config *conf,
8721 mbedtls_ssl_async_sign_t *f_async_sign,
8722 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8723 mbedtls_ssl_async_resume_t *f_async_resume,
8724 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008725 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008726{
8727 conf->f_async_sign_start = f_async_sign;
8728 conf->f_async_decrypt_start = f_async_decrypt;
8729 conf->f_async_resume = f_async_resume;
8730 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008731 conf->p_async_config_data = async_config_data;
8732}
8733
Gilles Peskine8f97af72018-04-26 11:46:10 +02008734void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8735{
8736 return( conf->p_async_config_data );
8737}
8738
Gilles Peskine1febfef2018-04-30 11:54:39 +02008739void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008740{
8741 if( ssl->handshake == NULL )
8742 return( NULL );
8743 else
8744 return( ssl->handshake->user_async_ctx );
8745}
8746
Gilles Peskine1febfef2018-04-30 11:54:39 +02008747void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008748 void *ctx )
8749{
8750 if( ssl->handshake != NULL )
8751 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008752}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008753#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008754
Paul Bakker5121ce52009-01-03 21:22:43 +00008755/*
8756 * SSL get accessors
8757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008759{
8760 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8761}
8762
Hanno Becker8b170a02017-10-10 11:51:19 +01008763int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8764{
8765 /*
8766 * Case A: We're currently holding back
8767 * a message for further processing.
8768 */
8769
8770 if( ssl->keep_current_message == 1 )
8771 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008772 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008773 return( 1 );
8774 }
8775
8776 /*
8777 * Case B: Further records are pending in the current datagram.
8778 */
8779
8780#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008781 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008782 ssl->in_left > ssl->next_record_offset )
8783 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008784 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008785 return( 1 );
8786 }
8787#endif /* MBEDTLS_SSL_PROTO_DTLS */
8788
8789 /*
8790 * Case C: A handshake message is being processed.
8791 */
8792
Hanno Becker8b170a02017-10-10 11:51:19 +01008793 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8794 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008795 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008796 return( 1 );
8797 }
8798
8799 /*
8800 * Case D: An application data message is being processed
8801 */
8802 if( ssl->in_offt != NULL )
8803 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008805 return( 1 );
8806 }
8807
8808 /*
8809 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008810 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008811 * we implement support for multiple alerts in single records.
8812 */
8813
8814 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8815 return( 0 );
8816}
8817
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008818uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008819{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008820 if( ssl->session != NULL )
8821 return( ssl->session->verify_result );
8822
8823 if( ssl->session_negotiate != NULL )
8824 return( ssl->session_negotiate->verify_result );
8825
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008826 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008827}
8828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008829const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008830{
Paul Bakker926c8e42013-03-06 10:23:34 +01008831 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008832 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008835}
8836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008837const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008838{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008840 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008841 {
8842 switch( ssl->minor_ver )
8843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008845 return( "DTLSv1.0" );
8846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008848 return( "DTLSv1.2" );
8849
8850 default:
8851 return( "unknown (DTLS)" );
8852 }
8853 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008854 MBEDTLS_SSL_TRANSPORT_ELSE
8855#endif /* MBEDTLS_SSL_PROTO_DTLS */
8856#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008857 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008858 switch( ssl->minor_ver )
8859 {
8860 case MBEDTLS_SSL_MINOR_VERSION_0:
8861 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008862
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008863 case MBEDTLS_SSL_MINOR_VERSION_1:
8864 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008865
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008866 case MBEDTLS_SSL_MINOR_VERSION_2:
8867 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008868
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008869 case MBEDTLS_SSL_MINOR_VERSION_3:
8870 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008871
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008872 default:
8873 return( "unknown" );
8874 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008875 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008876#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008877}
8878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008880{
Hanno Becker3136ede2018-08-17 15:28:19 +01008881 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008883 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008884
Hanno Becker43395762019-05-03 14:46:38 +01008885 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8886
Hanno Becker78640902018-08-13 16:35:15 +01008887 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008888 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008890#if defined(MBEDTLS_ZLIB_SUPPORT)
8891 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008893#endif
8894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008895 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008897 case MBEDTLS_MODE_GCM:
8898 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008899 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008900 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008901 transform_expansion = transform->minlen;
8902 break;
8903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008904 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008905
8906 block_size = mbedtls_cipher_get_block_size(
8907 &transform->cipher_ctx_enc );
8908
Hanno Becker3136ede2018-08-17 15:28:19 +01008909 /* Expansion due to the addition of the MAC. */
8910 transform_expansion += transform->maclen;
8911
8912 /* Expansion due to the addition of CBC padding;
8913 * Theoretically up to 256 bytes, but we never use
8914 * more than the block size of the underlying cipher. */
8915 transform_expansion += block_size;
8916
8917 /* For TLS 1.1 or higher, an explicit IV is added
8918 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008919#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8920 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008921 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008922#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008923
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008924 break;
8925
8926 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008929 }
8930
Hanno Beckera5a2b082019-05-15 14:03:01 +01008931#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008932 if( transform->out_cid_len != 0 )
8933 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008934#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008935
Hanno Becker43395762019-05-03 14:46:38 +01008936 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008937}
8938
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008939#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8940size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8941{
8942 size_t max_len;
8943
8944 /*
8945 * Assume mfl_code is correct since it was checked when set
8946 */
Angus Grattond8213d02016-05-25 20:56:48 +10008947 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008948
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008949 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008950 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008951 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008952 {
Angus Grattond8213d02016-05-25 20:56:48 +10008953 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008954 }
8955
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008956 /* During a handshake, use the value being negotiated */
8957 if( ssl->session_negotiate != NULL &&
8958 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8959 {
8960 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8961 }
8962
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008963 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008964}
8965#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8966
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008967#if defined(MBEDTLS_SSL_PROTO_DTLS)
8968static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8969{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008970 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8971 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8972 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8973 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8974 return ( 0 );
8975
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008976 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8977 return( ssl->mtu );
8978
8979 if( ssl->mtu == 0 )
8980 return( ssl->handshake->mtu );
8981
8982 return( ssl->mtu < ssl->handshake->mtu ?
8983 ssl->mtu : ssl->handshake->mtu );
8984}
8985#endif /* MBEDTLS_SSL_PROTO_DTLS */
8986
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008987int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8988{
8989 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8990
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008991#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8992 !defined(MBEDTLS_SSL_PROTO_DTLS)
8993 (void) ssl;
8994#endif
8995
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008996#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8997 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8998
8999 if( max_len > mfl )
9000 max_len = mfl;
9001#endif
9002
9003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009004 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009005 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009006 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009007 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9008 const size_t overhead = (size_t) ret;
9009
9010 if( ret < 0 )
9011 return( ret );
9012
9013 if( mtu <= overhead )
9014 {
9015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9016 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9017 }
9018
9019 if( max_len > mtu - overhead )
9020 max_len = mtu - overhead;
9021 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009022#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009023
Hanno Becker0defedb2018-08-10 12:35:02 +01009024#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9025 !defined(MBEDTLS_SSL_PROTO_DTLS)
9026 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009027#endif
9028
9029 return( (int) max_len );
9030}
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032#if defined(MBEDTLS_X509_CRT_PARSE_C)
9033const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009034{
9035 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009036 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009037
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009038#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009039 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009040#else
9041 return( NULL );
9042#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009046#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009047int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9048 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009049{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009050 if( ssl == NULL ||
9051 dst == NULL ||
9052 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009053 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009056 }
9057
Hanno Becker58fccf22019-02-06 14:30:46 +00009058 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009060#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009061
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009062const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9063{
9064 if( ssl == NULL )
9065 return( NULL );
9066
9067 return( ssl->session );
9068}
9069
Paul Bakker5121ce52009-01-03 21:22:43 +00009070/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009071 * Define ticket header determining Mbed TLS version
9072 * and structure of the ticket.
9073 */
9074
Hanno Becker41527622019-05-16 12:50:45 +01009075/*
Hanno Becker26829e92019-05-28 14:30:45 +01009076 * Define bitflag determining compile-time settings influencing
9077 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009078 */
9079
Hanno Becker26829e92019-05-28 14:30:45 +01009080#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009081#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009082#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009083#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009084#endif /* MBEDTLS_HAVE_TIME */
9085
9086#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009087#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009088#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009089#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009090#endif /* MBEDTLS_X509_CRT_PARSE_C */
9091
9092#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009093#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009094#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009095#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009096#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9097
9098#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009099#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009100#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009101#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009102#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9103
9104#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009105#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009106#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009107#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009108#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9109
9110#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009111#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009112#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009113#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009114#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9115
Hanno Becker41527622019-05-16 12:50:45 +01009116#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9117#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9118#else
9119#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9120#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9121
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009122#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9123#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9124#else
9125#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9126#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9127
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009128#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9129#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9130#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9131#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9132#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9133#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9134#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009135#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009136
Hanno Becker26829e92019-05-28 14:30:45 +01009137#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009138 ( (uint16_t) ( \
9139 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9140 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9141 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9142 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9143 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9144 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009145 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9146 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009147
Hanno Becker557fe9f2019-05-16 12:41:07 +01009148static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009149 MBEDTLS_VERSION_MAJOR,
9150 MBEDTLS_VERSION_MINOR,
9151 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009152 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9153 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009154};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009155
9156/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009157 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009158 * (in the presentation language of TLS, RFC 8446 section 3)
9159 *
Hanno Becker26829e92019-05-28 14:30:45 +01009160 * opaque mbedtls_version[3]; // major, minor, patch
9161 * opaque session_format[2]; // version-specific 16-bit field determining
9162 * // the format of the remaining
9163 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009164 *
9165 * Note: When updating the format, remember to keep
9166 * these version+format bytes.
9167 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009168 * // In this version, `session_format` determines
9169 * // the setting of those compile-time
9170 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009171 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009172 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009173 * uint8 ciphersuite[2]; // defined by the standard
9174 * uint8 compression; // 0 or 1
9175 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009176 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009177 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009178 * uint32 verify_result;
Hanno Becker0528f822019-06-18 12:45:31 +01009179 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9180 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9181 * case disabled: uint8_t peer_cert_digest_type;
9182 * opaque peer_cert_digest<0..2^8-1>;
9183 * }
Hanno Becker26829e92019-05-28 14:30:45 +01009184 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009185 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009186 * uint8 mfl_code; // up to 255 according to standard
9187 * uint8 trunc_hmac; // 0 or 1
9188 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009189 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009190 * The order is the same as in the definition of the structure, except
9191 * verify_result is put before peer_cert so that all mandatory fields come
9192 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009193 */
9194int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9195 unsigned char *buf,
9196 size_t buf_len,
9197 size_t *olen )
9198{
9199 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009200 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009201#if defined(MBEDTLS_HAVE_TIME)
9202 uint64_t start;
9203#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009204#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009205#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009206 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009207#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009208#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009209
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009210 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009211 * Add version identifier
9212 */
9213
9214 used += sizeof( ssl_serialized_session_header );
9215
9216 if( used <= buf_len )
9217 {
9218 memcpy( p, ssl_serialized_session_header,
9219 sizeof( ssl_serialized_session_header ) );
9220 p += sizeof( ssl_serialized_session_header );
9221 }
9222
9223 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009224 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009225 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009226#if defined(MBEDTLS_HAVE_TIME)
9227 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009228
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009229 if( used <= buf_len )
9230 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009231 start = (uint64_t) session->start;
9232
9233 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9234 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9235 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9236 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9237 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9238 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9239 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9240 *p++ = (unsigned char)( ( start ) & 0xFF );
9241 }
9242#endif /* MBEDTLS_HAVE_TIME */
9243
9244 /*
9245 * Basic mandatory fields
9246 */
9247 used += 2 /* ciphersuite */
9248 + 1 /* compression */
9249 + 1 /* id_len */
9250 + sizeof( session->id )
9251 + sizeof( session->master )
9252 + 4; /* verify_result */
9253
9254 if( used <= buf_len )
9255 {
9256 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9257 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9258
9259 *p++ = (unsigned char)( session->compression & 0xFF );
9260
9261 *p++ = (unsigned char)( session->id_len & 0xFF );
9262 memcpy( p, session->id, 32 );
9263 p += 32;
9264
9265 memcpy( p, session->master, 48 );
9266 p += 48;
9267
9268 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9269 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9270 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9271 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009272 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009273
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009274 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009275 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009276 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009277#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009278#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009279 if( session->peer_cert == NULL )
9280 cert_len = 0;
9281 else
9282 cert_len = session->peer_cert->raw.len;
9283
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009284 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009285
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009286 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009287 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009288 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9289 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9290 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9291
9292 if( session->peer_cert != NULL )
9293 {
9294 memcpy( p, session->peer_cert->raw.p, cert_len );
9295 p += cert_len;
9296 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009297 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009298
Hanno Becker5882dd02019-06-06 16:25:57 +01009299#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009300 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009301 if( session->peer_cert_digest != NULL )
9302 {
9303 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9304 if( used <= buf_len )
9305 {
9306 *p++ = (unsigned char) session->peer_cert_digest_type;
9307 *p++ = (unsigned char) session->peer_cert_digest_len;
9308 memcpy( p, session->peer_cert_digest,
9309 session->peer_cert_digest_len );
9310 p += session->peer_cert_digest_len;
9311 }
9312 }
9313 else
9314 {
9315 used += 2;
9316 if( used <= buf_len )
9317 {
9318 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9319 *p++ = 0;
9320 }
9321 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009322#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009323#endif /* MBEDTLS_X509_CRT_PARSE_C */
9324
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009325 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009326 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009327 */
9328#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009329 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009330
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009331 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009332 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009333 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9334 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9335 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9336
9337 if( session->ticket != NULL )
9338 {
9339 memcpy( p, session->ticket, session->ticket_len );
9340 p += session->ticket_len;
9341 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009342
9343 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9344 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9345 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9346 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009347 }
9348#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9349
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009350 /*
9351 * Misc extension-related info
9352 */
9353#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9354 used += 1;
9355
9356 if( used <= buf_len )
9357 *p++ = session->mfl_code;
9358#endif
9359
9360#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9361 used += 1;
9362
9363 if( used <= buf_len )
9364 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9365#endif
9366
9367#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9368 used += 1;
9369
9370 if( used <= buf_len )
9371 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9372#endif
9373
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009374 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009375 *olen = used;
9376
9377 if( used > buf_len )
9378 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009379
9380 return( 0 );
9381}
9382
9383/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009384 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009385 *
9386 * This internal version is wrapped by a public function that cleans up in
9387 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009388 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009389static int ssl_session_load( mbedtls_ssl_session *session,
9390 const unsigned char *buf,
9391 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009392{
9393 const unsigned char *p = buf;
9394 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009395#if defined(MBEDTLS_HAVE_TIME)
9396 uint64_t start;
9397#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009398#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009399#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009400 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009401#endif
9402#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009403
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009404 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009405 * Check version identifier
9406 */
9407
9408 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009409 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009410
9411 if( memcmp( p, ssl_serialized_session_header,
9412 sizeof( ssl_serialized_session_header ) ) != 0 )
9413 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009414 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009415 }
9416 p += sizeof( ssl_serialized_session_header );
9417
9418 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009419 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009420 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009421#if defined(MBEDTLS_HAVE_TIME)
9422 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009423 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9424
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009425 start = ( (uint64_t) p[0] << 56 ) |
9426 ( (uint64_t) p[1] << 48 ) |
9427 ( (uint64_t) p[2] << 40 ) |
9428 ( (uint64_t) p[3] << 32 ) |
9429 ( (uint64_t) p[4] << 24 ) |
9430 ( (uint64_t) p[5] << 16 ) |
9431 ( (uint64_t) p[6] << 8 ) |
9432 ( (uint64_t) p[7] );
9433 p += 8;
9434
9435 session->start = (time_t) start;
9436#endif /* MBEDTLS_HAVE_TIME */
9437
9438 /*
9439 * Basic mandatory fields
9440 */
9441 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9442 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9443
9444 session->ciphersuite = ( p[0] << 8 ) | p[1];
9445 p += 2;
9446
9447 session->compression = *p++;
9448
9449 session->id_len = *p++;
9450 memcpy( session->id, p, 32 );
9451 p += 32;
9452
9453 memcpy( session->master, p, 48 );
9454 p += 48;
9455
9456 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9457 ( (uint32_t) p[1] << 16 ) |
9458 ( (uint32_t) p[2] << 8 ) |
9459 ( (uint32_t) p[3] );
9460 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009461
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009462 /* Immediately clear invalid pointer values that have been read, in case
9463 * we exit early before we replaced them with valid ones. */
9464#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009465#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009466 session->peer_cert = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009467#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009468 session->peer_cert_digest = NULL;
Hanno Becker5882dd02019-06-06 16:25:57 +01009469#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009470#endif
9471#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9472 session->ticket = NULL;
9473#endif
9474
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009475 /*
9476 * Peer certificate
9477 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009478#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009480 if( 3 > (size_t)( end - p ) )
9481 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9482
9483 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9484 p += 3;
9485
9486 if( cert_len == 0 )
9487 {
9488 session->peer_cert = NULL;
9489 }
9490 else
9491 {
9492 int ret;
9493
9494 if( cert_len > (size_t)( end - p ) )
9495 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9496
9497 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9498
9499 if( session->peer_cert == NULL )
9500 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9501
9502 mbedtls_x509_crt_init( session->peer_cert );
9503
9504 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9505 p, cert_len ) ) != 0 )
9506 {
9507 mbedtls_x509_crt_free( session->peer_cert );
9508 mbedtls_free( session->peer_cert );
9509 session->peer_cert = NULL;
9510 return( ret );
9511 }
9512
9513 p += cert_len;
9514 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009515#elif defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009516 /* Deserialize CRT digest from the end of the ticket. */
9517 if( 2 > (size_t)( end - p ) )
9518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9519
9520 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9521 session->peer_cert_digest_len = (size_t) *p++;
9522
9523 if( session->peer_cert_digest_len != 0 )
9524 {
Hanno Becker2326d202019-06-06 14:54:55 +01009525 const mbedtls_md_info_t *md_info =
9526 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9527 if( md_info == NULL )
9528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9529 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9531
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009532 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9534
9535 session->peer_cert_digest =
9536 mbedtls_calloc( 1, session->peer_cert_digest_len );
9537 if( session->peer_cert_digest == NULL )
9538 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9539
9540 memcpy( session->peer_cert_digest, p,
9541 session->peer_cert_digest_len );
9542 p += session->peer_cert_digest_len;
9543 }
Hanno Becker5882dd02019-06-06 16:25:57 +01009544#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009545#endif /* MBEDTLS_X509_CRT_PARSE_C */
9546
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009547 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009548 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009549 */
9550#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9551 if( 3 > (size_t)( end - p ) )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
9554 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9555 p += 3;
9556
9557 if( session->ticket_len != 0 )
9558 {
9559 if( session->ticket_len > (size_t)( end - p ) )
9560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9561
9562 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9563 if( session->ticket == NULL )
9564 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9565
9566 memcpy( session->ticket, p, session->ticket_len );
9567 p += session->ticket_len;
9568 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009569
9570 if( 4 > (size_t)( end - p ) )
9571 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9572
9573 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9574 ( (uint32_t) p[1] << 16 ) |
9575 ( (uint32_t) p[2] << 8 ) |
9576 ( (uint32_t) p[3] );
9577 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009578#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9579
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009580 /*
9581 * Misc extension-related info
9582 */
9583#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9584 if( 1 > (size_t)( end - p ) )
9585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9586
9587 session->mfl_code = *p++;
9588#endif
9589
9590#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9591 if( 1 > (size_t)( end - p ) )
9592 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9593
9594 session->trunc_hmac = *p++;
9595#endif
9596
9597#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9598 if( 1 > (size_t)( end - p ) )
9599 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9600
9601 session->encrypt_then_mac = *p++;
9602#endif
9603
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009604 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009605 if( p != end )
9606 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9607
9608 return( 0 );
9609}
9610
9611/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009612 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009613 */
9614int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9615 const unsigned char *buf,
9616 size_t len )
9617{
9618 int ret = ssl_session_load( session, buf, len );
9619
9620 if( ret != 0 )
9621 mbedtls_ssl_session_free( session );
9622
9623 return( ret );
9624}
9625
9626/*
Paul Bakker1961b702013-01-25 14:49:24 +01009627 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009630{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009632
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009633 if( ssl == NULL || ssl->conf == NULL )
9634 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009637 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009639#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009641 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009643#endif
9644
Paul Bakker1961b702013-01-25 14:49:24 +01009645 return( ret );
9646}
9647
9648/*
9649 * Perform the SSL handshake
9650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009652{
9653 int ret = 0;
9654
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009655 if( ssl == NULL || ssl->conf == NULL )
9656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009663
9664 if( ret != 0 )
9665 break;
9666 }
9667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009669
9670 return( ret );
9671}
9672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673#if defined(MBEDTLS_SSL_RENEGOTIATION)
9674#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009675/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009676 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009679{
9680 int ret;
9681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009683
9684 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9686 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009687
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009688 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009689 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009691 return( ret );
9692 }
9693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009695
9696 return( 0 );
9697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009699
9700/*
9701 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 * - any side: calling mbedtls_ssl_renegotiate(),
9703 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9704 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009705 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009706 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009710{
9711 int ret;
9712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009714
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009715 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9716 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009717
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009718 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9719 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009721 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009723 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009724 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009725 ssl->handshake->out_msg_seq = 1;
9726 else
9727 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009728 }
9729#endif
9730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9732 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009737 return( ret );
9738 }
9739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009741
9742 return( 0 );
9743}
9744
9745/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009746 * Renegotiate current connection on client,
9747 * or request renegotiation on server
9748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009752
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009753 if( ssl == NULL || ssl->conf == NULL )
9754 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009757 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009758 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9761 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009764
9765 /* Did we already try/start sending HelloRequest? */
9766 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009768
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009769 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009770 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009774 /*
9775 * On client, either start the renegotiation process or,
9776 * if already in progress, continue the handshake
9777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009782
9783 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009786 return( ret );
9787 }
9788 }
9789 else
9790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009794 return( ret );
9795 }
9796 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009798
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009799 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009800}
9801
9802/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009803 * Check record counters and renegotiate if they're above the limit.
9804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009806{
Andres AG2196c7f2016-12-15 17:01:16 +00009807 size_t ep_len = ssl_ep_len( ssl );
9808 int in_ctr_cmp;
9809 int out_ctr_cmp;
9810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9812 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009813 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009814 {
9815 return( 0 );
9816 }
9817
Andres AG2196c7f2016-12-15 17:01:16 +00009818 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9819 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009820 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009821 ssl->conf->renego_period + ep_len, 8 - ep_len );
9822
9823 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009824 {
9825 return( 0 );
9826 }
9827
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009832
9833/*
9834 * Receive application data decrypted from the SSL layer
9835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009837{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009838 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009839 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009840
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009841 if( ssl == NULL || ssl->conf == NULL )
9842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009847 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009850 return( ret );
9851
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009852 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009854 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009855 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009856 return( ret );
9857 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009858 }
9859#endif
9860
Hanno Becker4a810fb2017-05-24 16:27:30 +01009861 /*
9862 * Check if renegotiation is necessary and/or handshake is
9863 * in process. If yes, perform/continue, and fall through
9864 * if an unexpected packet is received while the client
9865 * is waiting for the ServerHello.
9866 *
9867 * (There is no equivalent to the last condition on
9868 * the server-side as it is not treated as within
9869 * a handshake while waiting for the ClientHello
9870 * after a renegotiation request.)
9871 */
9872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009874 ret = ssl_check_ctr_renegotiate( ssl );
9875 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9876 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009879 return( ret );
9880 }
9881#endif
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009885 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009886 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9887 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009889 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009890 return( ret );
9891 }
9892 }
9893
Hanno Beckere41158b2017-10-23 13:30:32 +01009894 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009895 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009897 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009898 if( ssl->f_get_timer != NULL &&
9899 ssl->f_get_timer( ssl->p_timer ) == -1 )
9900 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009901 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009902 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009903
Hanno Becker327c93b2018-08-15 13:56:18 +01009904 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009905 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009906 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9907 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009908
Hanno Becker4a810fb2017-05-24 16:27:30 +01009909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9910 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009911 }
9912
9913 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009915 {
9916 /*
9917 * OpenSSL sends empty messages to randomize the IV
9918 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009919 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009922 return( 0 );
9923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009925 return( ret );
9926 }
9927 }
9928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009932
Hanno Becker4a810fb2017-05-24 16:27:30 +01009933 /*
9934 * - For client-side, expect SERVER_HELLO_REQUEST.
9935 * - For server-side, expect CLIENT_HELLO.
9936 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9937 */
9938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009940 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009942 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009945
9946 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009947#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009948 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009949 {
9950 continue;
9951 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009952 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009953#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009954#if defined(MBEDTLS_SSL_PROTO_TLS)
9955 {
9956 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9957 }
9958#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009959 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009960#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009961
Hanno Becker4a810fb2017-05-24 16:27:30 +01009962#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009963 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009967
9968 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009970 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009971 {
9972 continue;
9973 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009974 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009975#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009976#if defined(MBEDTLS_SSL_PROTO_TLS)
9977 {
9978 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9979 }
9980#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009981 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009982#endif /* MBEDTLS_SSL_SRV_C */
9983
Hanno Becker21df7f92017-10-17 11:03:26 +01009984#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009985 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009986 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9987 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9988 ssl->conf->allow_legacy_renegotiation ==
9989 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9990 {
9991 /*
9992 * Accept renegotiation request
9993 */
Paul Bakker48916f92012-09-16 19:57:18 +00009994
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009995 /* DTLS clients need to know renego is server-initiated */
9996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009997 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009998 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9999 {
10000 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10001 }
10002#endif
10003 ret = ssl_start_renegotiation( ssl );
10004 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10005 ret != 0 )
10006 {
10007 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10008 return( ret );
10009 }
10010 }
10011 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010012#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010013 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010014 /*
10015 * Refuse renegotiation
10016 */
10017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020#if defined(MBEDTLS_SSL_PROTO_SSL3)
10021 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010022 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010023 /* SSLv3 does not have a "no_renegotiation" warning, so
10024 we send a fatal alert and abort the connection. */
10025 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10026 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10027 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010028 }
10029 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10031#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10032 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10033 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10036 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10037 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010038 {
10039 return( ret );
10040 }
Paul Bakker48916f92012-09-16 19:57:18 +000010041 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010042 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10044 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010048 }
Paul Bakker48916f92012-09-16 19:57:18 +000010049 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010050
Hanno Becker90333da2017-10-10 11:27:13 +010010051 /* At this point, we don't know whether the renegotiation has been
10052 * completed or not. The cases to consider are the following:
10053 * 1) The renegotiation is complete. In this case, no new record
10054 * has been read yet.
10055 * 2) The renegotiation is incomplete because the client received
10056 * an application data record while awaiting the ServerHello.
10057 * 3) The renegotiation is incomplete because the client received
10058 * a non-handshake, non-application data message while awaiting
10059 * the ServerHello.
10060 * In each of these case, looping will be the proper action:
10061 * - For 1), the next iteration will read a new record and check
10062 * if it's application data.
10063 * - For 2), the loop condition isn't satisfied as application data
10064 * is present, hence continue is the same as break
10065 * - For 3), the loop condition is satisfied and read_record
10066 * will re-deliver the message that was held back by the client
10067 * when expecting the ServerHello.
10068 */
10069 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010070 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010071#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010073 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010074 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010075 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010076 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010079 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010081 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010082 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10087 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010090 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010091 }
10092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10096 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010097 }
10098
10099 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010100
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010101 /* We're going to return something now, cancel timer,
10102 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010104 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010105
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010106#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010107 /* If we requested renego but received AppData, resend HelloRequest.
10108 * Do it now, after setting in_offt, to avoid taking this branch
10109 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010111 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010113 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010114 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010117 return( ret );
10118 }
10119 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010121#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010122 }
10123
10124 n = ( len < ssl->in_msglen )
10125 ? len : ssl->in_msglen;
10126
10127 memcpy( buf, ssl->in_offt, n );
10128 ssl->in_msglen -= n;
10129
10130 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010131 {
10132 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010133 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010134 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010135 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010136 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010137 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010138 /* more data available */
10139 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010140 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010143
Paul Bakker23986e52011-04-24 08:57:21 +000010144 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010145}
10146
10147/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010148 * Send application data to be encrypted by the SSL layer, taking care of max
10149 * fragment length and buffer size.
10150 *
10151 * According to RFC 5246 Section 6.2.1:
10152 *
10153 * Zero-length fragments of Application data MAY be sent as they are
10154 * potentially useful as a traffic analysis countermeasure.
10155 *
10156 * Therefore, it is possible that the input message length is 0 and the
10157 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010158 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010159static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010161{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010162 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10163 const size_t max_len = (size_t) ret;
10164
10165 if( ret < 0 )
10166 {
10167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10168 return( ret );
10169 }
10170
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010171 if( len > max_len )
10172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010174 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010177 "maximum fragment length: %d > %d",
10178 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010180 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010181 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010182#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010183#if defined(MBEDTLS_SSL_PROTO_TLS)
10184 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010185 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010186 }
10187#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010188 }
Paul Bakker887bd502011-06-08 13:10:54 +000010189
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 if( ssl->out_left != 0 )
10191 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010192 /*
10193 * The user has previously tried to send the data and
10194 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10195 * written. In this case, we expect the high-level write function
10196 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201 return( ret );
10202 }
10203 }
Paul Bakker887bd502011-06-08 13:10:54 +000010204 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010205 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010206 /*
10207 * The user is trying to send a message the first time, so we need to
10208 * copy the data into the internal buffers and setup the data structure
10209 * to keep track of partial writes
10210 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010211 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010213 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010214
Hanno Becker67bc7c32018-08-06 11:33:50 +010010215 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010218 return( ret );
10219 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010220 }
10221
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010222 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010223}
10224
10225/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010226 * Write application data, doing 1/n-1 splitting if necessary.
10227 *
10228 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010229 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010230 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010233static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010234 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010235{
10236 int ret;
10237
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010238 if( ssl->conf->cbc_record_splitting ==
10239 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010240 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10242 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10243 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010244 {
10245 return( ssl_write_real( ssl, buf, len ) );
10246 }
10247
10248 if( ssl->split_done == 0 )
10249 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010250 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010251 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010252 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010253 }
10254
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010255 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10256 return( ret );
10257 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010258
10259 return( ret + 1 );
10260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010262
10263/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010264 * Write application data (public-facing wrapper)
10265 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010266int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010267{
10268 int ret;
10269
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010271
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010272 if( ssl == NULL || ssl->conf == NULL )
10273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10274
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010275#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010276 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10277 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010279 return( ret );
10280 }
10281#endif
10282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010285 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010286 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010288 return( ret );
10289 }
10290 }
10291
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010292#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010293 ret = ssl_write_split( ssl, buf, len );
10294#else
10295 ret = ssl_write_real( ssl, buf, len );
10296#endif
10297
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010299
10300 return( ret );
10301}
10302
10303/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010304 * Notify the peer that the connection is being closed
10305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010307{
10308 int ret;
10309
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010310 if( ssl == NULL || ssl->conf == NULL )
10311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010314
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010315 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10321 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10322 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 return( ret );
10326 }
10327 }
10328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010330
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010331 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010332}
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010335{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010336 if( transform == NULL )
10337 return;
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010340 deflateEnd( &transform->ctx_deflate );
10341 inflateEnd( &transform->ctx_inflate );
10342#endif
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10345 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010346
Hanno Becker92231322018-01-03 15:32:51 +000010347#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 mbedtls_md_free( &transform->md_ctx_enc );
10349 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010350#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010351
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010352 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010353}
10354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_X509_CRT_PARSE_C)
10356static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010359
10360 while( cur != NULL )
10361 {
10362 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010364 cur = next;
10365 }
10366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010368
Hanno Becker0271f962018-08-16 13:23:47 +010010369#if defined(MBEDTLS_SSL_PROTO_DTLS)
10370
10371static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10372{
10373 unsigned offset;
10374 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10375
10376 if( hs == NULL )
10377 return;
10378
Hanno Becker283f5ef2018-08-24 09:34:47 +010010379 ssl_free_buffered_record( ssl );
10380
Hanno Becker0271f962018-08-16 13:23:47 +010010381 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010382 ssl_buffering_free_slot( ssl, offset );
10383}
10384
10385static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10386 uint8_t slot )
10387{
10388 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10389 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010390
10391 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10392 return;
10393
Hanno Beckere605b192018-08-21 15:59:07 +010010394 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010395 {
Hanno Beckere605b192018-08-21 15:59:07 +010010396 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010397 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010398 mbedtls_free( hs_buf->data );
10399 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010400 }
10401}
10402
10403#endif /* MBEDTLS_SSL_PROTO_DTLS */
10404
Gilles Peskine9b562d52018-04-25 20:32:43 +020010405void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010406{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010407 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10408
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010409 if( handshake == NULL )
10410 return;
10411
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010412#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10413 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10414 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010415 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010416 handshake->async_in_progress = 0;
10417 }
10418#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10419
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010420#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10421 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10422 mbedtls_md5_free( &handshake->fin_md5 );
10423 mbedtls_sha1_free( &handshake->fin_sha1 );
10424#endif
10425#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10426#if defined(MBEDTLS_SHA256_C)
10427 mbedtls_sha256_free( &handshake->fin_sha256 );
10428#endif
10429#if defined(MBEDTLS_SHA512_C)
10430 mbedtls_sha512_free( &handshake->fin_sha512 );
10431#endif
10432#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_DHM_C)
10435 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437#if defined(MBEDTLS_ECDH_C)
10438 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010439#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010440#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010441 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010442#if defined(MBEDTLS_SSL_CLI_C)
10443 mbedtls_free( handshake->ecjpake_cache );
10444 handshake->ecjpake_cache = NULL;
10445 handshake->ecjpake_cache_len = 0;
10446#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010447#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010448
Janos Follath4ae5c292016-02-10 11:27:43 +000010449#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10450 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010451 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010453#endif
10454
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010455#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10456 if( handshake->psk != NULL )
10457 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010458 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010459 mbedtls_free( handshake->psk );
10460 }
10461#endif
10462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10464 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010465 /*
10466 * Free only the linked list wrapper, not the keys themselves
10467 * since the belong to the SNI callback
10468 */
10469 if( handshake->sni_key_cert != NULL )
10470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010472
10473 while( cur != NULL )
10474 {
10475 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010477 cur = next;
10478 }
10479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010481
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010482#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010483 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010484 if( handshake->ecrs_peer_cert != NULL )
10485 {
10486 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10487 mbedtls_free( handshake->ecrs_peer_cert );
10488 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010489#endif
10490
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010491#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10492 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10493 mbedtls_pk_free( &handshake->peer_pubkey );
10494#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496#if defined(MBEDTLS_SSL_PROTO_DTLS)
10497 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010498 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010499 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010500#endif
10501
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010502 mbedtls_platform_zeroize( handshake,
10503 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010504}
10505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010507{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010508 if( session == NULL )
10509 return;
10510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010512 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010513#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010514
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010515#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010516 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010517#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010518
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010519 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010520}
10521
Paul Bakker5121ce52009-01-03 21:22:43 +000010522/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010523 * Serialize a full SSL context
10524 */
10525int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10526 unsigned char *buf,
10527 size_t buf_len,
10528 size_t *olen )
10529{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010530 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010531 (void) ssl;
10532
10533 if( buf != NULL )
10534 memset( buf, 0, buf_len );
10535
10536 *olen = 0;
10537
10538 return( 0 );
10539}
10540
10541/*
10542 * Deserialize a full SSL context
10543 */
10544int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10545 const unsigned char *buf,
10546 size_t len )
10547{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010548 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010549 (void) ssl;
10550 (void) buf;
10551 (void) len;
10552
10553 return( 0 );
10554}
10555
10556/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010557 * Free an SSL context
10558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010559void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010560{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010561 if( ssl == NULL )
10562 return;
10563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010565
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010566 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010567 {
Angus Grattond8213d02016-05-25 20:56:48 +100010568 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010570 }
10571
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010572 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010573 {
Angus Grattond8213d02016-05-25 20:56:48 +100010574 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010575 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010576 }
10577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010578#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010579 if( ssl->compress_buf != NULL )
10580 {
Angus Grattond8213d02016-05-25 20:56:48 +100010581 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010582 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010583 }
10584#endif
10585
Paul Bakker48916f92012-09-16 19:57:18 +000010586 if( ssl->transform )
10587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588 mbedtls_ssl_transform_free( ssl->transform );
10589 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010590 }
10591
10592 if( ssl->handshake )
10593 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010594 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10596 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010598 mbedtls_free( ssl->handshake );
10599 mbedtls_free( ssl->transform_negotiate );
10600 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010601 }
10602
Paul Bakkerc0463502013-02-14 11:19:38 +010010603 if( ssl->session )
10604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 mbedtls_ssl_session_free( ssl->session );
10606 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010607 }
10608
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010609#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010610 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010611 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010612 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010613 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010614 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010615#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10618 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10621 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010622 }
10623#endif
10624
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010625#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010626 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010627#endif
10628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010630
Paul Bakker86f04f42013-02-14 11:20:09 +010010631 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010632 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010633}
10634
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010635/*
10636 * Initialze mbedtls_ssl_config
10637 */
10638void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10639{
10640 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010641
10642#if !defined(MBEDTLS_SSL_PROTO_TLS)
10643 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10644#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010645}
10646
Simon Butcherc97b6972015-12-27 23:48:17 +000010647#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010648static int ssl_preset_default_hashes[] = {
10649#if defined(MBEDTLS_SHA512_C)
10650 MBEDTLS_MD_SHA512,
10651 MBEDTLS_MD_SHA384,
10652#endif
10653#if defined(MBEDTLS_SHA256_C)
10654 MBEDTLS_MD_SHA256,
10655 MBEDTLS_MD_SHA224,
10656#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010657#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010658 MBEDTLS_MD_SHA1,
10659#endif
10660 MBEDTLS_MD_NONE
10661};
Simon Butcherc97b6972015-12-27 23:48:17 +000010662#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010663
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010664static int ssl_preset_suiteb_ciphersuites[] = {
10665 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10666 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10667 0
10668};
10669
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010670#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010671static int ssl_preset_suiteb_hashes[] = {
10672 MBEDTLS_MD_SHA256,
10673 MBEDTLS_MD_SHA384,
10674 MBEDTLS_MD_NONE
10675};
10676#endif
10677
10678#if defined(MBEDTLS_ECP_C)
10679static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10680 MBEDTLS_ECP_DP_SECP256R1,
10681 MBEDTLS_ECP_DP_SECP384R1,
10682 MBEDTLS_ECP_DP_NONE
10683};
10684#endif
10685
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010686/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010687 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010688 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010689int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010690 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010691{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010692#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010693 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010694#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010695
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010696 /* Use the functions here so that they are covered in tests,
10697 * but otherwise access member directly for efficiency */
10698 mbedtls_ssl_conf_endpoint( conf, endpoint );
10699 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010700
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010701 /*
10702 * Things that are common to all presets
10703 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010704#if defined(MBEDTLS_SSL_CLI_C)
10705 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10706 {
10707 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10708#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10709 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10710#endif
10711 }
10712#endif
10713
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010714#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010716#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010717
10718#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10719 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10720#endif
10721
10722#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Hanno Beckeraabbb582019-06-11 13:43:27 +010010723#if !defined(MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010725#endif /* !MBEDTLS_SSL_CONF_EXTENDED_MASTER_SECRET */
10726#if !defined(MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET)
Jarno Lamsad9382f82019-06-10 10:27:14 +030010727 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010728 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Hanno Beckeraabbb582019-06-11 13:43:27 +010010729#endif /* !MBEDTLS_SSL_CONF_ENFORCE_EXTENDED_MASTER_SECRET */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730#endif
10731
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010732#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10733 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10734#endif
10735
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010736#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010737 conf->f_cookie_write = ssl_cookie_write_dummy;
10738 conf->f_cookie_check = ssl_cookie_check_dummy;
10739#endif
10740
10741#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10742 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10743#endif
10744
Janos Follath088ce432017-04-10 12:42:31 +010010745#if defined(MBEDTLS_SSL_SRV_C)
10746 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10747#endif
10748
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010749#if defined(MBEDTLS_SSL_PROTO_DTLS)
10750 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10751 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10752#endif
10753
10754#if defined(MBEDTLS_SSL_RENEGOTIATION)
10755 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010756 memset( conf->renego_period, 0x00, 2 );
10757 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010758#endif
10759
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010760#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10761 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10762 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010763 const unsigned char dhm_p[] =
10764 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10765 const unsigned char dhm_g[] =
10766 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10767
Hanno Beckera90658f2017-10-04 15:29:08 +010010768 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10769 dhm_p, sizeof( dhm_p ),
10770 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010771 {
10772 return( ret );
10773 }
10774 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010775#endif
10776
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010777 /*
10778 * Preset-specific defaults
10779 */
10780 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010781 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010782 /*
10783 * NSA Suite B
10784 */
10785 case MBEDTLS_SSL_PRESET_SUITEB:
10786 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10787 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10788 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10789 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10790
10791 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10792 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10793 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10794 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10795 ssl_preset_suiteb_ciphersuites;
10796
10797#if defined(MBEDTLS_X509_CRT_PARSE_C)
10798 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010799#endif
10800
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010801#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010802 conf->sig_hashes = ssl_preset_suiteb_hashes;
10803#endif
10804
10805#if defined(MBEDTLS_ECP_C)
10806 conf->curve_list = ssl_preset_suiteb_curves;
10807#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010808 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010809
10810 /*
10811 * Default
10812 */
10813 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010814 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10815 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10816 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10817 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10818 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10819 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10820 MBEDTLS_SSL_MIN_MINOR_VERSION :
10821 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010822 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10823 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10824
10825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010826 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010827 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10828#endif
10829
10830 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10831 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10832 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10833 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10834 mbedtls_ssl_list_ciphersuites();
10835
10836#if defined(MBEDTLS_X509_CRT_PARSE_C)
10837 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10838#endif
10839
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010840#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010841 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010842#endif
10843
10844#if defined(MBEDTLS_ECP_C)
10845 conf->curve_list = mbedtls_ecp_grp_id_list();
10846#endif
10847
10848#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10849 conf->dhm_min_bitlen = 1024;
10850#endif
10851 }
10852
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010853 return( 0 );
10854}
10855
10856/*
10857 * Free mbedtls_ssl_config
10858 */
10859void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10860{
10861#if defined(MBEDTLS_DHM_C)
10862 mbedtls_mpi_free( &conf->dhm_P );
10863 mbedtls_mpi_free( &conf->dhm_G );
10864#endif
10865
10866#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10867 if( conf->psk != NULL )
10868 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010869 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010870 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010871 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010872 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010873 }
10874
10875 if( conf->psk_identity != NULL )
10876 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010877 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010878 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010879 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010880 conf->psk_identity_len = 0;
10881 }
10882#endif
10883
10884#if defined(MBEDTLS_X509_CRT_PARSE_C)
10885 ssl_key_cert_free( conf->key_cert );
10886#endif
10887
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010888 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010889}
10890
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010891#if defined(MBEDTLS_PK_C) && \
10892 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010893/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010896unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010897{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898#if defined(MBEDTLS_RSA_C)
10899 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10900 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902#if defined(MBEDTLS_ECDSA_C)
10903 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10904 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010906 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010907}
10908
Hanno Becker7e5437a2017-04-28 17:15:26 +010010909unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10910{
10911 switch( type ) {
10912 case MBEDTLS_PK_RSA:
10913 return( MBEDTLS_SSL_SIG_RSA );
10914 case MBEDTLS_PK_ECDSA:
10915 case MBEDTLS_PK_ECKEY:
10916 return( MBEDTLS_SSL_SIG_ECDSA );
10917 default:
10918 return( MBEDTLS_SSL_SIG_ANON );
10919 }
10920}
10921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010923{
10924 switch( sig )
10925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010926#if defined(MBEDTLS_RSA_C)
10927 case MBEDTLS_SSL_SIG_RSA:
10928 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930#if defined(MBEDTLS_ECDSA_C)
10931 case MBEDTLS_SSL_SIG_ECDSA:
10932 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010933#endif
10934 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010935 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010936 }
10937}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010938#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010939
Hanno Becker7e5437a2017-04-28 17:15:26 +010010940#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10941 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10942
10943/* Find an entry in a signature-hash set matching a given hash algorithm. */
10944mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10945 mbedtls_pk_type_t sig_alg )
10946{
10947 switch( sig_alg )
10948 {
10949 case MBEDTLS_PK_RSA:
10950 return( set->rsa );
10951 case MBEDTLS_PK_ECDSA:
10952 return( set->ecdsa );
10953 default:
10954 return( MBEDTLS_MD_NONE );
10955 }
10956}
10957
10958/* Add a signature-hash-pair to a signature-hash set */
10959void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10960 mbedtls_pk_type_t sig_alg,
10961 mbedtls_md_type_t md_alg )
10962{
10963 switch( sig_alg )
10964 {
10965 case MBEDTLS_PK_RSA:
10966 if( set->rsa == MBEDTLS_MD_NONE )
10967 set->rsa = md_alg;
10968 break;
10969
10970 case MBEDTLS_PK_ECDSA:
10971 if( set->ecdsa == MBEDTLS_MD_NONE )
10972 set->ecdsa = md_alg;
10973 break;
10974
10975 default:
10976 break;
10977 }
10978}
10979
10980/* Allow exactly one hash algorithm for each signature. */
10981void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10982 mbedtls_md_type_t md_alg )
10983{
10984 set->rsa = md_alg;
10985 set->ecdsa = md_alg;
10986}
10987
10988#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10989 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10990
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010991/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010992 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010995{
10996 switch( hash )
10997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998#if defined(MBEDTLS_MD5_C)
10999 case MBEDTLS_SSL_HASH_MD5:
11000 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011001#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002#if defined(MBEDTLS_SHA1_C)
11003 case MBEDTLS_SSL_HASH_SHA1:
11004 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011005#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006#if defined(MBEDTLS_SHA256_C)
11007 case MBEDTLS_SSL_HASH_SHA224:
11008 return( MBEDTLS_MD_SHA224 );
11009 case MBEDTLS_SSL_HASH_SHA256:
11010 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011011#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#if defined(MBEDTLS_SHA512_C)
11013 case MBEDTLS_SSL_HASH_SHA384:
11014 return( MBEDTLS_MD_SHA384 );
11015 case MBEDTLS_SSL_HASH_SHA512:
11016 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011017#endif
11018 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011020 }
11021}
11022
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011023/*
11024 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11025 */
11026unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11027{
11028 switch( md )
11029 {
11030#if defined(MBEDTLS_MD5_C)
11031 case MBEDTLS_MD_MD5:
11032 return( MBEDTLS_SSL_HASH_MD5 );
11033#endif
11034#if defined(MBEDTLS_SHA1_C)
11035 case MBEDTLS_MD_SHA1:
11036 return( MBEDTLS_SSL_HASH_SHA1 );
11037#endif
11038#if defined(MBEDTLS_SHA256_C)
11039 case MBEDTLS_MD_SHA224:
11040 return( MBEDTLS_SSL_HASH_SHA224 );
11041 case MBEDTLS_MD_SHA256:
11042 return( MBEDTLS_SSL_HASH_SHA256 );
11043#endif
11044#if defined(MBEDTLS_SHA512_C)
11045 case MBEDTLS_MD_SHA384:
11046 return( MBEDTLS_SSL_HASH_SHA384 );
11047 case MBEDTLS_MD_SHA512:
11048 return( MBEDTLS_SSL_HASH_SHA512 );
11049#endif
11050 default:
11051 return( MBEDTLS_SSL_HASH_NONE );
11052 }
11053}
11054
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011055#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011056/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011057 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011058 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011059 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011060int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011063
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011064 if( ssl->conf->curve_list == NULL )
11065 return( -1 );
11066
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011067 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011068 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011069 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011070
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011071 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011072}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011073#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011074
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011075#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011076/*
11077 * Check if a hash proposed by the peer is in our list.
11078 * Return 0 if we're willing to use it, -1 otherwise.
11079 */
11080int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11081 mbedtls_md_type_t md )
11082{
11083 const int *cur;
11084
11085 if( ssl->conf->sig_hashes == NULL )
11086 return( -1 );
11087
11088 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11089 if( *cur == (int) md )
11090 return( 0 );
11091
11092 return( -1 );
11093}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011094#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096#if defined(MBEDTLS_X509_CRT_PARSE_C)
11097int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11098 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011099 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011100 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011101{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011102 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011103#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011104 int usage = 0;
11105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011106#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011107 const char *ext_oid;
11108 size_t ext_len;
11109#endif
11110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11112 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011113 ((void) cert);
11114 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011115 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011116#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011118#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11119 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011120 {
11121 /* Server part of the key exchange */
11122 switch( ciphersuite->key_exchange )
11123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 case MBEDTLS_KEY_EXCHANGE_RSA:
11125 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011126 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011127 break;
11128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11130 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11131 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11132 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011133 break;
11134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11136 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011137 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011138 break;
11139
11140 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011141 case MBEDTLS_KEY_EXCHANGE_NONE:
11142 case MBEDTLS_KEY_EXCHANGE_PSK:
11143 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11144 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011145 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011146 usage = 0;
11147 }
11148 }
11149 else
11150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011151 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11152 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011153 }
11154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011155 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011156 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011157 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011158 ret = -1;
11159 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011160#else
11161 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011162#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011164#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11165 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011167 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11168 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011169 }
11170 else
11171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11173 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011174 }
11175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011176 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011177 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011178 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011179 ret = -1;
11180 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011181#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011182
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011183 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011185#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011186
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011187/*
11188 * Convert version numbers to/from wire format
11189 * and, for DTLS, to/from TLS equivalent.
11190 *
11191 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011192 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011193 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11194 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011197 unsigned char ver[2] )
11198{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011199#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11200 ((void) transport);
11201#endif
11202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011203#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011204 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011206 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011207 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11208
11209 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11210 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11211 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011212 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011213#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011214#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011215 {
11216 ver[0] = (unsigned char) major;
11217 ver[1] = (unsigned char) minor;
11218 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011219#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011220}
11221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011222void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011223 const unsigned char ver[2] )
11224{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011225#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11226 ((void) transport);
11227#endif
11228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011229#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011230 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011231 {
11232 *major = 255 - ver[0] + 2;
11233 *minor = 255 - ver[1] + 1;
11234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011235 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011236 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11237 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011238 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011239#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011240#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011241 {
11242 *major = ver[0];
11243 *minor = ver[1];
11244 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011245#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011246}
11247
Simon Butcher99000142016-10-13 17:21:01 +010011248int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11249{
11250#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11251 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11252 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11253
11254 switch( md )
11255 {
11256#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11257#if defined(MBEDTLS_MD5_C)
11258 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011259 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011260#endif
11261#if defined(MBEDTLS_SHA1_C)
11262 case MBEDTLS_SSL_HASH_SHA1:
11263 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11264 break;
11265#endif
11266#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11267#if defined(MBEDTLS_SHA512_C)
11268 case MBEDTLS_SSL_HASH_SHA384:
11269 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11270 break;
11271#endif
11272#if defined(MBEDTLS_SHA256_C)
11273 case MBEDTLS_SSL_HASH_SHA256:
11274 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11275 break;
11276#endif
11277 default:
11278 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11279 }
11280
11281 return 0;
11282#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11283 (void) ssl;
11284 (void) md;
11285
11286 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11288}
11289
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011290#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11291 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11292int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11293 unsigned char *output,
11294 unsigned char *data, size_t data_len )
11295{
11296 int ret = 0;
11297 mbedtls_md5_context mbedtls_md5;
11298 mbedtls_sha1_context mbedtls_sha1;
11299
11300 mbedtls_md5_init( &mbedtls_md5 );
11301 mbedtls_sha1_init( &mbedtls_sha1 );
11302
11303 /*
11304 * digitally-signed struct {
11305 * opaque md5_hash[16];
11306 * opaque sha_hash[20];
11307 * };
11308 *
11309 * md5_hash
11310 * MD5(ClientHello.random + ServerHello.random
11311 * + ServerParams);
11312 * sha_hash
11313 * SHA(ClientHello.random + ServerHello.random
11314 * + ServerParams);
11315 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011316 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011317 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011319 goto exit;
11320 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011321 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011322 ssl->handshake->randbytes, 64 ) ) != 0 )
11323 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011325 goto exit;
11326 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011327 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011328 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011330 goto exit;
11331 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011332 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011333 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011335 goto exit;
11336 }
11337
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011338 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011339 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011341 goto exit;
11342 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011343 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011344 ssl->handshake->randbytes, 64 ) ) != 0 )
11345 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347 goto exit;
11348 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011349 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011350 data_len ) ) != 0 )
11351 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011353 goto exit;
11354 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011355 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011356 output + 16 ) ) != 0 )
11357 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011358 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359 goto exit;
11360 }
11361
11362exit:
11363 mbedtls_md5_free( &mbedtls_md5 );
11364 mbedtls_sha1_free( &mbedtls_sha1 );
11365
11366 if( ret != 0 )
11367 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11368 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11369
11370 return( ret );
11371
11372}
11373#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11374 MBEDTLS_SSL_PROTO_TLS1_1 */
11375
11376#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11377 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11378int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011379 unsigned char *hash, size_t *hashlen,
11380 unsigned char *data, size_t data_len,
11381 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011382{
11383 int ret = 0;
11384 mbedtls_md_context_t ctx;
11385 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011386 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011387
11388 mbedtls_md_init( &ctx );
11389
11390 /*
11391 * digitally-signed struct {
11392 * opaque client_random[32];
11393 * opaque server_random[32];
11394 * ServerDHParams params;
11395 * };
11396 */
11397 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11398 {
11399 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11400 goto exit;
11401 }
11402 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11403 {
11404 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11405 goto exit;
11406 }
11407 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11408 {
11409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11410 goto exit;
11411 }
11412 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11413 {
11414 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11415 goto exit;
11416 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011417 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011418 {
11419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11420 goto exit;
11421 }
11422
11423exit:
11424 mbedtls_md_free( &ctx );
11425
11426 if( ret != 0 )
11427 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11428 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11429
11430 return( ret );
11431}
11432#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11433 MBEDTLS_SSL_PROTO_TLS1_2 */
11434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011435#endif /* MBEDTLS_SSL_TLS_C */