blob: f2034a4a871e9f96f181aae0643c4f843ff2cd2a [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 Beckerd5258fa2019-02-07 12:27:42 +0000398#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
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 }
411#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
412
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],
754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
755 int encrypt_then_mac,
756#endif
757#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
758 int trunc_hmac,
759#endif
760#if defined(MBEDTLS_ZLIB_SUPPORT)
761 int compression,
762#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200763 ssl_tls_prf_t tls_prf,
764 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200765 int minor_ver,
766 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200767 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000768{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770 unsigned char keyblk[256];
771 unsigned char *key1;
772 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100773 unsigned char *mac_enc;
774 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000775 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200776 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000777 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000778 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 const mbedtls_cipher_info_t *cipher_info;
780 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100781
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200782#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
783 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
784 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200785 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200786 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000787#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000788
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200789 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200791 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000792#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200793 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200795 /*
796 * Get various info structures
797 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200798 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200799 if( ciphersuite_info == NULL )
800 {
801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200802 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
804 }
805
Hanno Becker8759e162017-12-27 21:34:08 +0000806 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100807 if( cipher_info == NULL )
808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000810 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100812 }
813
Hanno Becker8759e162017-12-27 21:34:08 +0000814 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100815 if( md_info == NULL )
816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000818 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100820 }
821
Hanno Beckera5a2b082019-05-15 14:03:01 +0100822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 /* Copy own and peer's CID if the use of the CID
824 * extension has been negotiated. */
825 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
826 {
827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100828
Hanno Becker4932f9f2019-05-03 15:23:51 +0100829 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100830 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100831 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100832 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100833
834 transform->out_cid_len = ssl->handshake->peer_cid_len;
835 memcpy( transform->out_cid, ssl->handshake->peer_cid,
836 ssl->handshake->peer_cid_len );
837 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
838 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100839 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100840#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200843 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000844 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200845 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100846 if( ret != 0 )
847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100849 return( ret );
850 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200853 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200854 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200855 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000857
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 /*
859 * Determine the appropriate key, IV and MAC length.
860 */
Paul Bakker68884e32013-01-07 18:20:04 +0100861
Hanno Beckere7f2df02017-12-27 08:17:40 +0000862 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200863
Hanno Beckerf1229442018-01-03 15:32:31 +0000864#if defined(MBEDTLS_GCM_C) || \
865 defined(MBEDTLS_CCM_C) || \
866 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200868 cipher_info->mode == MBEDTLS_MODE_CCM ||
869 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 {
Hanno Becker8759e162017-12-27 21:34:08 +0000871 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200873 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000874 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000875 transform->taglen =
876 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200877
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200878 /* All modes haves 96-bit IVs;
879 * GCM and CCM has 4 implicit and 8 explicit bytes
880 * ChachaPoly has all 12 bytes implicit
881 */
Paul Bakker68884e32013-01-07 18:20:04 +0100882 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200883 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
884 transform->fixed_ivlen = 12;
885 else
886 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200888 /* Minimum length of encrypted record */
889 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000890 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100891 }
892 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000893#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
894#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
895 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
896 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100897 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200898 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
900 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200903 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100904 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200906 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000907 mac_key_len = mbedtls_md_get_size( md_info );
908 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200911 /*
912 * If HMAC is to be truncated, we shall keep the leftmost bytes,
913 * (rfc 6066 page 13 or rfc 2104 section 4),
914 * so we only need to adjust the length here.
915 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200916 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000919
920#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
921 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000922 * HMAC implementation which also truncates the key
923 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000924 mac_key_len = transform->maclen;
925#endif
926 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200928
929 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100930 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000931
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200932 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200934 transform->minlen = transform->maclen;
935 else
Paul Bakker68884e32013-01-07 18:20:04 +0100936 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200937 /*
938 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100939 * 1. if EtM is in use: one block plus MAC
940 * otherwise: * first multiple of blocklen greater than maclen
941 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200942 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200944 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100945 {
946 transform->minlen = transform->maclen
947 + cipher_info->block_size;
948 }
949 else
950#endif
951 {
952 transform->minlen = transform->maclen
953 + cipher_info->block_size
954 - transform->maclen % cipher_info->block_size;
955 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200958 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
959 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200960 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100961 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200962#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200964 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
965 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200966 {
967 transform->minlen += transform->ivlen;
968 }
969 else
970#endif
971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200974 }
Paul Bakker68884e32013-01-07 18:20:04 +0100975 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000977 else
978#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
982 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000983
Hanno Beckere7f2df02017-12-27 08:17:40 +0000984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
985 (unsigned) keylen,
986 (unsigned) transform->minlen,
987 (unsigned) transform->ivlen,
988 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000989
990 /*
991 * Finally setup the cipher contexts, IVs and MAC secrets.
992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200994 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000996 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000997 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000998
Paul Bakker68884e32013-01-07 18:20:04 +0100999 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001000 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001002 /*
1003 * This is not used in TLS v1.1.
1004 */
Paul Bakker48916f92012-09-16 19:57:18 +00001005 iv_copy_len = ( transform->fixed_ivlen ) ?
1006 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001007 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1008 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001010 }
1011 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_SSL_CLI_C */
1013#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001014 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001016 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001017 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001018
Hanno Becker81c7b182017-11-09 18:39:33 +00001019 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001020 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001022 /*
1023 * This is not used in TLS v1.1.
1024 */
Paul Bakker48916f92012-09-16 19:57:18 +00001025 iv_copy_len = ( transform->fixed_ivlen ) ?
1026 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001027 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1028 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001031 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001036 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Hanno Becker92231322018-01-03 15:32:51 +00001038#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001040 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001041 {
Hanno Becker92231322018-01-03 15:32:51 +00001042 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001046 }
1047
Hanno Becker81c7b182017-11-09 18:39:33 +00001048 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1049 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001050 }
1051 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1053#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1054 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001055 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001056 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001057 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1058 For AEAD-based ciphersuites, there is nothing to do here. */
1059 if( mac_key_len != 0 )
1060 {
1061 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1062 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1063 }
Paul Bakker68884e32013-01-07 18:20:04 +01001064 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 else
1066#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1069 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001070 }
Hanno Becker92231322018-01-03 15:32:51 +00001071#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1074 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001075 {
1076 int ret = 0;
1077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001079
Hanno Beckere7f2df02017-12-27 08:17:40 +00001080 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001081 transform->iv_enc, transform->iv_dec,
1082 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001083 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001084 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1087 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001088 }
1089 }
Hanno Becker92231322018-01-03 15:32:51 +00001090#else
1091 ((void) mac_dec);
1092 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001094
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001095#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1096 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001097 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001098 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001099 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001100 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001101 iv_copy_len );
1102 }
1103#endif
1104
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001105 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001107 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001109 return( ret );
1110 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001111
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001112 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001113 cipher_info ) ) != 0 )
1114 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001116 return( ret );
1117 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001120 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001124 return( ret );
1125 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001128 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001132 return( ret );
1133 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135#if defined(MBEDTLS_CIPHER_MODE_CBC)
1136 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1139 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001142 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001143 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1146 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001149 return( ret );
1150 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001154 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001156 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001158 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001161
Paul Bakker48916f92012-09-16 19:57:18 +00001162 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1163 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001164
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001165 if( deflateInit( &transform->ctx_deflate,
1166 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001167 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1170 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001171 }
1172 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001174
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 return( 0 );
1176}
1177
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001178/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001179 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001180 *
1181 * Inputs:
1182 * - SSL/TLS minor version
1183 * - hash associated with the ciphersuite (only used by TLS 1.2)
1184 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001185 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001186 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1187 */
1188static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1189 int minor_ver,
1190 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001192#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1193 (void) hash;
1194#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001195
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001196#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001197 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001198 {
1199 handshake->tls_prf = ssl3_prf;
1200 handshake->calc_verify = ssl_calc_verify_ssl;
1201 handshake->calc_finished = ssl_calc_finished_ssl;
1202 }
1203 else
1204#endif
1205#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001206 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001207 {
1208 handshake->tls_prf = tls1_prf;
1209 handshake->calc_verify = ssl_calc_verify_tls;
1210 handshake->calc_finished = ssl_calc_finished_tls;
1211 }
1212 else
1213#endif
1214#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1215#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001216 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1217 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001218 {
1219 handshake->tls_prf = tls_prf_sha384;
1220 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1221 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1222 }
1223 else
1224#endif
1225#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001226 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001227 {
1228 handshake->tls_prf = tls_prf_sha256;
1229 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1230 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1231 }
1232 else
1233#endif
1234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1235 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1237 }
1238
1239 return( 0 );
1240}
1241
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001242/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001243 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 *
1245 * Parameters:
1246 * [in/out] handshake
1247 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1248 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001249 * [out] master
1250 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001251 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001252static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001253 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001254 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255{
1256 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257
1258#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001259 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001260 (void) ssl;
1261#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001262
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001264 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001265 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1266 return( 0 );
1267 }
1268
1269 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1270 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271
1272#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001273 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001274 {
1275 unsigned char session_hash[48];
1276 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001277
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001278 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001279
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001280 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1281 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001282
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001283 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001284 "extended master secret",
1285 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001286 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001287 }
1288 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001289#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001290 {
1291 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1292 "master secret",
1293 handshake->randbytes, 64,
1294 master, 48 );
1295 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001296 if( ret != 0 )
1297 {
1298 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1299 return( ret );
1300 }
1301
1302 mbedtls_platform_zeroize( handshake->premaster,
1303 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001304
1305 return( 0 );
1306}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001308int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1309{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001310 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001311 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1312 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001313
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1315
1316 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001317 ret = ssl_set_handshake_prfs( ssl->handshake,
1318 ssl->minor_ver,
1319 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001320 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001321 {
1322 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001323 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001324 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001325
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001326 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001327 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001328 ssl->session_negotiate->master,
1329 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001330 if( ret != 0 )
1331 {
1332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1333 return( ret );
1334 }
1335
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001336 /* Swap the client and server random values:
1337 * - MS derivation wanted client+server (RFC 5246 8.1)
1338 * - key derivation wants server+client (RFC 5246 6.3) */
1339 {
1340 unsigned char tmp[64];
1341 memcpy( tmp, ssl->handshake->randbytes, 64 );
1342 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1343 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1344 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1345 }
1346
1347 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001349 ssl->session_negotiate->ciphersuite,
1350 ssl->session_negotiate->master,
1351#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1352 ssl->session_negotiate->encrypt_then_mac,
1353#endif
1354#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1355 ssl->session_negotiate->trunc_hmac,
1356#endif
1357#if defined(MBEDTLS_ZLIB_SUPPORT)
1358 ssl->session_negotiate->compression,
1359#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001360 ssl->handshake->tls_prf,
1361 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001362 ssl->minor_ver,
1363 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001364 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001365 if( ret != 0 )
1366 {
1367 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1368 return( ret );
1369 }
1370
1371 /* We no longer need Server/ClientHello.random values */
1372 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1373 sizeof( ssl->handshake->randbytes ) );
1374
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001375 /* Allocate compression buffer */
1376#if defined(MBEDTLS_ZLIB_SUPPORT)
1377 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1378 ssl->compress_buf == NULL )
1379 {
1380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1381 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1382 if( ssl->compress_buf == NULL )
1383 {
1384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001385 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001386 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1387 }
1388 }
1389#endif
1390
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1392
1393 return( 0 );
1394}
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001397void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1398 unsigned char hash[36],
1399 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001400{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001401 mbedtls_md5_context md5;
1402 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 unsigned char pad_1[48];
1404 unsigned char pad_2[48];
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001407
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001408 mbedtls_md5_init( &md5 );
1409 mbedtls_sha1_init( &sha1 );
1410
1411 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1412 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Paul Bakker380da532012-04-18 16:10:25 +00001414 memset( pad_1, 0x36, 48 );
1415 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001417 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1418 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1419 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001421 mbedtls_md5_starts_ret( &md5 );
1422 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1423 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1424 mbedtls_md5_update_ret( &md5, hash, 16 );
1425 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001427 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1428 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1429 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001431 mbedtls_sha1_starts_ret( &sha1 );
1432 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1433 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1434 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1435 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001436
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001437 *hlen = 36;
1438
1439 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001441
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001442 mbedtls_md5_free( &md5 );
1443 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001444
Paul Bakker380da532012-04-18 16:10:25 +00001445 return;
1446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001450void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1451 unsigned char hash[36],
1452 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001453{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001454 mbedtls_md5_context md5;
1455 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001458
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001459 mbedtls_md5_init( &md5 );
1460 mbedtls_sha1_init( &sha1 );
1461
1462 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1463 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001465 mbedtls_md5_finish_ret( &md5, hash );
1466 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001467
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001468 *hlen = 36;
1469
1470 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001472
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001473 mbedtls_md5_free( &md5 );
1474 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001475
Paul Bakker380da532012-04-18 16:10:25 +00001476 return;
1477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1481#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001482void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1483 unsigned char hash[32],
1484 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001485{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001486 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001487
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001488 mbedtls_sha256_init( &sha256 );
1489
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001492 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001493 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001494
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001495 *hlen = 32;
1496
1497 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001499
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001500 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001501
Paul Bakker380da532012-04-18 16:10:25 +00001502 return;
1503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001507void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1508 unsigned char hash[48],
1509 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001510{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001511 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001512
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001513 mbedtls_sha512_init( &sha512 );
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001516
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001517 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001518 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001520 *hlen = 48;
1521
1522 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001525 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001526
Paul Bakker5121ce52009-01-03 21:22:43 +00001527 return;
1528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_SHA512_C */
1530#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1533int 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 +02001534{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001535 unsigned char *p = ssl->handshake->premaster;
1536 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001537 const unsigned char *psk = ssl->conf->psk;
1538 size_t psk_len = ssl->conf->psk_len;
1539
1540 /* If the psk callback was called, use its result */
1541 if( ssl->handshake->psk != NULL )
1542 {
1543 psk = ssl->handshake->psk;
1544 psk_len = ssl->handshake->psk_len;
1545 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001546
1547 /*
1548 * PMS = struct {
1549 * opaque other_secret<0..2^16-1>;
1550 * opaque psk<0..2^16-1>;
1551 * };
1552 * with "other_secret" depending on the particular key exchange
1553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1555 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001556 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001557 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001559
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001560 *(p++) = (unsigned char)( psk_len >> 8 );
1561 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001562
1563 if( end < p || (size_t)( end - p ) < psk_len )
1564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1565
1566 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001567 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001573 {
1574 /*
1575 * other_secret already set by the ClientKeyExchange message,
1576 * and is 48 bytes long
1577 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001578 if( end - p < 2 )
1579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1580
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001581 *p++ = 0;
1582 *p++ = 48;
1583 p += 48;
1584 }
1585 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1587#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1588 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001589 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001590 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001591 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001592
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001593 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001595 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001596 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001599 return( ret );
1600 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001601 *(p++) = (unsigned char)( len >> 8 );
1602 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001603 p += len;
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001606 }
1607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1609#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1610 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001611 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001612 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 size_t zlen;
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001616 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001617 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001620 return( ret );
1621 }
1622
1623 *(p++) = (unsigned char)( zlen >> 8 );
1624 *(p++) = (unsigned char)( zlen );
1625 p += zlen;
1626
Janos Follath3fbdada2018-08-15 10:26:53 +01001627 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1628 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001629 }
1630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001635 }
1636
1637 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001638 if( end - p < 2 )
1639 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001640
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001641 *(p++) = (unsigned char)( psk_len >> 8 );
1642 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001643
1644 if( end < p || (size_t)( end - p ) < psk_len )
1645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1646
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001647 memcpy( p, psk, psk_len );
1648 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001649
1650 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1651
1652 return( 0 );
1653}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001657/*
1658 * SSLv3.0 MAC functions
1659 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001660#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001661static void ssl_mac( mbedtls_md_context_t *md_ctx,
1662 const unsigned char *secret,
1663 const unsigned char *buf, size_t len,
1664 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001665 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001666{
1667 unsigned char header[11];
1668 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001669 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1671 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001672
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001673 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001675 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001676 else
Paul Bakker68884e32013-01-07 18:20:04 +01001677 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
1679 memcpy( header, ctr, 8 );
1680 header[ 8] = (unsigned char) type;
1681 header[ 9] = (unsigned char)( len >> 8 );
1682 header[10] = (unsigned char)( len );
1683
Paul Bakker68884e32013-01-07 18:20:04 +01001684 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_md_starts( md_ctx );
1686 mbedtls_md_update( md_ctx, secret, md_size );
1687 mbedtls_md_update( md_ctx, padding, padlen );
1688 mbedtls_md_update( md_ctx, header, 11 );
1689 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001690 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Paul Bakker68884e32013-01-07 18:20:04 +01001692 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_md_starts( md_ctx );
1694 mbedtls_md_update( md_ctx, secret, md_size );
1695 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001696 mbedtls_md_update( md_ctx, out, md_size );
1697 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001700
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001701/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001702 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001703#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001704 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1705 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1706 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1707/* This function makes sure every byte in the memory region is accessed
1708 * (in ascending addresses order) */
1709static void ssl_read_memory( unsigned char *p, size_t len )
1710{
1711 unsigned char acc = 0;
1712 volatile unsigned char force;
1713
1714 for( ; len != 0; p++, len-- )
1715 acc ^= *p;
1716
1717 force = acc;
1718 (void) force;
1719}
1720#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1721
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001722/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001724 */
Hanno Becker3307b532017-12-27 21:37:21 +00001725
Hanno Beckera5a2b082019-05-15 14:03:01 +01001726#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001727/* This functions transforms a DTLS plaintext fragment and a record content
1728 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001729 *
1730 * struct {
1731 * opaque content[DTLSPlaintext.length];
1732 * ContentType real_type;
1733 * uint8 zeros[length_of_padding];
1734 * } DTLSInnerPlaintext;
1735 *
1736 * Input:
1737 * - `content`: The beginning of the buffer holding the
1738 * plaintext to be wrapped.
1739 * - `*content_size`: The length of the plaintext in Bytes.
1740 * - `max_len`: The number of Bytes available starting from
1741 * `content`. This must be `>= *content_size`.
1742 * - `rec_type`: The desired record content type.
1743 *
1744 * Output:
1745 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1746 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1747 *
1748 * Returns:
1749 * - `0` on success.
1750 * - A negative error code if `max_len` didn't offer enough space
1751 * for the expansion.
1752 */
1753static int ssl_cid_build_inner_plaintext( unsigned char *content,
1754 size_t *content_size,
1755 size_t remaining,
1756 uint8_t rec_type )
1757{
1758 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001759 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1760 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1761 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001762
1763 /* Write real content type */
1764 if( remaining == 0 )
1765 return( -1 );
1766 content[ len ] = rec_type;
1767 len++;
1768 remaining--;
1769
1770 if( remaining < pad )
1771 return( -1 );
1772 memset( content + len, 0, pad );
1773 len += pad;
1774 remaining -= pad;
1775
1776 *content_size = len;
1777 return( 0 );
1778}
1779
Hanno Becker7dc25772019-05-20 15:08:01 +01001780/* This function parses a DTLSInnerPlaintext structure.
1781 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001782static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1783 size_t *content_size,
1784 uint8_t *rec_type )
1785{
1786 size_t remaining = *content_size;
1787
1788 /* Determine length of padding by skipping zeroes from the back. */
1789 do
1790 {
1791 if( remaining == 0 )
1792 return( -1 );
1793 remaining--;
1794 } while( content[ remaining ] == 0 );
1795
1796 *content_size = remaining;
1797 *rec_type = content[ remaining ];
1798
1799 return( 0 );
1800}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001801#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001802
Hanno Becker99abf512019-05-20 14:50:53 +01001803/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001804 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001805static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001807 mbedtls_record *rec )
1808{
Hanno Becker99abf512019-05-20 14:50:53 +01001809 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001810 *
1811 * additional_data = seq_num + TLSCompressed.type +
1812 * TLSCompressed.version + TLSCompressed.length;
1813 *
Hanno Becker99abf512019-05-20 14:50:53 +01001814 * For the CID extension, this is extended as follows
1815 * (quoting draft-ietf-tls-dtls-connection-id-05,
1816 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001817 *
1818 * additional_data = seq_num + DTLSPlaintext.type +
1819 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001820 * cid +
1821 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001822 * length_of_DTLSInnerPlaintext;
1823 */
1824
Hanno Becker3307b532017-12-27 21:37:21 +00001825 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1826 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001827 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001828
Hanno Beckera5a2b082019-05-15 14:03:01 +01001829#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001830 if( rec->cid_len != 0 )
1831 {
1832 memcpy( add_data + 11, rec->cid, rec->cid_len );
1833 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1834 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1835 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1836 *add_data_len = 13 + 1 + rec->cid_len;
1837 }
1838 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001839#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001840 {
1841 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1842 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1843 *add_data_len = 13;
1844 }
Hanno Becker3307b532017-12-27 21:37:21 +00001845}
1846
Hanno Becker611a83b2018-01-03 14:27:32 +00001847int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1848 mbedtls_ssl_transform *transform,
1849 mbedtls_record *rec,
1850 int (*f_rng)(void *, unsigned char *, size_t),
1851 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001854 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001855 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001856 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001857 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001858 size_t post_avail;
1859
1860 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001861#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001862 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001863 ((void) ssl);
1864#endif
1865
1866 /* The PRNG is used for dynamic IV generation that's used
1867 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1868#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1869 ( defined(MBEDTLS_AES_C) || \
1870 defined(MBEDTLS_ARIA_C) || \
1871 defined(MBEDTLS_CAMELLIA_C) ) && \
1872 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1873 ((void) f_rng);
1874 ((void) p_rng);
1875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Hanno Becker3307b532017-12-27 21:37:21 +00001879 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001880 {
Hanno Becker3307b532017-12-27 21:37:21 +00001881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1882 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1883 }
Hanno Becker505089d2019-05-01 09:45:57 +01001884 if( rec == NULL
1885 || rec->buf == NULL
1886 || rec->buf_len < rec->data_offset
1887 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001889 || rec->cid_len != 0
1890#endif
1891 )
Hanno Becker3307b532017-12-27 21:37:21 +00001892 {
1893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001895 }
1896
Hanno Becker3307b532017-12-27 21:37:21 +00001897 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001898 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001900 data, rec->data_len );
1901
1902 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1903
1904 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1905 {
1906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1907 (unsigned) rec->data_len,
1908 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1910 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001911
Hanno Beckera5a2b082019-05-15 14:03:01 +01001912#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001913 /*
1914 * Add CID information
1915 */
1916 rec->cid_len = transform->out_cid_len;
1917 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1918 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001919
1920 if( rec->cid_len != 0 )
1921 {
1922 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001923 * Wrap plaintext into DTLSInnerPlaintext structure.
1924 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001926 * Note that this changes `rec->data_len`, and hence
1927 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001928 */
1929 if( ssl_cid_build_inner_plaintext( data,
1930 &rec->data_len,
1931 post_avail,
1932 rec->type ) != 0 )
1933 {
1934 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1935 }
1936
1937 rec->type = MBEDTLS_SSL_MSG_CID;
1938 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001939#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001940
Hanno Becker92c930f2019-04-29 17:31:37 +01001941 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001944 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001946#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( mode == MBEDTLS_MODE_STREAM ||
1948 ( mode == MBEDTLS_MODE_CBC
1949#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001950 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001951#endif
1952 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001953 {
Hanno Becker3307b532017-12-27 21:37:21 +00001954 if( post_avail < transform->maclen )
1955 {
1956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1957 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1958 }
1959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001961 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001962 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001963 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001964 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1965 data, rec->data_len, rec->ctr, rec->type, mac );
1966 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001967 }
1968 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1971 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001972 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Hanno Becker992b6872017-11-09 18:57:39 +00001974 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1975
Hanno Beckere83efe62019-04-29 13:52:53 +01001976 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001979 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001980 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1981 data, rec->data_len );
1982 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1983 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1984
1985 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001986 }
1987 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001988#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001992 }
1993
Hanno Becker3307b532017-12-27 21:37:21 +00001994 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1995 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001996
Hanno Becker3307b532017-12-27 21:37:21 +00001997 rec->data_len += transform->maclen;
1998 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001999 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002000 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00002001#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002003 /*
2004 * Encrypt
2005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2007 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002008 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002010 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00002012 "including %d bytes of padding",
2013 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Hanno Becker3307b532017-12-27 21:37:21 +00002015 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2016 transform->iv_enc, transform->ivlen,
2017 data, rec->data_len,
2018 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002021 return( ret );
2022 }
2023
Hanno Becker3307b532017-12-27 21:37:21 +00002024 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2027 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
Paul Bakker68884e32013-01-07 18:20:04 +01002030 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002032
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002033#if defined(MBEDTLS_GCM_C) || \
2034 defined(MBEDTLS_CCM_C) || \
2035 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002037 mode == MBEDTLS_MODE_CCM ||
2038 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002039 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002040 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002041 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002042 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002043
Hanno Becker3307b532017-12-27 21:37:21 +00002044 /* Check that there's space for both the authentication tag
2045 * and the explicit IV before and after the record content. */
2046 if( post_avail < transform->taglen ||
2047 rec->data_offset < explicit_iv_len )
2048 {
2049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2050 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2051 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002052
Paul Bakker68884e32013-01-07 18:20:04 +01002053 /*
2054 * Generate IV
2055 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002056 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2057 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002058 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002059 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002060 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2061 explicit_iv_len );
2062 /* Prefix record content with explicit IV. */
2063 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002064 }
2065 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2066 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002067 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 unsigned char i;
2069
2070 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2071
2072 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002073 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002074 }
2075 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002076 {
2077 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002080 }
2081
Hanno Beckere83efe62019-04-29 13:52:53 +01002082 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002083
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002084 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2085 iv, transform->ivlen );
2086 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002087 data - explicit_iv_len, explicit_iv_len );
2088 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002089 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002091 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002092 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002093
Paul Bakker68884e32013-01-07 18:20:04 +01002094 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002095 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002096 */
Hanno Becker3307b532017-12-27 21:37:21 +00002097
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002098 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002099 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002100 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002101 data, rec->data_len, /* source */
2102 data, &rec->data_len, /* destination */
2103 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002106 return( ret );
2107 }
2108
Hanno Becker3307b532017-12-27 21:37:21 +00002109 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2110 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002111
Hanno Becker3307b532017-12-27 21:37:21 +00002112 rec->data_len += transform->taglen + explicit_iv_len;
2113 rec->data_offset -= explicit_iv_len;
2114 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002115 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002116 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2119#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002120 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002123 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002124 size_t padlen, i;
2125 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 /* Currently we're always using minimal padding
2128 * (up to 255 bytes would be allowed). */
2129 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2130 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 padlen = 0;
2132
Hanno Becker3307b532017-12-27 21:37:21 +00002133 /* Check there's enough space in the buffer for the padding. */
2134 if( post_avail < padlen + 1 )
2135 {
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2137 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2138 }
2139
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002141 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Hanno Becker3307b532017-12-27 21:37:21 +00002143 rec->data_len += padlen + 1;
2144 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002147 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002148 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2149 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002150 */
Hanno Becker3307b532017-12-27 21:37:21 +00002151 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002152 {
Hanno Becker3307b532017-12-27 21:37:21 +00002153 if( f_rng == NULL )
2154 {
2155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2157 }
2158
2159 if( rec->data_offset < transform->ivlen )
2160 {
2161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2162 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2163 }
2164
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002165 /*
2166 * Generate IV
2167 */
Hanno Becker3307b532017-12-27 21:37:21 +00002168 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002169 if( ret != 0 )
2170 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002171
Hanno Becker3307b532017-12-27 21:37:21 +00002172 memcpy( data - transform->ivlen, transform->iv_enc,
2173 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002174
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002179 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002180 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002181 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002182
Hanno Becker3307b532017-12-27 21:37:21 +00002183 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2184 transform->iv_enc,
2185 transform->ivlen,
2186 data, rec->data_len,
2187 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002190 return( ret );
2191 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002192
Hanno Becker3307b532017-12-27 21:37:21 +00002193 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002197 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002200 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002201 {
2202 /*
2203 * Save IV in SSL3 and TLS1
2204 */
Hanno Becker3307b532017-12-27 21:37:21 +00002205 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2206 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 }
Hanno Becker3307b532017-12-27 21:37:21 +00002208 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002209#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002210 {
2211 data -= transform->ivlen;
2212 rec->data_offset -= transform->ivlen;
2213 rec->data_len += transform->ivlen;
2214 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002218 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002219 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2220
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221 /*
2222 * MAC(MAC_write_key, seq_num +
2223 * TLSCipherText.type +
2224 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002225 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002226 * IV + // except for TLS 1.0
2227 * ENC(content + padding + padding_length));
2228 */
Hanno Becker3307b532017-12-27 21:37:21 +00002229
2230 if( post_avail < transform->maclen)
2231 {
2232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2233 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2234 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002235
Hanno Beckere83efe62019-04-29 13:52:53 +01002236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002239 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002240 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002241
Hanno Becker3307b532017-12-27 21:37:21 +00002242 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002243 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002244 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2245 data, rec->data_len );
2246 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2247 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002248
Hanno Becker3307b532017-12-27 21:37:21 +00002249 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002250
Hanno Becker3307b532017-12-27 21:37:21 +00002251 rec->data_len += transform->maclen;
2252 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002253 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002254 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002257 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002259 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2262 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002264
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265 /* Make extra sure authentication was performed, exactly once */
2266 if( auth_done != 1 )
2267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2269 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002270 }
2271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273
2274 return( 0 );
2275}
2276
Hanno Becker611a83b2018-01-03 14:27:32 +00002277int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2278 mbedtls_ssl_transform *transform,
2279 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002284#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002285 size_t padlen = 0, correct = 1;
2286#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002287 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002288 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002289 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002290
Hanno Becker611a83b2018-01-03 14:27:32 +00002291#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002292 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002293 ((void) ssl);
2294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002297 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002298 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2301 }
2302 if( rec == NULL ||
2303 rec->buf == NULL ||
2304 rec->buf_len < rec->data_offset ||
2305 rec->buf_len - rec->data_offset < rec->data_len )
2306 {
2307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 }
2310
Hanno Becker4c6876b2017-12-27 21:28:58 +00002311 data = rec->buf + rec->data_offset;
2312 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Hanno Beckera5a2b082019-05-15 14:03:01 +01002314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002315 /*
2316 * Match record's CID with incoming CID.
2317 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002318 if( rec->cid_len != transform->in_cid_len ||
2319 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2320 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002321 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002322 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2326 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002327 {
2328 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002329 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2330 transform->iv_dec,
2331 transform->ivlen,
2332 data, rec->data_len,
2333 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002336 return( ret );
2337 }
2338
Hanno Becker4c6876b2017-12-27 21:28:58 +00002339 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 }
Paul Bakker68884e32013-01-07 18:20:04 +01002345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347#if defined(MBEDTLS_GCM_C) || \
2348 defined(MBEDTLS_CCM_C) || \
2349 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 mode == MBEDTLS_MODE_CCM ||
2352 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002353 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002355 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002356
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 /*
2358 * Compute and update sizes
2359 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002360 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002363 "+ taglen (%d)", rec->data_len,
2364 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002366 }
Paul Bakker68884e32013-01-07 18:20:04 +01002367
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002368 /*
2369 * Prepare IV
2370 */
2371 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2372 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002373 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002374 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002375 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 }
2378 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2379 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002380 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002381 unsigned char i;
2382
2383 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2384
2385 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002387 }
2388 else
2389 {
2390 /* Reminder if we ever add an AEAD mode with a different size */
2391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2392 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2393 }
2394
Hanno Becker4c6876b2017-12-27 21:28:58 +00002395 data += explicit_iv_len;
2396 rec->data_offset += explicit_iv_len;
2397 rec->data_len -= explicit_iv_len + transform->taglen;
2398
Hanno Beckere83efe62019-04-29 13:52:53 +01002399 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002400 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002401 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002402
2403 memcpy( transform->iv_dec + transform->fixed_ivlen,
2404 data - explicit_iv_len, explicit_iv_len );
2405
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002406 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002407 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002408 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002409
Paul Bakker68884e32013-01-07 18:20:04 +01002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002412 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002413 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002414 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2415 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002416 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002417 data, rec->data_len,
2418 data, &olen,
2419 data + rec->data_len,
2420 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2425 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002426
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002427 return( ret );
2428 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002429 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002430
Hanno Becker4c6876b2017-12-27 21:28:58 +00002431 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002435 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2439#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002440 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002442 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002443 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 /*
Paul Bakker45829992013-01-03 14:52:21 +01002446 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002449 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2450 {
2451 /* The ciphertext is prefixed with the CBC IV. */
2452 minlen += transform->ivlen;
2453 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002454#endif
Paul Bakker45829992013-01-03 14:52:21 +01002455
Hanno Becker4c6876b2017-12-27 21:28:58 +00002456 /* Size considerations:
2457 *
2458 * - The CBC cipher text must not be empty and hence
2459 * at least of size transform->ivlen.
2460 *
2461 * Together with the potential IV-prefix, this explains
2462 * the first of the two checks below.
2463 *
2464 * - The record must contain a MAC, either in plain or
2465 * encrypted, depending on whether Encrypt-then-MAC
2466 * is used or not.
2467 * - If it is, the message contains the IV-prefix,
2468 * the CBC ciphertext, and the MAC.
2469 * - If it is not, the padded plaintext, and hence
2470 * the CBC ciphertext, has at least length maclen + 1
2471 * because there is at least the padding length byte.
2472 *
2473 * As the CBC ciphertext is not empty, both cases give the
2474 * lower bound minlen + maclen + 1 on the record size, which
2475 * we test for in the second check below.
2476 */
2477 if( rec->data_len < minlen + transform->ivlen ||
2478 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 "+ 1 ) ( + expl IV )", rec->data_len,
2482 transform->ivlen,
2483 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002485 }
2486
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487 /*
2488 * Authenticate before decrypt if enabled
2489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002491 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 {
Hanno Becker992b6872017-11-09 18:57:39 +00002493 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496
Hanno Becker4c6876b2017-12-27 21:28:58 +00002497 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2498 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Beckere83efe62019-04-29 13:52:53 +01002500 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002501
Hanno Beckere83efe62019-04-29 13:52:53 +01002502 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2503 add_data_len );
2504 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2505 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002506 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2507 data, rec->data_len );
2508 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2509 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002510
Hanno Becker4c6876b2017-12-27 21:28:58 +00002511 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2512 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002513 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002514 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2517 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002525
2526 /*
2527 * Check length sanity
2528 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002529 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002532 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002534 }
2535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002537 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002538 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002539 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002540 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002541 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002542 /* This is safe because data_len >= minlen + maclen + 1 initially,
2543 * and at this point we have at most subtracted maclen (note that
2544 * minlen == transform->ivlen here). */
2545 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002546
Hanno Becker4c6876b2017-12-27 21:28:58 +00002547 data += transform->ivlen;
2548 rec->data_offset += transform->ivlen;
2549 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002550 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002552
Hanno Becker4c6876b2017-12-27 21:28:58 +00002553 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2554 transform->iv_dec, transform->ivlen,
2555 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002558 return( ret );
2559 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002560
Hanno Becker4c6876b2017-12-27 21:28:58 +00002561 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002568 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002569 {
2570 /*
2571 * Save IV in SSL3 and TLS1
2572 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002573 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2574 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002576#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Becker4c6876b2017-12-27 21:28:58 +00002578 /* Safe since data_len >= minlen + maclen + 1, so after having
2579 * subtracted at most minlen and maclen up to this point,
2580 * data_len > 0. */
2581 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002582
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583 if( auth_done == 1 )
2584 {
2585 correct *= ( rec->data_len >= padlen + 1 );
2586 padlen *= ( rec->data_len >= padlen + 1 );
2587 }
2588 else
Paul Bakker45829992013-01-03 14:52:21 +01002589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002591 if( rec->data_len < transform->maclen + padlen + 1 )
2592 {
2593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2594 rec->data_len,
2595 transform->maclen,
2596 padlen + 1 ) );
2597 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002598#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002599
2600 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2601 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
Hanno Becker4c6876b2017-12-27 21:28:58 +00002604 padlen++;
2605
2606 /* Regardless of the validity of the padding,
2607 * we have data_len >= padlen here. */
2608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002610 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002612 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614#if defined(MBEDTLS_SSL_DEBUG_ALL)
2615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002616 "should be no more than %d",
2617 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002618#endif
Paul Bakker45829992013-01-03 14:52:21 +01002619 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621 }
2622 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2624#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2625 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002626 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002628 /* The padding check involves a series of up to 256
2629 * consecutive memory reads at the end of the record
2630 * plaintext buffer. In order to hide the length and
2631 * validity of the padding, always perform exactly
2632 * `min(256,plaintext_len)` reads (but take into account
2633 * only the last `padlen` bytes for the padding check). */
2634 size_t pad_count = 0;
2635 size_t real_count = 0;
2636 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002637
Hanno Becker4c6876b2017-12-27 21:28:58 +00002638 /* Index of first padding byte; it has been ensured above
2639 * that the subtraction is safe. */
2640 size_t const padding_idx = rec->data_len - padlen;
2641 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2642 size_t const start_idx = rec->data_len - num_checks;
2643 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002644
Hanno Becker4c6876b2017-12-27 21:28:58 +00002645 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002646 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002647 real_count |= ( idx >= padding_idx );
2648 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002649 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002653 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002655#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002656 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002658 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002659#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2660 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002664 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002665
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 /* If the padding was found to be invalid, padlen == 0
2667 * and the subtraction is safe. If the padding was found valid,
2668 * padlen hasn't been changed and the previous assertion
2669 * data_len >= padlen still holds. */
2670 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002671 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002674 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2677 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002678 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002679
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002680#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002682 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002686 * Authenticate if not done yet.
2687 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002689#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002690 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002691 {
Hanno Becker992b6872017-11-09 18:57:39 +00002692 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002693
Hanno Becker4c6876b2017-12-27 21:28:58 +00002694 /* If the initial value of padlen was such that
2695 * data_len < maclen + padlen + 1, then padlen
2696 * got reset to 1, and the initial check
2697 * data_len >= minlen + maclen + 1
2698 * guarantees that at this point we still
2699 * have at least data_len >= maclen.
2700 *
2701 * If the initial value of padlen was such that
2702 * data_len >= maclen + padlen + 1, then we have
2703 * subtracted either padlen + 1 (if the padding was correct)
2704 * or 0 (if the padding was incorrect) since then,
2705 * hence data_len >= maclen in any case.
2706 */
2707 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
Hanno Beckere83efe62019-04-29 13:52:53 +01002709 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002712 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002714 ssl_mac( &transform->md_ctx_dec,
2715 transform->mac_dec,
2716 data, rec->data_len,
2717 rec->ctr, rec->type,
2718 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002719 }
2720 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2722#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2723 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002724 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002725 {
2726 /*
2727 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002728 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002729 *
2730 * Known timing attacks:
2731 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2732 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002733 * To compensate for different timings for the MAC calculation
2734 * depending on how much padding was removed (which is determined
2735 * by padlen), process extra_run more blocks through the hash
2736 * function.
2737 *
2738 * The formula in the paper is
2739 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2740 * where L1 is the size of the header plus the decrypted message
2741 * plus CBC padding and L2 is the size of the header plus the
2742 * decrypted message. This is for an underlying hash function
2743 * with 64-byte blocks.
2744 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2745 * correctly. We round down instead of up, so -56 is the correct
2746 * value for our calculations instead of -55.
2747 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002748 * Repeat the formula rather than defining a block_size variable.
2749 * This avoids requiring division by a variable at runtime
2750 * (which would be marginally less efficient and would require
2751 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002752 */
2753 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002754 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002755
2756 /*
2757 * The next two sizes are the minimum and maximum values of
2758 * in_msglen over all padlen values.
2759 *
2760 * They're independent of padlen, since we previously did
2761 * in_msglen -= padlen.
2762 *
2763 * Note that max_len + maclen is never more than the buffer
2764 * length, as we previously did in_msglen -= maclen too.
2765 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002766 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002767 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2768
Hanno Becker4c6876b2017-12-27 21:28:58 +00002769 memset( tmp, 0, sizeof( tmp ) );
2770
2771 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002772 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002773#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2774 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002775 case MBEDTLS_MD_MD5:
2776 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002777 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002779 extra_run =
2780 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2781 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002782 break;
2783#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002784#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002785 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002786 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002787 extra_run =
2788 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2789 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002790 break;
2791#endif
2792 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002794 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2795 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002796
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002797 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002798
Hanno Beckere83efe62019-04-29 13:52:53 +01002799 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2800 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002801 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2802 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002803 /* Make sure we access everything even when padlen > 0. This
2804 * makes the synchronisation requirements for just-in-time
2805 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002806 ssl_read_memory( data + rec->data_len, padlen );
2807 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002808
2809 /* Call mbedtls_md_process at least once due to cache attacks
2810 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002811 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002812 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002813
Hanno Becker4c6876b2017-12-27 21:28:58 +00002814 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815
2816 /* Make sure we access all the memory that could contain the MAC,
2817 * before we check it in the next code block. This makes the
2818 * synchronisation requirements for just-in-time Prime+Probe
2819 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 ssl_read_memory( data + min_len,
2821 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 }
2823 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002824#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2825 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002830
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002831#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002832 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2833 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002834#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
Hanno Becker4c6876b2017-12-27 21:28:58 +00002836 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2837 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#if defined(MBEDTLS_SSL_DEBUG_ALL)
2840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002841#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002842 correct = 0;
2843 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002844 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002845 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002846
2847 /*
2848 * Finally check the correct flag
2849 */
2850 if( correct == 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002852#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002853
2854 /* Make extra sure authentication was performed, exactly once */
2855 if( auth_done != 1 )
2856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2858 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002859 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Hanno Beckera5a2b082019-05-15 14:03:01 +01002861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002862 if( rec->cid_len != 0 )
2863 {
2864 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2865 &rec->type );
2866 if( ret != 0 )
2867 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2868 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002869#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
2873 return( 0 );
2874}
2875
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002876#undef MAC_NONE
2877#undef MAC_PLAINTEXT
2878#undef MAC_CIPHERTEXT
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881/*
2882 * Compression/decompression functions
2883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002885{
2886 int ret;
2887 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002888 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002889 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002890 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002894 if( len_pre == 0 )
2895 return( 0 );
2896
Paul Bakker2770fbd2012-07-03 13:30:23 +00002897 memcpy( msg_pre, ssl->out_msg, len_pre );
2898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002900 ssl->out_msglen ) );
2901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903 ssl->out_msg, ssl->out_msglen );
2904
Paul Bakker48916f92012-09-16 19:57:18 +00002905 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2906 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2907 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002908 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002909
Paul Bakker48916f92012-09-16 19:57:18 +00002910 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911 if( ret != Z_OK )
2912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2914 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002915 }
2916
Angus Grattond8213d02016-05-25 20:56:48 +10002917 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002918 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002921 ssl->out_msglen ) );
2922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924 ssl->out_msg, ssl->out_msglen );
2925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002927
2928 return( 0 );
2929}
2930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002932{
2933 int ret;
2934 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002935 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002936 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002937 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002940
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002941 if( len_pre == 0 )
2942 return( 0 );
2943
Paul Bakker2770fbd2012-07-03 13:30:23 +00002944 memcpy( msg_pre, ssl->in_msg, len_pre );
2945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 ssl->in_msglen ) );
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002950 ssl->in_msg, ssl->in_msglen );
2951
Paul Bakker48916f92012-09-16 19:57:18 +00002952 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2953 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2954 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002955 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002956 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959 if( ret != Z_OK )
2960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2962 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963 }
2964
Angus Grattond8213d02016-05-25 20:56:48 +10002965 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002966 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002969 ssl->in_msglen ) );
2970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002972 ssl->in_msg, ssl->in_msglen );
2973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002975
2976 return( 0 );
2977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002978#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2981static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#if defined(MBEDTLS_SSL_PROTO_DTLS)
2984static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002985{
2986 /* If renegotiation is not enforced, retransmit until we would reach max
2987 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002988 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002989 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002990 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002991 unsigned char doublings = 1;
2992
2993 while( ratio != 0 )
2994 {
2995 ++doublings;
2996 ratio >>= 1;
2997 }
2998
2999 if( ++ssl->renego_records_seen > doublings )
3000 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003002 return( 0 );
3003 }
3004 }
3005
3006 return( ssl_write_hello_request( ssl ) );
3007}
3008#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003009#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003010
Paul Bakker5121ce52009-01-03 21:22:43 +00003011/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003012 * Fill the input message buffer by appending data to it.
3013 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003014 *
3015 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3016 * available (from this read and/or a previous one). Otherwise, an error code
3017 * is returned (possibly EOF or WANT_READ).
3018 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003019 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3020 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3021 * since we always read a whole datagram at once.
3022 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003023 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003024 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027{
Paul Bakker23986e52011-04-24 08:57:21 +00003028 int ret;
3029 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003033 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003036 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003037 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003038 }
3039
Angus Grattond8213d02016-05-25 20:56:48 +10003040 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003044 }
3045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003047 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003049 uint32_t timeout;
3050
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003051 /* Just to be sure */
3052 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3053 {
3054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3055 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3057 }
3058
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 /*
3060 * The point is, we need to always read a full datagram at once, so we
3061 * sometimes read more then requested, and handle the additional data.
3062 * It could be the rest of the current record (while fetching the
3063 * header) and/or some other records in the same datagram.
3064 */
3065
3066 /*
3067 * Move to the next record in the already read datagram if applicable
3068 */
3069 if( ssl->next_record_offset != 0 )
3070 {
3071 if( ssl->in_left < ssl->next_record_offset )
3072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3074 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003075 }
3076
3077 ssl->in_left -= ssl->next_record_offset;
3078
3079 if( ssl->in_left != 0 )
3080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003082 ssl->next_record_offset ) );
3083 memmove( ssl->in_hdr,
3084 ssl->in_hdr + ssl->next_record_offset,
3085 ssl->in_left );
3086 }
3087
3088 ssl->next_record_offset = 0;
3089 }
3090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003093
3094 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003095 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 */
3097 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003100 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003101 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003102
3103 /*
3104 * A record can't be split accross datagrams. If we need to read but
3105 * are not at the beginning of a new record, the caller did something
3106 * wrong.
3107 */
3108 if( ssl->in_left != 0 )
3109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003112 }
3113
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003114 /*
3115 * Don't even try to read if time's out already.
3116 * This avoids by-passing the timer when repeatedly receiving messages
3117 * that will end up being dropped.
3118 */
3119 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003120 {
3121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003122 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003123 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003124 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003125 {
Angus Grattond8213d02016-05-25 20:56:48 +10003126 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 timeout = ssl->handshake->retransmit_timeout;
3130 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003131 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003135 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003136 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3137 timeout );
3138 else
3139 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003142
3143 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003145 }
3146
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003147 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003150 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003153 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003154 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003157 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003158 }
3159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003163 return( ret );
3164 }
3165
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003166 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003167 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003169 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003171 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003172 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003175 return( ret );
3176 }
3177
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003178 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003181 }
3182
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 if( ret < 0 )
3184 return( ret );
3185
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003186 ssl->in_left = ret;
3187 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003188 MBEDTLS_SSL_TRANSPORT_ELSE
3189#endif /* MBEDTLS_SSL_PROTO_DTLS */
3190#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003193 ssl->in_left, nb_want ) );
3194
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003195 while( ssl->in_left < nb_want )
3196 {
3197 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003198
3199 if( ssl_check_timer( ssl ) != 0 )
3200 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3201 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003202 {
3203 if( ssl->f_recv_timeout != NULL )
3204 {
3205 ret = ssl->f_recv_timeout( ssl->p_bio,
3206 ssl->in_hdr + ssl->in_left, len,
3207 ssl->conf->read_timeout );
3208 }
3209 else
3210 {
3211 ret = ssl->f_recv( ssl->p_bio,
3212 ssl->in_hdr + ssl->in_left, len );
3213 }
3214 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003217 ssl->in_left, nb_want ) );
3218 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003219
3220 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003222
3223 if( ret < 0 )
3224 return( ret );
3225
mohammad160352aecb92018-03-28 23:41:40 -07003226 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003227 {
Darryl Green11999bb2018-03-13 15:22:58 +00003228 MBEDTLS_SSL_DEBUG_MSG( 1,
3229 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003230 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3232 }
3233
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003234 ssl->in_left += ret;
3235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003237#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 return( 0 );
3242}
3243
3244/*
3245 * Flush any data not yet written
3246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003248{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003249 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003250 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003254 if( ssl->f_send == NULL )
3255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003257 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003259 }
3260
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003261 /* Avoid incrementing counter if data is flushed */
3262 if( ssl->out_left == 0 )
3263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003265 return( 0 );
3266 }
3267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 while( ssl->out_left > 0 )
3269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003271 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272
Hanno Becker2b1e3542018-08-06 11:19:13 +01003273 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003274 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277
3278 if( ret <= 0 )
3279 return( ret );
3280
mohammad160352aecb92018-03-28 23:41:40 -07003281 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003282 {
Darryl Green11999bb2018-03-13 15:22:58 +00003283 MBEDTLS_SSL_DEBUG_MSG( 1,
3284 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003285 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3287 }
3288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 ssl->out_left -= ret;
3290 }
3291
Hanno Becker2b1e3542018-08-06 11:19:13 +01003292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003293 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003294 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003295 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003296 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003297 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003298#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003299#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003300 {
3301 ssl->out_hdr = ssl->out_buf + 8;
3302 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003303#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003304 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 return( 0 );
3309}
3310
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003311/*
3312 * Functions to handle the DTLS retransmission state machine
3313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315/*
3316 * Append current handshake message to current outgoing flight
3317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003318static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3322 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3323 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003324
3325 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003326 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003327 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003330 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003331 }
3332
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003333 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003334 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003338 }
3339
3340 /* Copy current handshake message with headers */
3341 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3342 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003343 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003344 msg->next = NULL;
3345
3346 /* Append to the current flight */
3347 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003348 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349 else
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352 while( cur->next != NULL )
3353 cur = cur->next;
3354 cur->next = msg;
3355 }
3356
Hanno Becker3b235902018-08-06 09:54:53 +01003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003358 return( 0 );
3359}
3360
3361/*
3362 * Free the current flight of handshake messages
3363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003366 mbedtls_ssl_flight_item *cur = flight;
3367 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368
3369 while( cur != NULL )
3370 {
3371 next = cur->next;
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_free( cur->p );
3374 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003375
3376 cur = next;
3377 }
3378}
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3381static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003382#endif
3383
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003384/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 * Swap transform_out and out_ctr with the alternative ones
3386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003390 unsigned char tmp_out_ctr[8];
3391
3392 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003395 return;
3396 }
3397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003399
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003400 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003401 tmp_transform = ssl->transform_out;
3402 ssl->transform_out = ssl->handshake->alt_transform_out;
3403 ssl->handshake->alt_transform_out = tmp_transform;
3404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003405 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003406 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3407 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003408 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003409
3410 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003411 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3414 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3419 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003420 }
3421 }
3422#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003423}
3424
3425/*
3426 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003427 */
3428int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3429{
3430 int ret = 0;
3431
3432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3433
3434 ret = mbedtls_ssl_flight_transmit( ssl );
3435
3436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3437
3438 return( ret );
3439}
3440
3441/*
3442 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003443 *
3444 * Need to remember the current message in case flush_output returns
3445 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003448int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003449{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003454 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003456
3457 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003458 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003459 ssl_swap_epochs( ssl );
3460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003462 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003463
3464 while( ssl->handshake->cur_msg != NULL )
3465 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003466 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003467 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003468
Hanno Beckere1dcb032018-08-17 16:47:58 +01003469 int const is_finished =
3470 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3471 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3472
Hanno Becker04da1892018-08-14 13:22:10 +01003473 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3474 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3475
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003476 /* Swap epochs before sending Finished: we can't do it after
3477 * sending ChangeCipherSpec, in case write returns WANT_READ.
3478 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003479 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003480 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003482 ssl_swap_epochs( ssl );
3483 }
3484
Hanno Becker67bc7c32018-08-06 11:33:50 +01003485 ret = ssl_get_remaining_payload_in_datagram( ssl );
3486 if( ret < 0 )
3487 return( ret );
3488 max_frag_len = (size_t) ret;
3489
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003490 /* CCS is copied as is, while HS messages may need fragmentation */
3491 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3492 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003493 if( max_frag_len == 0 )
3494 {
3495 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3496 return( ret );
3497
3498 continue;
3499 }
3500
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003501 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003502 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003503 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003505 /* Update position inside current message */
3506 ssl->handshake->cur_msg_p += cur->len;
3507 }
3508 else
3509 {
3510 const unsigned char * const p = ssl->handshake->cur_msg_p;
3511 const size_t hs_len = cur->len - 12;
3512 const size_t frag_off = p - ( cur->p + 12 );
3513 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003514 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003515
Hanno Beckere1dcb032018-08-17 16:47:58 +01003516 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003517 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003518 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003519 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003520
Hanno Becker67bc7c32018-08-06 11:33:50 +01003521 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3522 return( ret );
3523
3524 continue;
3525 }
3526 max_hs_frag_len = max_frag_len - 12;
3527
3528 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3529 max_hs_frag_len : rem_len;
3530
3531 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003534 (unsigned) cur_hs_frag_len,
3535 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003536 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003537
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003538 /* Messages are stored with handshake headers as if not fragmented,
3539 * copy beginning of headers then fill fragmentation fields.
3540 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3541 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003542
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003543 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3544 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3545 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3546
Hanno Becker67bc7c32018-08-06 11:33:50 +01003547 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3548 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3549 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003550
3551 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3552
Hanno Becker3f7b9732018-08-28 09:53:25 +01003553 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003554 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3555 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003556 ssl->out_msgtype = cur->type;
3557
3558 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003559 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003560 }
3561
3562 /* If done with the current message move to the next one if any */
3563 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3564 {
3565 if( cur->next != NULL )
3566 {
3567 ssl->handshake->cur_msg = cur->next;
3568 ssl->handshake->cur_msg_p = cur->next->p + 12;
3569 }
3570 else
3571 {
3572 ssl->handshake->cur_msg = NULL;
3573 ssl->handshake->cur_msg_p = NULL;
3574 }
3575 }
3576
3577 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003578 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581 return( ret );
3582 }
3583 }
3584
Hanno Becker67bc7c32018-08-06 11:33:50 +01003585 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3586 return( ret );
3587
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003588 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3590 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003591 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003593 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003594 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3595 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003596
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598
3599 return( 0 );
3600}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601
3602/*
3603 * To be called when the last message of an incoming flight is received.
3604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606{
3607 /* We won't need to resend that one any more */
3608 ssl_flight_free( ssl->handshake->flight );
3609 ssl->handshake->flight = NULL;
3610 ssl->handshake->cur_msg = NULL;
3611
3612 /* The next incoming flight will start with this msg_seq */
3613 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3614
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003615 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003616 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003617
Hanno Becker0271f962018-08-16 13:23:47 +01003618 /* Clear future message buffering structure. */
3619 ssl_buffering_free( ssl );
3620
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003621 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003622 ssl_set_timer( ssl, 0 );
3623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3625 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003628 }
3629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003631}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632
3633/*
3634 * To be called when the last message of an outgoing flight is send.
3635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003637{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003638 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003639 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3642 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003645 }
3646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650
Paul Bakker5121ce52009-01-03 21:22:43 +00003651/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003652 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003654
3655/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003656 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 *
3658 * - fill in handshake headers
3659 * - update handshake checksum
3660 * - DTLS: save message for resending
3661 * - then pass to the record layer
3662 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003663 * DTLS: except for HelloRequest, messages are only queued, and will only be
3664 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003665 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003666 * Inputs:
3667 * - ssl->out_msglen: 4 + actual handshake message len
3668 * (4 is the size of handshake headers for TLS)
3669 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3670 * - ssl->out_msg + 4: the handshake message body
3671 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003672 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 * - ssl->out_msglen: the length of the record contents
3674 * (including handshake headers but excluding record headers)
3675 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003676 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003677int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003678{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003679 int ret;
3680 const size_t hs_len = ssl->out_msglen - 4;
3681 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3684
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003685 /*
3686 * Sanity checks
3687 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003688 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003689 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3690 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003691 /* In SSLv3, the client might send a NoCertificate alert. */
3692#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3693 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3694 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3695 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3696#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3697 {
3698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3700 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003703 /* Whenever we send anything different from a
3704 * HelloRequest we should be in a handshake - double check. */
3705 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3706 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003707 ssl->handshake == NULL )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003714 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003718 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003720 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003722
Hanno Beckerb50a2532018-08-06 11:52:54 +01003723 /* Double-check that we did not exceed the bounds
3724 * of the outgoing record buffer.
3725 * This should never fail as the various message
3726 * writing functions must obey the bounds of the
3727 * outgoing record buffer, but better be safe.
3728 *
3729 * Note: We deliberately do not check for the MTU or MFL here.
3730 */
3731 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3732 {
3733 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3734 "size %u, maximum %u",
3735 (unsigned) ssl->out_msglen,
3736 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3738 }
3739
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003740 /*
3741 * Fill handshake headers
3742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003745 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3746 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3747 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003749 /*
3750 * DTLS has additional fields in the Handshake layer,
3751 * between the length field and the actual payload:
3752 * uint16 message_seq;
3753 * uint24 fragment_offset;
3754 * uint24 fragment_length;
3755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003757 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003758 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003759 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003760 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003761 {
3762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3763 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003764 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003765 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3767 }
3768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003770 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003771
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003772 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003773 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003774 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003775 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3776 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3777 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003778 }
3779 else
3780 {
3781 ssl->out_msg[4] = 0;
3782 ssl->out_msg[5] = 0;
3783 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003784
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003785 /* Handshake hashes are computed without fragmentation,
3786 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003787 memset( ssl->out_msg + 6, 0x00, 3 );
3788 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003790#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003791
Hanno Becker0207e532018-08-28 10:28:28 +01003792 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003793 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3794 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 }
3796
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003799 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003800 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3801 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802 {
3803 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003806 return( ret );
3807 }
3808 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003809 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003810#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003811 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003813 {
3814 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3815 return( ret );
3816 }
3817 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003818
3819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3820
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003821 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003822}
3823
3824/*
3825 * Record layer functions
3826 */
3827
3828/*
3829 * Write current record.
3830 *
3831 * Uses:
3832 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3833 * - ssl->out_msglen: length of the record content (excl headers)
3834 * - ssl->out_msg: record content
3835 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003836int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003837{
3838 int ret, done = 0;
3839 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003841
3842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003845 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003847 {
3848 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003851 return( ret );
3852 }
3853
3854 len = ssl->out_msglen;
3855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003858#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3859 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 ret = mbedtls_ssl_hw_record_write( ssl );
3864 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3867 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003868 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003869
3870 if( ret == 0 )
3871 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003872 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 if( !done )
3875 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003876 unsigned i;
3877 size_t protected_record_size;
3878
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003879 /* Skip writing the record content type to after the encryption,
3880 * as it may change when using the CID extension. */
3881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003883 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003884
Hanno Becker19859472018-08-06 09:40:20 +01003885 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003886 ssl->out_len[0] = (unsigned char)( len >> 8 );
3887 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003888
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003890 {
Hanno Becker3307b532017-12-27 21:37:21 +00003891 mbedtls_record rec;
3892
3893 rec.buf = ssl->out_iv;
3894 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3895 ( ssl->out_iv - ssl->out_buf );
3896 rec.data_len = ssl->out_msglen;
3897 rec.data_offset = ssl->out_msg - rec.buf;
3898
3899 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3900 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3901 ssl->conf->transport, rec.ver );
3902 rec.type = ssl->out_msgtype;
3903
Hanno Beckera5a2b082019-05-15 14:03:01 +01003904#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003905 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003906 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003907#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003908
Hanno Becker611a83b2018-01-03 14:27:32 +00003909 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003910 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003913 return( ret );
3914 }
3915
Hanno Becker3307b532017-12-27 21:37:21 +00003916 if( rec.data_offset != 0 )
3917 {
3918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3920 }
3921
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003922 /* Update the record content type and CID. */
3923 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003924#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003925 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003926#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003927 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003928 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3929 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003930 }
3931
Hanno Becker43395762019-05-03 14:46:38 +01003932 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003933
3934#if defined(MBEDTLS_SSL_PROTO_DTLS)
3935 /* In case of DTLS, double-check that we don't exceed
3936 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003937 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003938 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003939 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003940 if( ret < 0 )
3941 return( ret );
3942
3943 if( protected_record_size > (size_t) ret )
3944 {
3945 /* Should never happen */
3946 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3947 }
3948 }
3949#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003950
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003951 /* Now write the potentially updated record content type. */
3952 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003955 "version = [%d:%d], msglen = %d",
3956 ssl->out_hdr[0], ssl->out_hdr[1],
3957 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003960 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003961
3962 ssl->out_left += protected_record_size;
3963 ssl->out_hdr += protected_record_size;
3964 ssl_update_out_pointers( ssl, ssl->transform_out );
3965
Hanno Becker04484622018-08-06 09:49:38 +01003966 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3967 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3968 break;
3969
3970 /* The loop goes to its end iff the counter is wrapping */
3971 if( i == ssl_ep_len( ssl ) )
3972 {
3973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3974 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003976 }
3977
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003979 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003980 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003982 size_t remaining;
3983 ret = ssl_get_remaining_payload_in_datagram( ssl );
3984 if( ret < 0 )
3985 {
3986 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3987 ret );
3988 return( ret );
3989 }
3990
3991 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003993 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003994 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003995 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003996 else
3997 {
Hanno Becker513815a2018-08-20 11:56:09 +01003998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003999 }
4000 }
4001#endif /* MBEDTLS_SSL_PROTO_DTLS */
4002
4003 if( ( flush == SSL_FORCE_FLUSH ) &&
4004 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004006 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007 return( ret );
4008 }
4009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
4012 return( 0 );
4013}
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004016
4017static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4018{
4019 if( ssl->in_msglen < ssl->in_hslen ||
4020 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4021 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4022 {
4023 return( 1 );
4024 }
4025 return( 0 );
4026}
Hanno Becker44650b72018-08-16 12:51:11 +01004027
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004028static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004029{
4030 return( ( ssl->in_msg[9] << 16 ) |
4031 ( ssl->in_msg[10] << 8 ) |
4032 ssl->in_msg[11] );
4033}
4034
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004035static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004036{
4037 return( ( ssl->in_msg[6] << 16 ) |
4038 ( ssl->in_msg[7] << 8 ) |
4039 ssl->in_msg[8] );
4040}
4041
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004042static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004043{
4044 uint32_t msg_len, frag_off, frag_len;
4045
4046 msg_len = ssl_get_hs_total_len( ssl );
4047 frag_off = ssl_get_hs_frag_off( ssl );
4048 frag_len = ssl_get_hs_frag_len( ssl );
4049
4050 if( frag_off > msg_len )
4051 return( -1 );
4052
4053 if( frag_len > msg_len - frag_off )
4054 return( -1 );
4055
4056 if( frag_len + 12 > ssl->in_msglen )
4057 return( -1 );
4058
4059 return( 0 );
4060}
4061
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004062/*
4063 * Mark bits in bitmask (used for DTLS HS reassembly)
4064 */
4065static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4066{
4067 unsigned int start_bits, end_bits;
4068
4069 start_bits = 8 - ( offset % 8 );
4070 if( start_bits != 8 )
4071 {
4072 size_t first_byte_idx = offset / 8;
4073
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004074 /* Special case */
4075 if( len <= start_bits )
4076 {
4077 for( ; len != 0; len-- )
4078 mask[first_byte_idx] |= 1 << ( start_bits - len );
4079
4080 /* Avoid potential issues with offset or len becoming invalid */
4081 return;
4082 }
4083
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004084 offset += start_bits; /* Now offset % 8 == 0 */
4085 len -= start_bits;
4086
4087 for( ; start_bits != 0; start_bits-- )
4088 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4089 }
4090
4091 end_bits = len % 8;
4092 if( end_bits != 0 )
4093 {
4094 size_t last_byte_idx = ( offset + len ) / 8;
4095
4096 len -= end_bits; /* Now len % 8 == 0 */
4097
4098 for( ; end_bits != 0; end_bits-- )
4099 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4100 }
4101
4102 memset( mask + offset / 8, 0xFF, len / 8 );
4103}
4104
4105/*
4106 * Check that bitmask is full
4107 */
4108static int ssl_bitmask_check( unsigned char *mask, size_t len )
4109{
4110 size_t i;
4111
4112 for( i = 0; i < len / 8; i++ )
4113 if( mask[i] != 0xFF )
4114 return( -1 );
4115
4116 for( i = 0; i < len % 8; i++ )
4117 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4118 return( -1 );
4119
4120 return( 0 );
4121}
4122
Hanno Becker56e205e2018-08-16 09:06:12 +01004123/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004124static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004125 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004126{
Hanno Becker56e205e2018-08-16 09:06:12 +01004127 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004128
Hanno Becker56e205e2018-08-16 09:06:12 +01004129 alloc_len = 12; /* Handshake header */
4130 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004131
Hanno Beckerd07df862018-08-16 09:14:58 +01004132 if( add_bitmap )
4133 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004134
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004135 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004136}
Hanno Becker56e205e2018-08-16 09:06:12 +01004137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004138#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004139
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004140static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004141{
4142 return( ( ssl->in_msg[1] << 16 ) |
4143 ( ssl->in_msg[2] << 8 ) |
4144 ssl->in_msg[3] );
4145}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004146
Simon Butcher99000142016-10-13 17:21:01 +01004147int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004152 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004154 }
4155
Hanno Becker12555c62018-08-16 12:47:53 +01004156 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004159 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004160 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004163 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004164 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004165 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004166 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004167
Hanno Becker44650b72018-08-16 12:51:11 +01004168 if( ssl_check_hs_header( ssl ) != 0 )
4169 {
4170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4172 }
4173
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004174 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004175 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4176 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4177 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4178 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004179 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004180 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4181 {
4182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4183 recv_msg_seq,
4184 ssl->handshake->in_msg_seq ) );
4185 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4186 }
4187
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004188 /* Retransmit only on last message from previous flight, to avoid
4189 * too many retransmissions.
4190 * Besides, No sane server ever retransmits HelloVerifyRequest */
4191 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004192 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004195 "message_seq = %d, start_of_flight = %d",
4196 recv_msg_seq,
4197 ssl->handshake->in_flight_start_seq ) );
4198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004202 return( ret );
4203 }
4204 }
4205 else
4206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004208 "message_seq = %d, expected = %d",
4209 recv_msg_seq,
4210 ssl->handshake->in_msg_seq ) );
4211 }
4212
Hanno Becker90333da2017-10-10 11:27:13 +01004213 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004214 }
4215 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004216
Hanno Becker6d97ef52018-08-16 13:09:04 +01004217 /* Message reassembly is handled alongside buffering of future
4218 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004219 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004220 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004221 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004224 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004225 }
4226 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004227 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004229#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004230 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004231 /* With TLS we don't handle fragmentation (for now) */
4232 if( ssl->in_msglen < ssl->in_hslen )
4233 {
4234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4235 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4236 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004237 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004238#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004239
Simon Butcher99000142016-10-13 17:21:01 +01004240 return( 0 );
4241}
4242
4243void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4244{
Hanno Becker0271f962018-08-16 13:23:47 +01004245 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004246
Hanno Becker0271f962018-08-16 13:23:47 +01004247 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004248 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004249 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004250 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004251
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004252 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004253#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004254 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004255 ssl->handshake != NULL )
4256 {
Hanno Becker0271f962018-08-16 13:23:47 +01004257 unsigned offset;
4258 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004259
Hanno Becker0271f962018-08-16 13:23:47 +01004260 /* Increment handshake sequence number */
4261 hs->in_msg_seq++;
4262
4263 /*
4264 * Clear up handshake buffering and reassembly structure.
4265 */
4266
4267 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004268 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004269
4270 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004271 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4272 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004273 offset++, hs_buf++ )
4274 {
4275 *hs_buf = *(hs_buf + 1);
4276 }
4277
4278 /* Create a fresh last entry */
4279 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004280 }
4281#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004282}
4283
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004284/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004285 * DTLS anti-replay: RFC 6347 4.1.2.6
4286 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004287 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4288 * Bit n is set iff record number in_window_top - n has been seen.
4289 *
4290 * Usually, in_window_top is the last record number seen and the lsb of
4291 * in_window is set. The only exception is the initial state (record number 0
4292 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004294#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4295static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004296{
4297 ssl->in_window_top = 0;
4298 ssl->in_window = 0;
4299}
4300
4301static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4302{
4303 return( ( (uint64_t) buf[0] << 40 ) |
4304 ( (uint64_t) buf[1] << 32 ) |
4305 ( (uint64_t) buf[2] << 24 ) |
4306 ( (uint64_t) buf[3] << 16 ) |
4307 ( (uint64_t) buf[4] << 8 ) |
4308 ( (uint64_t) buf[5] ) );
4309}
4310
4311/*
4312 * Return 0 if sequence number is acceptable, -1 otherwise
4313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004314int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004315{
4316 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4317 uint64_t bit;
4318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004319 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004320 return( 0 );
4321
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004322 if( rec_seqnum > ssl->in_window_top )
4323 return( 0 );
4324
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004325 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004326
4327 if( bit >= 64 )
4328 return( -1 );
4329
4330 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4331 return( -1 );
4332
4333 return( 0 );
4334}
4335
4336/*
4337 * Update replay window on new validated record
4338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004339void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004340{
4341 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4342
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004343 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004344 return;
4345
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004346 if( rec_seqnum > ssl->in_window_top )
4347 {
4348 /* Update window_top and the contents of the window */
4349 uint64_t shift = rec_seqnum - ssl->in_window_top;
4350
4351 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004352 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004353 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004354 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004356 ssl->in_window |= 1;
4357 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004358
4359 ssl->in_window_top = rec_seqnum;
4360 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004361 else
4362 {
4363 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004364 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004365
4366 if( bit < 64 ) /* Always true, but be extra sure */
4367 ssl->in_window |= (uint64_t) 1 << bit;
4368 }
4369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004370#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004371
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004372#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004373/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004374static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4375
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004376/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004377 * Without any SSL context, check if a datagram looks like a ClientHello with
4378 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004379 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004380 *
4381 * - if cookie is valid, return 0
4382 * - if ClientHello looks superficially valid but cookie is not,
4383 * fill obuf and set olen, then
4384 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4385 * - otherwise return a specific error code
4386 */
4387static int ssl_check_dtls_clihlo_cookie(
4388 mbedtls_ssl_cookie_write_t *f_cookie_write,
4389 mbedtls_ssl_cookie_check_t *f_cookie_check,
4390 void *p_cookie,
4391 const unsigned char *cli_id, size_t cli_id_len,
4392 const unsigned char *in, size_t in_len,
4393 unsigned char *obuf, size_t buf_len, size_t *olen )
4394{
4395 size_t sid_len, cookie_len;
4396 unsigned char *p;
4397
4398 if( f_cookie_write == NULL || f_cookie_check == NULL )
4399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4400
4401 /*
4402 * Structure of ClientHello with record and handshake headers,
4403 * and expected values. We don't need to check a lot, more checks will be
4404 * done when actually parsing the ClientHello - skipping those checks
4405 * avoids code duplication and does not make cookie forging any easier.
4406 *
4407 * 0-0 ContentType type; copied, must be handshake
4408 * 1-2 ProtocolVersion version; copied
4409 * 3-4 uint16 epoch; copied, must be 0
4410 * 5-10 uint48 sequence_number; copied
4411 * 11-12 uint16 length; (ignored)
4412 *
4413 * 13-13 HandshakeType msg_type; (ignored)
4414 * 14-16 uint24 length; (ignored)
4415 * 17-18 uint16 message_seq; copied
4416 * 19-21 uint24 fragment_offset; copied, must be 0
4417 * 22-24 uint24 fragment_length; (ignored)
4418 *
4419 * 25-26 ProtocolVersion client_version; (ignored)
4420 * 27-58 Random random; (ignored)
4421 * 59-xx SessionID session_id; 1 byte len + sid_len content
4422 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4423 * ...
4424 *
4425 * Minimum length is 61 bytes.
4426 */
4427 if( in_len < 61 ||
4428 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4429 in[3] != 0 || in[4] != 0 ||
4430 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4431 {
4432 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4433 }
4434
4435 sid_len = in[59];
4436 if( sid_len > in_len - 61 )
4437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4438
4439 cookie_len = in[60 + sid_len];
4440 if( cookie_len > in_len - 60 )
4441 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4442
4443 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4444 cli_id, cli_id_len ) == 0 )
4445 {
4446 /* Valid cookie */
4447 return( 0 );
4448 }
4449
4450 /*
4451 * If we get here, we've got an invalid cookie, let's prepare HVR.
4452 *
4453 * 0-0 ContentType type; copied
4454 * 1-2 ProtocolVersion version; copied
4455 * 3-4 uint16 epoch; copied
4456 * 5-10 uint48 sequence_number; copied
4457 * 11-12 uint16 length; olen - 13
4458 *
4459 * 13-13 HandshakeType msg_type; hello_verify_request
4460 * 14-16 uint24 length; olen - 25
4461 * 17-18 uint16 message_seq; copied
4462 * 19-21 uint24 fragment_offset; copied
4463 * 22-24 uint24 fragment_length; olen - 25
4464 *
4465 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4466 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4467 *
4468 * Minimum length is 28.
4469 */
4470 if( buf_len < 28 )
4471 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4472
4473 /* Copy most fields and adapt others */
4474 memcpy( obuf, in, 25 );
4475 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4476 obuf[25] = 0xfe;
4477 obuf[26] = 0xff;
4478
4479 /* Generate and write actual cookie */
4480 p = obuf + 28;
4481 if( f_cookie_write( p_cookie,
4482 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4483 {
4484 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4485 }
4486
4487 *olen = p - obuf;
4488
4489 /* Go back and fill length fields */
4490 obuf[27] = (unsigned char)( *olen - 28 );
4491
4492 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4493 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4494 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4495
4496 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4497 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4498
4499 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4500}
4501
4502/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004503 * Handle possible client reconnect with the same UDP quadruplet
4504 * (RFC 6347 Section 4.2.8).
4505 *
4506 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4507 * that looks like a ClientHello.
4508 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509 * - if the input looks like a ClientHello without cookies,
4510 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004511 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004512 * - if the input looks like a ClientHello with a valid cookie,
4513 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004514 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004515 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004516 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004517 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004518 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4519 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004520 */
4521static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4522{
4523 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004524 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004525
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004526 ret = ssl_check_dtls_clihlo_cookie(
4527 ssl->conf->f_cookie_write,
4528 ssl->conf->f_cookie_check,
4529 ssl->conf->p_cookie,
4530 ssl->cli_id, ssl->cli_id_len,
4531 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004532 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004533
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004534 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4535
4536 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004537 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004538 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004539 * If the error is permanent we'll catch it later,
4540 * if it's not, then hopefully it'll work next time. */
4541 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4542
4543 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004544 }
4545
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004546 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004547 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004548 /* Got a valid cookie, partially reset context */
4549 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4550 {
4551 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4552 return( ret );
4553 }
4554
4555 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004556 }
4557
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004558 return( ret );
4559}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004560#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004561
Hanno Becker46483f12019-05-03 13:25:54 +01004562static int ssl_check_record_type( uint8_t record_type )
4563{
4564 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4565 record_type != MBEDTLS_SSL_MSG_ALERT &&
4566 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4567 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4568 {
4569 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4570 }
4571
4572 return( 0 );
4573}
4574
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004575/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004576 * ContentType type;
4577 * ProtocolVersion version;
4578 * uint16 epoch; // DTLS only
4579 * uint48 sequence_number; // DTLS only
4580 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004581 *
4582 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004583 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004584 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4585 *
4586 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004587 * 1. proceed with the record if this function returns 0
4588 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4589 * 3. return CLIENT_RECONNECT if this function return that value
4590 * 4. drop the whole datagram if this function returns anything else.
4591 * Point 2 is needed when the peer is resending, and we have already received
4592 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004595{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004596 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004597 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004598
Hanno Becker8b09b732019-05-08 12:03:28 +01004599 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004600
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004602 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004603
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004604 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004606 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004607 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4608 ssl->conf->cid_len != 0 )
4609 {
4610 /* Shift pointers to account for record header including CID
4611 * struct {
4612 * ContentType special_type = tls12_cid;
4613 * ProtocolVersion version;
4614 * uint16 epoch;
4615 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004616 * opaque cid[cid_length]; // Additional field compared to
4617 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004618 * uint16 length;
4619 * opaque enc_content[DTLSCiphertext.length];
4620 * } DTLSCiphertext;
4621 */
4622
4623 /* So far, we only support static CID lengths
4624 * fixed in the configuration. */
4625 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4626 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4627 }
4628 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004629#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004630 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004633
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004634#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004635 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004636 * Section 4.1.2.7, that is, send alert only with TLS */
4637 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4638 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004639 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4640 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004641 }
4642#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004645 }
4646
4647 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004648 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4651 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 }
4653
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004654 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
4659
Hanno Becker8b09b732019-05-08 12:03:28 +01004660 /* Now that the total length of the record header is known, ensure
4661 * that the current datagram is large enough to hold it.
4662 * This would fail, for example, if we received a datagram of
4663 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4664 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4665 if( ret != 0 )
4666 {
4667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4668 return( ret );
4669 }
4670 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4671
4672 /* Parse and validate record length
4673 * This must happen after the CID parsing because
4674 * its position in the record header depends on
4675 * the presence of a CID. */
4676
4677 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004678 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004679 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004683 }
4684
Hanno Becker8b09b732019-05-08 12:03:28 +01004685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004686 "version = [%d:%d], msglen = %d",
4687 ssl->in_msgtype,
4688 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004689
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004690 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004691 * DTLS-related tests.
4692 * Check epoch before checking length constraint because
4693 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4694 * message gets duplicated before the corresponding Finished message,
4695 * the second ChangeCipherSpec should be discarded because it belongs
4696 * to an old epoch, but not because its length is shorter than
4697 * the minimum record length for packets using the new record transform.
4698 * Note that these two kinds of failures are handled differently,
4699 * as an unexpected record is silently skipped but an invalid
4700 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004701 */
4702#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004703 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004704 {
4705 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4706
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004707 /* Check epoch (and sequence number) with DTLS */
4708 if( rec_epoch != ssl->in_epoch )
4709 {
4710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4711 "expected %d, received %d",
4712 ssl->in_epoch, rec_epoch ) );
4713
4714#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4715 /*
4716 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4717 * access the first byte of record content (handshake type), as we
4718 * have an active transform (possibly iv_len != 0), so use the
4719 * fact that the record header len is 13 instead.
4720 */
4721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4722 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4723 rec_epoch == 0 &&
4724 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4725 ssl->in_left > 13 &&
4726 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4727 {
4728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4729 "from the same port" ) );
4730 return( ssl_handle_possible_reconnect( ssl ) );
4731 }
4732 else
4733#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004734 {
4735 /* Consider buffering the record. */
4736 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4737 {
4738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4739 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4740 }
4741
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004742 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004743 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004744 }
4745
4746#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4747 /* Replay detection only works for the current epoch */
4748 if( rec_epoch == ssl->in_epoch &&
4749 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4750 {
4751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4752 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4753 }
4754#endif
4755 }
4756#endif /* MBEDTLS_SSL_PROTO_DTLS */
4757
Hanno Becker52c6dc62017-05-26 16:07:36 +01004758
4759 /* Check length against bounds of the current transform and version */
4760 if( ssl->transform_in == NULL )
4761 {
4762 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004764 {
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4766 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4767 }
4768 }
4769 else
4770 {
4771 if( ssl->in_msglen < ssl->transform_in->minlen )
4772 {
4773 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4775 }
4776
4777#if defined(MBEDTLS_SSL_PROTO_SSL3)
4778 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004779 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004780 {
4781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4782 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4783 }
4784#endif
4785#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4786 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4787 /*
4788 * TLS encrypted messages can have up to 256 bytes of padding
4789 */
4790 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4791 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004792 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004793 {
4794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4795 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4796 }
4797#endif
4798 }
4799
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004800 return( 0 );
4801}
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004803/*
4804 * If applicable, decrypt (and decompress) record content
4805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004806static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004807{
4808 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004810 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004811 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4814 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 ret = mbedtls_ssl_hw_record_read( ssl );
4819 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4822 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004823 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004824
4825 if( ret == 0 )
4826 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004827 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004829 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004831 mbedtls_record rec;
4832
4833 rec.buf = ssl->in_iv;
4834 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4835 - ( ssl->in_iv - ssl->in_buf );
4836 rec.data_len = ssl->in_msglen;
4837 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004838#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004839 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004840 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004841#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004842
4843 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4844 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4845 ssl->conf->transport, rec.ver );
4846 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004847 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4848 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004851
Hanno Beckera5a2b082019-05-15 14:03:01 +01004852#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004853 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4854 ssl->conf->ignore_unexpected_cid
4855 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4856 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004857 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004858 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004859 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004860#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004861
Paul Bakker5121ce52009-01-03 21:22:43 +00004862 return( ret );
4863 }
4864
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004865 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004866 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004867 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4868 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004871 /* The record content type may change during decryption,
4872 * so re-read it. */
4873 ssl->in_msgtype = rec.type;
4874 /* Also update the input buffer, because unfortunately
4875 * the server-side ssl_parse_client_hello() reparses the
4876 * record header when receiving a ClientHello initiating
4877 * a renegotiation. */
4878 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004879 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004880 ssl->in_msglen = rec.data_len;
4881 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4882 ssl->in_len[1] = (unsigned char)( rec.data_len );
4883
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4885 ssl->in_msg, ssl->in_msglen );
4886
Hanno Beckera5a2b082019-05-15 14:03:01 +01004887#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004888 /* We have already checked the record content type
4889 * in ssl_parse_record_header(), failing or silently
4890 * dropping the record in the case of an unknown type.
4891 *
4892 * Since with the use of CIDs, the record content type
4893 * might change during decryption, re-check the record
4894 * content type, but treat a failure as fatal this time. */
4895 if( ssl_check_record_type( ssl->in_msgtype ) )
4896 {
4897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4899 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004900#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004901
Angus Grattond8213d02016-05-25 20:56:48 +10004902 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004907 else if( ssl->in_msglen == 0 )
4908 {
4909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4910 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4911 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4912 {
4913 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4915 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4916 }
4917#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4918
4919 ssl->nb_zero++;
4920
4921 /*
4922 * Three or more empty messages may be a DoS attack
4923 * (excessive CPU consumption).
4924 */
4925 if( ssl->nb_zero > 3 )
4926 {
4927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004928 "messages, possible DoS attack" ) );
4929 /* Treat the records as if they were not properly authenticated,
4930 * thereby failing the connection if we see more than allowed
4931 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004932 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4933 }
4934 }
4935 else
4936 ssl->nb_zero = 0;
4937
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4939#if defined(MBEDTLS_SSL_PROTO_TLS)
4940 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004941 {
4942 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004943 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004944 if( ++ssl->in_ctr[i - 1] != 0 )
4945 break;
4946
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004947 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004948 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4951 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4952 }
4953 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004954#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004955
Paul Bakker5121ce52009-01-03 21:22:43 +00004956 }
4957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004959 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004961 {
4962 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004965 return( ret );
4966 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004971 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004974 }
4975#endif
4976
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004977 return( 0 );
4978}
4979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004981
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004982/*
4983 * Read a record.
4984 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004985 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4986 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4987 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004988 */
Hanno Becker1097b342018-08-15 14:09:41 +01004989
4990/* Helper functions for mbedtls_ssl_read_record(). */
4991static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004992static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4993static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004994
Hanno Becker327c93b2018-08-15 13:56:18 +01004995int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004996 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004997{
4998 int ret;
4999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005002 if( ssl->keep_current_message == 0 )
5003 {
5004 do {
Simon Butcher99000142016-10-13 17:21:01 +01005005
Hanno Becker26994592018-08-15 14:14:59 +01005006 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005007 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005008 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005009
Hanno Beckere74d5562018-08-15 14:26:08 +01005010 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005011 {
Hanno Becker40f50842018-08-15 14:48:01 +01005012#if defined(MBEDTLS_SSL_PROTO_DTLS)
5013 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005014
Hanno Becker40f50842018-08-15 14:48:01 +01005015 /* We only check for buffered messages if the
5016 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005017 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005018 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005019 {
Hanno Becker40f50842018-08-15 14:48:01 +01005020 if( ssl_load_buffered_message( ssl ) == 0 )
5021 have_buffered = 1;
5022 }
5023
5024 if( have_buffered == 0 )
5025#endif /* MBEDTLS_SSL_PROTO_DTLS */
5026 {
5027 ret = ssl_get_next_record( ssl );
5028 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5029 continue;
5030
5031 if( ret != 0 )
5032 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005033 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005034 return( ret );
5035 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005036 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005037 }
5038
5039 ret = mbedtls_ssl_handle_message_type( ssl );
5040
Hanno Becker40f50842018-08-15 14:48:01 +01005041#if defined(MBEDTLS_SSL_PROTO_DTLS)
5042 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5043 {
5044 /* Buffer future message */
5045 ret = ssl_buffer_message( ssl );
5046 if( ret != 0 )
5047 return( ret );
5048
5049 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5050 }
5051#endif /* MBEDTLS_SSL_PROTO_DTLS */
5052
Hanno Becker90333da2017-10-10 11:27:13 +01005053 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5054 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055
5056 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005057 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005058 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005059 return( ret );
5060 }
5061
Hanno Becker327c93b2018-08-15 13:56:18 +01005062 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005063 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005064 {
5065 mbedtls_ssl_update_handshake_status( ssl );
5066 }
Simon Butcher99000142016-10-13 17:21:01 +01005067 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005068 else
Simon Butcher99000142016-10-13 17:21:01 +01005069 {
Hanno Becker02f59072018-08-15 14:00:24 +01005070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005071 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005072 }
5073
5074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5075
5076 return( 0 );
5077}
5078
Hanno Becker40f50842018-08-15 14:48:01 +01005079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005080static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005081{
Hanno Becker40f50842018-08-15 14:48:01 +01005082 if( ssl->in_left > ssl->next_record_offset )
5083 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005084
Hanno Becker40f50842018-08-15 14:48:01 +01005085 return( 0 );
5086}
5087
5088static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5089{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005090 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005091 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005092 int ret = 0;
5093
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005094 if( hs == NULL )
5095 return( -1 );
5096
Hanno Beckere00ae372018-08-20 09:39:42 +01005097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5098
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005099 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5100 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5101 {
5102 /* Check if we have seen a ChangeCipherSpec before.
5103 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005104 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 {
5106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5107 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005108 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005109 }
5110
Hanno Becker39b8bc92018-08-28 17:17:13 +01005111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005112 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5113 ssl->in_msglen = 1;
5114 ssl->in_msg[0] = 1;
5115
5116 /* As long as they are equal, the exact value doesn't matter. */
5117 ssl->in_left = 0;
5118 ssl->next_record_offset = 0;
5119
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005120 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005121 goto exit;
5122 }
Hanno Becker37f95322018-08-16 13:55:32 +01005123
Hanno Beckerb8f50142018-08-28 10:01:34 +01005124#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005125 /* Debug only */
5126 {
5127 unsigned offset;
5128 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5129 {
5130 hs_buf = &hs->buffering.hs[offset];
5131 if( hs_buf->is_valid == 1 )
5132 {
5133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5134 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005135 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005136 }
5137 }
5138 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005139#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005140
5141 /* Check if we have buffered and/or fully reassembled the
5142 * next handshake message. */
5143 hs_buf = &hs->buffering.hs[0];
5144 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5145 {
5146 /* Synthesize a record containing the buffered HS message. */
5147 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5148 ( hs_buf->data[2] << 8 ) |
5149 hs_buf->data[3];
5150
5151 /* Double-check that we haven't accidentally buffered
5152 * a message that doesn't fit into the input buffer. */
5153 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5154 {
5155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5156 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5157 }
5158
5159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5160 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5161 hs_buf->data, msg_len + 12 );
5162
5163 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5164 ssl->in_hslen = msg_len + 12;
5165 ssl->in_msglen = msg_len + 12;
5166 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5167
5168 ret = 0;
5169 goto exit;
5170 }
5171 else
5172 {
5173 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5174 hs->in_msg_seq ) );
5175 }
5176
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005177 ret = -1;
5178
5179exit:
5180
5181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5182 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005183}
5184
Hanno Beckera02b0b42018-08-21 17:20:27 +01005185static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5186 size_t desired )
5187{
5188 int offset;
5189 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5191 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005192
Hanno Becker01315ea2018-08-21 17:22:17 +01005193 /* Get rid of future records epoch first, if such exist. */
5194 ssl_free_buffered_record( ssl );
5195
5196 /* Check if we have enough space available now. */
5197 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5198 hs->buffering.total_bytes_buffered ) )
5199 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005201 return( 0 );
5202 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005203
Hanno Becker4f432ad2018-08-28 10:02:32 +01005204 /* We don't have enough space to buffer the next expected handshake
5205 * message. Remove buffers used for future messages to gain space,
5206 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005207 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5208 offset >= 0; offset-- )
5209 {
5210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5211 offset ) );
5212
Hanno Beckerb309b922018-08-23 13:18:05 +01005213 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005214
5215 /* Check if we have enough space available now. */
5216 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5217 hs->buffering.total_bytes_buffered ) )
5218 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005220 return( 0 );
5221 }
5222 }
5223
5224 return( -1 );
5225}
5226
Hanno Becker40f50842018-08-15 14:48:01 +01005227static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5228{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005229 int ret = 0;
5230 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5231
5232 if( hs == NULL )
5233 return( 0 );
5234
5235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5236
5237 switch( ssl->in_msgtype )
5238 {
5239 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005241
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005242 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005243 break;
5244
5245 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005246 {
5247 unsigned recv_msg_seq_offset;
5248 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5249 mbedtls_ssl_hs_buffer *hs_buf;
5250 size_t msg_len = ssl->in_hslen - 12;
5251
5252 /* We should never receive an old handshake
5253 * message - double-check nonetheless. */
5254 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5255 {
5256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5258 }
5259
5260 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5261 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5262 {
5263 /* Silently ignore -- message too far in the future */
5264 MBEDTLS_SSL_DEBUG_MSG( 2,
5265 ( "Ignore future HS message with sequence number %u, "
5266 "buffering window %u - %u",
5267 recv_msg_seq, ssl->handshake->in_msg_seq,
5268 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5269
5270 goto exit;
5271 }
5272
5273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5274 recv_msg_seq, recv_msg_seq_offset ) );
5275
5276 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5277
5278 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005279 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005280 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005281 size_t reassembly_buf_sz;
5282
Hanno Becker37f95322018-08-16 13:55:32 +01005283 hs_buf->is_fragmented =
5284 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5285
5286 /* We copy the message back into the input buffer
5287 * after reassembly, so check that it's not too large.
5288 * This is an implementation-specific limitation
5289 * and not one from the standard, hence it is not
5290 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005291 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005292 {
5293 /* Ignore message */
5294 goto exit;
5295 }
5296
Hanno Beckere0b150f2018-08-21 15:51:03 +01005297 /* Check if we have enough space to buffer the message. */
5298 if( hs->buffering.total_bytes_buffered >
5299 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5300 {
5301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5302 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5303 }
5304
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005305 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5306 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005307
5308 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5309 hs->buffering.total_bytes_buffered ) )
5310 {
5311 if( recv_msg_seq_offset > 0 )
5312 {
5313 /* If we can't buffer a future message because
5314 * of space limitations -- ignore. */
5315 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",
5316 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5317 (unsigned) hs->buffering.total_bytes_buffered ) );
5318 goto exit;
5319 }
Hanno Beckere1801392018-08-21 16:51:05 +01005320 else
5321 {
5322 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",
5323 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5324 (unsigned) hs->buffering.total_bytes_buffered ) );
5325 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005326
Hanno Beckera02b0b42018-08-21 17:20:27 +01005327 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005328 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005329 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",
5330 (unsigned) msg_len,
5331 (unsigned) reassembly_buf_sz,
5332 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005333 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005334 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5335 goto exit;
5336 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005337 }
5338
5339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5340 msg_len ) );
5341
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005342 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5343 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005344 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005345 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005346 goto exit;
5347 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005348 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005349
5350 /* Prepare final header: copy msg_type, length and message_seq,
5351 * then add standardised fragment_offset and fragment_length */
5352 memcpy( hs_buf->data, ssl->in_msg, 6 );
5353 memset( hs_buf->data + 6, 0, 3 );
5354 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5355
5356 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005357
5358 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005359 }
5360 else
5361 {
5362 /* Make sure msg_type and length are consistent */
5363 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5364 {
5365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5366 /* Ignore */
5367 goto exit;
5368 }
5369 }
5370
Hanno Becker4422bbb2018-08-20 09:40:19 +01005371 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005372 {
5373 size_t frag_len, frag_off;
5374 unsigned char * const msg = hs_buf->data + 12;
5375
5376 /*
5377 * Check and copy current fragment
5378 */
5379
5380 /* Validation of header fields already done in
5381 * mbedtls_ssl_prepare_handshake_record(). */
5382 frag_off = ssl_get_hs_frag_off( ssl );
5383 frag_len = ssl_get_hs_frag_len( ssl );
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5386 frag_off, frag_len ) );
5387 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5388
5389 if( hs_buf->is_fragmented )
5390 {
5391 unsigned char * const bitmask = msg + msg_len;
5392 ssl_bitmask_set( bitmask, frag_off, frag_len );
5393 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5394 msg_len ) == 0 );
5395 }
5396 else
5397 {
5398 hs_buf->is_complete = 1;
5399 }
5400
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5402 hs_buf->is_complete ? "" : "not yet " ) );
5403 }
5404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005406 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005407
5408 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005409 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005410 break;
5411 }
5412
5413exit:
5414
5415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5416 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005417}
5418#endif /* MBEDTLS_SSL_PROTO_DTLS */
5419
Hanno Becker1097b342018-08-15 14:09:41 +01005420static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005421{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005422 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 * Consume last content-layer message and potentially
5424 * update in_msglen which keeps track of the contents'
5425 * consumption state.
5426 *
5427 * (1) Handshake messages:
5428 * Remove last handshake message, move content
5429 * and adapt in_msglen.
5430 *
5431 * (2) Alert messages:
5432 * Consume whole record content, in_msglen = 0.
5433 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005434 * (3) Change cipher spec:
5435 * Consume whole record content, in_msglen = 0.
5436 *
5437 * (4) Application data:
5438 * Don't do anything - the record layer provides
5439 * the application data as a stream transport
5440 * and consumes through mbedtls_ssl_read only.
5441 *
5442 */
5443
5444 /* Case (1): Handshake messages */
5445 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005446 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005447 /* Hard assertion to be sure that no application data
5448 * is in flight, as corrupting ssl->in_msglen during
5449 * ssl->in_offt != NULL is fatal. */
5450 if( ssl->in_offt != NULL )
5451 {
5452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5454 }
5455
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005456 /*
5457 * Get next Handshake message in the current record
5458 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005459
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005461 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005462 * current handshake content: If DTLS handshake
5463 * fragmentation is used, that's the fragment
5464 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005465 * size here is faulty and should be changed at
5466 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 * (2) While it doesn't seem to cause problems, one
5468 * has to be very careful not to assume that in_hslen
5469 * is always <= in_msglen in a sensible communication.
5470 * Again, it's wrong for DTLS handshake fragmentation.
5471 * The following check is therefore mandatory, and
5472 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005473 * Additionally, ssl->in_hslen might be arbitrarily out of
5474 * bounds after handling a DTLS message with an unexpected
5475 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005476 */
5477 if( ssl->in_hslen < ssl->in_msglen )
5478 {
5479 ssl->in_msglen -= ssl->in_hslen;
5480 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5481 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005482
Hanno Becker4a810fb2017-05-24 16:27:30 +01005483 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5484 ssl->in_msg, ssl->in_msglen );
5485 }
5486 else
5487 {
5488 ssl->in_msglen = 0;
5489 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005490
Hanno Becker4a810fb2017-05-24 16:27:30 +01005491 ssl->in_hslen = 0;
5492 }
5493 /* Case (4): Application data */
5494 else if( ssl->in_offt != NULL )
5495 {
5496 return( 0 );
5497 }
5498 /* Everything else (CCS & Alerts) */
5499 else
5500 {
5501 ssl->in_msglen = 0;
5502 }
5503
Hanno Becker1097b342018-08-15 14:09:41 +01005504 return( 0 );
5505}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005506
Hanno Beckere74d5562018-08-15 14:26:08 +01005507static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5508{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005509 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005510 return( 1 );
5511
5512 return( 0 );
5513}
5514
Hanno Becker5f066e72018-08-16 14:56:31 +01005515#if defined(MBEDTLS_SSL_PROTO_DTLS)
5516
5517static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 if( hs == NULL )
5521 return;
5522
Hanno Becker01315ea2018-08-21 17:22:17 +01005523 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005524 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005525 hs->buffering.total_bytes_buffered -=
5526 hs->buffering.future_record.len;
5527
5528 mbedtls_free( hs->buffering.future_record.data );
5529 hs->buffering.future_record.data = NULL;
5530 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005531}
5532
5533static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5534{
5535 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5536 unsigned char * rec;
5537 size_t rec_len;
5538 unsigned rec_epoch;
5539
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005540 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005541 return( 0 );
5542
5543 if( hs == NULL )
5544 return( 0 );
5545
Hanno Becker5f066e72018-08-16 14:56:31 +01005546 rec = hs->buffering.future_record.data;
5547 rec_len = hs->buffering.future_record.len;
5548 rec_epoch = hs->buffering.future_record.epoch;
5549
5550 if( rec == NULL )
5551 return( 0 );
5552
Hanno Becker4cb782d2018-08-20 11:19:05 +01005553 /* Only consider loading future records if the
5554 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005555 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005556 return( 0 );
5557
Hanno Becker5f066e72018-08-16 14:56:31 +01005558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5559
5560 if( rec_epoch != ssl->in_epoch )
5561 {
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5563 goto exit;
5564 }
5565
5566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5567
5568 /* Double-check that the record is not too large */
5569 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5570 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5571 {
5572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5574 }
5575
5576 memcpy( ssl->in_hdr, rec, rec_len );
5577 ssl->in_left = rec_len;
5578 ssl->next_record_offset = 0;
5579
5580 ssl_free_buffered_record( ssl );
5581
5582exit:
5583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5584 return( 0 );
5585}
5586
5587static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5588{
5589 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5590 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005591 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005592
5593 /* Don't buffer future records outside handshakes. */
5594 if( hs == NULL )
5595 return( 0 );
5596
5597 /* Only buffer handshake records (we are only interested
5598 * in Finished messages). */
5599 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5600 return( 0 );
5601
5602 /* Don't buffer more than one future epoch record. */
5603 if( hs->buffering.future_record.data != NULL )
5604 return( 0 );
5605
Hanno Becker01315ea2018-08-21 17:22:17 +01005606 /* Don't buffer record if there's not enough buffering space remaining. */
5607 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5608 hs->buffering.total_bytes_buffered ) )
5609 {
5610 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",
5611 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5612 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005613 return( 0 );
5614 }
5615
Hanno Becker5f066e72018-08-16 14:56:31 +01005616 /* Buffer record */
5617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5618 ssl->in_epoch + 1 ) );
5619 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5620 rec_hdr_len + ssl->in_msglen );
5621
5622 /* ssl_parse_record_header() only considers records
5623 * of the next epoch as candidates for buffering. */
5624 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005625 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005626
5627 hs->buffering.future_record.data =
5628 mbedtls_calloc( 1, hs->buffering.future_record.len );
5629 if( hs->buffering.future_record.data == NULL )
5630 {
5631 /* If we run out of RAM trying to buffer a
5632 * record from the next epoch, just ignore. */
5633 return( 0 );
5634 }
5635
Hanno Becker01315ea2018-08-21 17:22:17 +01005636 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005637
Hanno Becker01315ea2018-08-21 17:22:17 +01005638 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005639 return( 0 );
5640}
5641
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
5643
Hanno Beckere74d5562018-08-15 14:26:08 +01005644static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005645{
5646 int ret;
5647
Hanno Becker5f066e72018-08-16 14:56:31 +01005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
5649 /* We might have buffered a future record; if so,
5650 * and if the epoch matches now, load it.
5651 * On success, this call will set ssl->in_left to
5652 * the length of the buffered record, so that
5653 * the calls to ssl_fetch_input() below will
5654 * essentially be no-ops. */
5655 ret = ssl_load_buffered_record( ssl );
5656 if( ret != 0 )
5657 return( ret );
5658#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005659
Hanno Becker8b09b732019-05-08 12:03:28 +01005660 /* Reset in pointers to default state for TLS/DTLS records,
5661 * assuming no CID and no offset between record content and
5662 * record plaintext. */
5663 ssl_update_in_pointers( ssl );
5664
5665 /* Ensure that we have enough space available for the default form
5666 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5667 * with no space for CIDs counted in). */
5668 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5669 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005671 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005672 return( ret );
5673 }
5674
5675 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005678 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005679 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005681 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5682 {
5683 ret = ssl_buffer_future_record( ssl );
5684 if( ret != 0 )
5685 return( ret );
5686
5687 /* Fall through to handling of unexpected records */
5688 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5689 }
5690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005691 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5692 {
5693 /* Skip unexpected record (but not whole datagram) */
5694 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005695 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5698 "(header)" ) );
5699 }
5700 else
5701 {
5702 /* Skip invalid record and the rest of the datagram */
5703 ssl->next_record_offset = 0;
5704 ssl->in_left = 0;
5705
5706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5707 "(header)" ) );
5708 }
5709
5710 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005711 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005712 }
5713#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005715 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005716
5717 /*
5718 * Read and optionally decrypt the message contents
5719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005721 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005724 return( ret );
5725 }
5726
5727 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005729 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005730 {
Hanno Becker43395762019-05-03 14:46:38 +01005731 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005732 if( ssl->next_record_offset < ssl->in_left )
5733 {
5734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5735 }
5736 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005737 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005738#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005739#if defined(MBEDTLS_SSL_PROTO_TLS)
5740 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005741 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005742 }
5743#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005744
5745 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005747#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005748 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005749 {
5750 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005751 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005752 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005753 /* Except when waiting for Finished as a bad mac here
5754 * probably means something went wrong in the handshake
5755 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5756 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5757 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5758 {
5759#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5760 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5761 {
5762 mbedtls_ssl_send_alert_message( ssl,
5763 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5764 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5765 }
5766#endif
5767 return( ret );
5768 }
5769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005770#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005771 if( ssl->conf->badmac_limit != 0 &&
5772 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005774 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5775 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005776 }
5777#endif
5778
Hanno Becker4a810fb2017-05-24 16:27:30 +01005779 /* As above, invalid records cause
5780 * dismissal of the whole datagram. */
5781
5782 ssl->next_record_offset = 0;
5783 ssl->in_left = 0;
5784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005786 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005787 }
5788
5789 return( ret );
5790 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005791 MBEDTLS_SSL_TRANSPORT_ELSE
5792#endif /* MBEDTLS_SSL_PROTO_DTLS */
5793#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005794 {
5795 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005796#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5797 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005799 mbedtls_ssl_send_alert_message( ssl,
5800 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5801 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005802 }
5803#endif
5804 return( ret );
5805 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005806#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005807 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005808
Simon Butcher99000142016-10-13 17:21:01 +01005809 return( 0 );
5810}
5811
5812int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5813{
5814 int ret;
5815
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005816 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005817 * Handle particular types of records
5818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 {
Simon Butcher99000142016-10-13 17:21:01 +01005821 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5822 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005823 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
Hanno Beckere678eaa2018-08-21 14:57:46 +01005827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005828 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005830 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5832 ssl->in_msglen ) );
5833 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005834 }
5835
Hanno Beckere678eaa2018-08-21 14:57:46 +01005836 if( ssl->in_msg[0] != 1 )
5837 {
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5839 ssl->in_msg[0] ) );
5840 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5841 }
5842
5843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005844 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005845 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5846 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5847 {
5848 if( ssl->handshake == NULL )
5849 {
5850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5851 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5852 }
5853
5854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5855 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5856 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005857#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005858 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005862 if( ssl->in_msglen != 2 )
5863 {
5864 /* Note: Standard allows for more than one 2 byte alert
5865 to be packed in a single message, but Mbed TLS doesn't
5866 currently support this. */
5867 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5868 ssl->in_msglen ) );
5869 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5870 }
5871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005873 ssl->in_msg[0], ssl->in_msg[1] ) );
5874
5875 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005876 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005878 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005881 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883 }
5884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5886 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5889 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005891
5892#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5893 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5894 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5895 {
Hanno Becker90333da2017-10-10 11:27:13 +01005896 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005897 /* Will be handled when trying to parse ServerHello */
5898 return( 0 );
5899 }
5900#endif
5901
5902#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5903 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5904 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5905 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5906 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5907 {
5908 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5909 /* Will be handled in mbedtls_ssl_parse_certificate() */
5910 return( 0 );
5911 }
5912#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5913
5914 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005915 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
Hanno Beckerc76c6192017-06-06 10:03:17 +01005918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005919 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005920 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005921 /* Drop unexpected ApplicationData records,
5922 * except at the beginning of renegotiations */
5923 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5924 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5925#if defined(MBEDTLS_SSL_RENEGOTIATION)
5926 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5927 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005928#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005929 )
5930 {
5931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5932 return( MBEDTLS_ERR_SSL_NON_FATAL );
5933 }
5934
5935 if( ssl->handshake != NULL &&
5936 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5937 {
5938 ssl_handshake_wrapup_free_hs_transform( ssl );
5939 }
5940 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005942
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 return( 0 );
5944}
5945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005947{
5948 int ret;
5949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005950 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5951 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5952 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005953 {
5954 return( ret );
5955 }
5956
5957 return( 0 );
5958}
5959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005960int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005961 unsigned char level,
5962 unsigned char message )
5963{
5964 int ret;
5965
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005966 if( ssl == NULL || ssl->conf == NULL )
5967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005970 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005973 ssl->out_msglen = 2;
5974 ssl->out_msg[0] = level;
5975 ssl->out_msg[1] = message;
5976
Hanno Becker67bc7c32018-08-06 11:33:50 +01005977 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005980 return( ret );
5981 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005983
5984 return( 0 );
5985}
5986
Hanno Becker17572472019-02-08 07:19:04 +00005987#if defined(MBEDTLS_X509_CRT_PARSE_C)
5988static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
5989{
5990#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
5991 if( session->peer_cert != NULL )
5992 {
5993 mbedtls_x509_crt_free( session->peer_cert );
5994 mbedtls_free( session->peer_cert );
5995 session->peer_cert = NULL;
5996 }
5997#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
5998 if( session->peer_cert_digest != NULL )
5999 {
6000 /* Zeroization is not necessary. */
6001 mbedtls_free( session->peer_cert_digest );
6002 session->peer_cert_digest = NULL;
6003 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6004 session->peer_cert_digest_len = 0;
6005 }
6006#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6007}
6008#endif /* MBEDTLS_X509_CRT_PARSE_C */
6009
Paul Bakker5121ce52009-01-03 21:22:43 +00006010/*
6011 * Handshake functions
6012 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006013#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006014/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006015int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006016{
Hanno Becker8759e162017-12-27 21:34:08 +00006017 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006020
Hanno Becker5097cba2019-02-05 13:36:46 +00006021 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006024 ssl->state++;
6025 return( 0 );
6026 }
6027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006030}
6031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006032int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006033{
Hanno Becker8759e162017-12-27 21:34:08 +00006034 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006037
Hanno Becker5097cba2019-02-05 13:36:46 +00006038 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006041 ssl->state++;
6042 return( 0 );
6043 }
6044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006047}
Gilles Peskinef9828522017-05-03 12:28:43 +02006048
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006049#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006050/* Some certificate support -> implement write and parse */
6051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006054 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006055 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006057 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006060
Hanno Becker5097cba2019-02-05 13:36:46 +00006061 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006064 ssl->state++;
6065 return( 0 );
6066 }
6067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006069 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 {
6071 if( ssl->client_auth == 0 )
6072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006073 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 ssl->state++;
6075 return( 0 );
6076 }
6077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 /*
6080 * If using SSLv3 and got no cert, send an Alert message
6081 * (otherwise an empty Certificate message will be sent).
6082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6084 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 {
6086 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006087 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6088 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6089 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 goto write_msg;
6093 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006096#endif /* MBEDTLS_SSL_CLI_C */
6097#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006098 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6103 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006104 }
6105 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006106#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006108 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006109
6110 /*
6111 * 0 . 0 handshake type
6112 * 1 . 3 handshake length
6113 * 4 . 6 length of all certs
6114 * 7 . 9 length of cert. 1
6115 * 10 . n-1 peer certificate
6116 * n . n+2 length of cert. 2
6117 * n+3 . ... upper level cert, etc.
6118 */
6119 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006120 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
Paul Bakker29087132010-03-21 21:03:34 +00006122 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 {
6124 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006125 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006128 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 }
6131
6132 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6133 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6134 ssl->out_msg[i + 2] = (unsigned char)( n );
6135
6136 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6137 i += n; crt = crt->next;
6138 }
6139
6140 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6141 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6142 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6143
6144 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006145 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6146 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006147
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006148#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006149write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
6152 ssl->state++;
6153
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006154 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006157 return( ret );
6158 }
6159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006161
Paul Bakkered27a042013-04-18 22:46:23 +02006162 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006163}
6164
Hanno Becker285ff0c2019-01-31 07:44:03 +00006165#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerdf759382019-02-05 17:02:46 +00006166
6167#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006168static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6169 unsigned char *crt_buf,
6170 size_t crt_buf_len )
6171{
6172 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6173
6174 if( peer_crt == NULL )
6175 return( -1 );
6176
6177 if( peer_crt->raw.len != crt_buf_len )
6178 return( -1 );
6179
Hanno Becker68b856d2019-02-08 14:00:04 +00006180 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006181}
Hanno Beckerdf759382019-02-05 17:02:46 +00006182#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6183static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6184 unsigned char *crt_buf,
6185 size_t crt_buf_len )
6186{
6187 int ret;
6188 unsigned char const * const peer_cert_digest =
6189 ssl->session->peer_cert_digest;
6190 mbedtls_md_type_t const peer_cert_digest_type =
6191 ssl->session->peer_cert_digest_type;
6192 mbedtls_md_info_t const * const digest_info =
6193 mbedtls_md_info_from_type( peer_cert_digest_type );
6194 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6195 size_t digest_len;
6196
6197 if( peer_cert_digest == NULL || digest_info == NULL )
6198 return( -1 );
6199
6200 digest_len = mbedtls_md_get_size( digest_info );
6201 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6202 return( -1 );
6203
6204 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6205 if( ret != 0 )
6206 return( -1 );
6207
6208 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6209}
6210#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker285ff0c2019-01-31 07:44:03 +00006211#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006212
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006213/*
6214 * Once the certificate message is read, parse it into a cert chain and
6215 * perform basic checks, but leave actual verification to the caller
6216 */
Hanno Becker35e41772019-02-05 15:37:23 +00006217static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6218 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006219{
Hanno Becker35e41772019-02-05 15:37:23 +00006220 int ret;
6221#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6222 int crt_cnt=0;
6223#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006224 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006225 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006230 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6231 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006233 }
6234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6236 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006239 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6240 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 }
6243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006245
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006249 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006250
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006251 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006255 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6256 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258 }
6259
Hanno Becker33c3dc82019-01-30 14:46:46 +00006260 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6261 i += 3;
6262
Hanno Becker33c3dc82019-01-30 14:46:46 +00006263 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 while( i < ssl->in_hslen )
6265 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006266 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006267 if ( i + 3 > ssl->in_hslen ) {
6268 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006269 mbedtls_ssl_send_alert_message( ssl,
6270 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6271 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006272 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6273 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006274 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6275 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006276 if( ssl->in_msg[i] != 0 )
6277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006279 mbedtls_ssl_send_alert_message( ssl,
6280 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6281 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283 }
6284
Hanno Becker33c3dc82019-01-30 14:46:46 +00006285 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006286 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6287 | (unsigned int) ssl->in_msg[i + 2];
6288 i += 3;
6289
6290 if( n < 128 || i + n > ssl->in_hslen )
6291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006293 mbedtls_ssl_send_alert_message( ssl,
6294 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6295 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297 }
6298
Hanno Becker33c3dc82019-01-30 14:46:46 +00006299 /* Check if we're handling the first CRT in the chain. */
Hanno Becker35e41772019-02-05 15:37:23 +00006300#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6301 if( crt_cnt++ == 0 &&
6302 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6303 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006304 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006305 /* During client-side renegotiation, check that the server's
6306 * end-CRTs hasn't changed compared to the initial handshake,
6307 * mitigating the triple handshake attack. On success, reuse
6308 * the original end-CRT instead of parsing it again. */
Hanno Becker35e41772019-02-05 15:37:23 +00006309 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6310 if( ssl_check_peer_crt_unchanged( ssl,
6311 &ssl->in_msg[i],
6312 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006313 {
Hanno Becker35e41772019-02-05 15:37:23 +00006314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6315 mbedtls_ssl_send_alert_message( ssl,
6316 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6317 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6318 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006319 }
Hanno Becker35e41772019-02-05 15:37:23 +00006320
6321 /* Now we can safely free the original chain. */
6322 ssl_clear_peer_cert( ssl->session );
6323 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006324#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6325
Hanno Becker33c3dc82019-01-30 14:46:46 +00006326 /* Parse the next certificate in the chain. */
Hanno Becker0cc7af52019-02-08 14:39:16 +00006327#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker35e41772019-02-05 15:37:23 +00006328 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0cc7af52019-02-08 14:39:16 +00006329#else
Hanno Becker42de8f82019-02-26 11:51:34 +00006330 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0cc7af52019-02-08 14:39:16 +00006331 * it in-place from the input buffer instead of making a copy. */
6332 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6333#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006334 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006336 case 0: /*ok*/
6337 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6338 /* Ignore certificate with an unknown algorithm: maybe a
6339 prior certificate was already trusted. */
6340 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006341
Hanno Becker33c3dc82019-01-30 14:46:46 +00006342 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6343 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6344 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006345
Hanno Becker33c3dc82019-01-30 14:46:46 +00006346 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6347 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6348 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006349
Hanno Becker33c3dc82019-01-30 14:46:46 +00006350 default:
6351 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6352 crt_parse_der_failed:
6353 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6354 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6355 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
6357
6358 i += n;
6359 }
6360
Hanno Becker35e41772019-02-05 15:37:23 +00006361 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006362 return( 0 );
6363}
6364
Hanno Beckerb8a08572019-02-05 12:49:06 +00006365#if defined(MBEDTLS_SSL_SRV_C)
6366static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6367{
6368 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6369 return( -1 );
6370
6371#if defined(MBEDTLS_SSL_PROTO_SSL3)
6372 /*
6373 * Check if the client sent an empty certificate
6374 */
6375 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6376 {
6377 if( ssl->in_msglen == 2 &&
6378 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6379 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6380 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6381 {
6382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6383 return( 0 );
6384 }
6385
6386 return( -1 );
6387 }
6388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6389
6390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6391 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6392 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6393 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6394 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6395 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6396 {
6397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6398 return( 0 );
6399 }
6400
6401 return( -1 );
6402#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6403 MBEDTLS_SSL_PROTO_TLS1_2 */
6404}
6405#endif /* MBEDTLS_SSL_SRV_C */
6406
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006407/* Check if a certificate message is expected.
6408 * Return either
6409 * - SSL_CERTIFICATE_EXPECTED, or
6410 * - SSL_CERTIFICATE_SKIP
6411 * indicating whether a Certificate message is expected or not.
6412 */
6413#define SSL_CERTIFICATE_EXPECTED 0
6414#define SSL_CERTIFICATE_SKIP 1
6415static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6416 int authmode )
6417{
6418 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6419 ssl->handshake->ciphersuite_info;
6420
6421 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6422 return( SSL_CERTIFICATE_SKIP );
6423
6424#if defined(MBEDTLS_SSL_SRV_C)
6425 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6426 {
6427 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6428 return( SSL_CERTIFICATE_SKIP );
6429
6430 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6431 {
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006432 ssl->session_negotiate->verify_result =
6433 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6434 return( SSL_CERTIFICATE_SKIP );
6435 }
6436 }
Hanno Beckerfd5dc8a2019-03-01 08:10:46 +00006437#else
6438 ((void) authmode);
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006439#endif /* MBEDTLS_SSL_SRV_C */
6440
6441 return( SSL_CERTIFICATE_EXPECTED );
6442}
6443
Hanno Becker3cf50612019-02-05 14:36:34 +00006444static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6445 int authmode,
6446 mbedtls_x509_crt *chain,
6447 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006448{
Hanno Becker613d4902019-02-05 13:11:17 +00006449 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006450 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6451 ssl->handshake->ciphersuite_info;
Hanno Becker3cf50612019-02-05 14:36:34 +00006452 mbedtls_x509_crt *ca_chain;
6453 mbedtls_x509_crl *ca_crl;
6454
6455 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6456 return( 0 );
6457
6458#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6459 if( ssl->handshake->sni_ca_chain != NULL )
6460 {
6461 ca_chain = ssl->handshake->sni_ca_chain;
6462 ca_crl = ssl->handshake->sni_ca_crl;
6463 }
6464 else
6465#endif
6466 {
6467 ca_chain = ssl->conf->ca_chain;
6468 ca_crl = ssl->conf->ca_crl;
6469 }
6470
6471 /*
6472 * Main check: verify certificate
6473 */
6474 ret = mbedtls_x509_crt_verify_restartable(
6475 chain,
6476 ca_chain, ca_crl,
6477 ssl->conf->cert_profile,
6478 ssl->hostname,
6479 &ssl->session_negotiate->verify_result,
6480 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
6481
6482 if( ret != 0 )
6483 {
6484 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6485 }
6486
6487#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6488 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6489 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6490#endif
6491
6492 /*
6493 * Secondary checks: always done, but change 'ret' only if it was 0
6494 */
6495
6496#if defined(MBEDTLS_ECP_C)
6497 {
6498 const mbedtls_pk_context *pk = &chain->pk;
6499
6500 /* If certificate uses an EC key, make sure the curve is OK */
6501 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6502 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6503 {
6504 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6505
6506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6507 if( ret == 0 )
6508 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6509 }
6510 }
6511#endif /* MBEDTLS_ECP_C */
6512
6513 if( mbedtls_ssl_check_cert_usage( chain,
6514 ciphersuite_info,
6515 ! ssl->conf->endpoint,
6516 &ssl->session_negotiate->verify_result ) != 0 )
6517 {
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6519 if( ret == 0 )
6520 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6521 }
6522
6523 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6524 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6525 * with details encoded in the verification flags. All other kinds
6526 * of error codes, including those from the user provided f_vrfy
6527 * functions, are treated as fatal and lead to a failure of
6528 * ssl_parse_certificate even if verification was optional. */
6529 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6530 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6531 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6532 {
6533 ret = 0;
6534 }
6535
6536 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6537 {
6538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6539 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6540 }
6541
6542 if( ret != 0 )
6543 {
6544 uint8_t alert;
6545
6546 /* The certificate may have been rejected for several reasons.
6547 Pick one and send the corresponding alert. Which alert to send
6548 may be a subject of debate in some cases. */
6549 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6550 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6551 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6552 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6553 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6554 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6555 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6556 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6557 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6558 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6559 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6560 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6561 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6562 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6563 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6564 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6565 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6566 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6567 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6568 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6569 else
6570 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6571 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6572 alert );
6573 }
6574
6575#if defined(MBEDTLS_DEBUG_C)
6576 if( ssl->session_negotiate->verify_result != 0 )
6577 {
6578 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6579 ssl->session_negotiate->verify_result ) );
6580 }
6581 else
6582 {
6583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6584 }
6585#endif /* MBEDTLS_DEBUG_C */
6586
6587 return( ret );
6588}
6589
Hanno Becker34106f62019-02-08 14:59:05 +00006590#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6591static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6592 unsigned char *start, size_t len )
6593{
6594 int ret;
6595 /* Remember digest of the peer's end-CRT. */
6596 ssl->session_negotiate->peer_cert_digest =
6597 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6598 if( ssl->session_negotiate->peer_cert_digest == NULL )
6599 {
6600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6601 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6602 mbedtls_ssl_send_alert_message( ssl,
6603 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6604 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6605
6606 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6607 }
6608
6609 ret = mbedtls_md( mbedtls_md_info_from_type(
6610 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6611 start, len,
6612 ssl->session_negotiate->peer_cert_digest );
6613
6614 ssl->session_negotiate->peer_cert_digest_type =
6615 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6616 ssl->session_negotiate->peer_cert_digest_len =
6617 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6618
6619 return( ret );
6620}
6621
6622static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6623 unsigned char *start, size_t len )
6624{
6625 unsigned char *end = start + len;
6626 int ret;
6627
6628 /* Make a copy of the peer's raw public key. */
6629 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6630 ret = mbedtls_pk_parse_subpubkey( &start, end,
6631 &ssl->handshake->peer_pubkey );
6632 if( ret != 0 )
6633 {
6634 /* We should have parsed the public key before. */
6635 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6636 }
6637
6638 return( 0 );
6639}
6640#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6641
Hanno Becker3cf50612019-02-05 14:36:34 +00006642int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6643{
6644 int ret = 0;
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006645 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006646#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6647 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6648 ? ssl->handshake->sni_authmode
6649 : ssl->conf->authmode;
6650#else
6651 const int authmode = ssl->conf->authmode;
6652#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006653 void *rs_ctx = NULL;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006654 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006655
6656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6657
Hanno Becker6b9a6f32019-02-07 10:11:07 +00006658 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6659 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006660 {
6661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006662 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006663 }
6664
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006665#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6666 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006667 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006668 {
Hanno Beckere4aeb762019-02-05 17:19:52 +00006669 chain = ssl->handshake->ecrs_peer_cert;
6670 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006671 goto crt_verify;
6672 }
6673#endif
6674
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006675 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006676 {
6677 /* mbedtls_ssl_read_record may have sent an alert already. We
6678 let it decide whether to alert. */
6679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006680 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006681 }
6682
Hanno Beckerb8a08572019-02-05 12:49:06 +00006683#if defined(MBEDTLS_SSL_SRV_C)
6684 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6685 {
6686 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006687
6688 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006689 ret = 0;
6690 else
6691 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006692
Hanno Becker613d4902019-02-05 13:11:17 +00006693 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006694 }
6695#endif /* MBEDTLS_SSL_SRV_C */
6696
Hanno Becker35e41772019-02-05 15:37:23 +00006697 /* Clear existing peer CRT structure in case we tried to
6698 * reuse a session but it failed, and allocate a new one. */
Hanno Beckera46c2872019-02-05 13:08:01 +00006699 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Beckere4aeb762019-02-05 17:19:52 +00006700
6701 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6702 if( chain == NULL )
Hanno Becker35e41772019-02-05 15:37:23 +00006703 {
6704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6705 sizeof( mbedtls_x509_crt ) ) );
6706 mbedtls_ssl_send_alert_message( ssl,
6707 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6708 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Beckera46c2872019-02-05 13:08:01 +00006709
Hanno Beckere4aeb762019-02-05 17:19:52 +00006710 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6711 goto exit;
6712 }
6713 mbedtls_x509_crt_init( chain );
6714
6715 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Becker35e41772019-02-05 15:37:23 +00006716 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006717 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006718
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006719#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6720 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006721 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006722
6723crt_verify:
6724 if( ssl->handshake->ecrs_enabled)
6725 rs_ctx = &ssl->handshake->ecrs_ctx;
6726#endif
6727
Hanno Becker3cf50612019-02-05 14:36:34 +00006728 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Beckere4aeb762019-02-05 17:19:52 +00006729 chain, rs_ctx );
Hanno Becker3cf50612019-02-05 14:36:34 +00006730 if( ret != 0 )
Hanno Beckere4aeb762019-02-05 17:19:52 +00006731 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006732
Hanno Becker3008d282019-02-05 17:02:28 +00006733#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker3008d282019-02-05 17:02:28 +00006734 {
Hanno Becker34106f62019-02-08 14:59:05 +00006735 unsigned char *crt_start, *pk_start;
6736 size_t crt_len, pk_len;
Hanno Beckere4aeb762019-02-05 17:19:52 +00006737
Hanno Becker34106f62019-02-08 14:59:05 +00006738 /* We parse the CRT chain without copying, so
6739 * these pointers point into the input buffer,
6740 * and are hence still valid after freeing the
6741 * CRT chain. */
Hanno Becker3008d282019-02-05 17:02:28 +00006742
Hanno Becker34106f62019-02-08 14:59:05 +00006743 crt_start = chain->raw.p;
6744 crt_len = chain->raw.len;
Hanno Becker3008d282019-02-05 17:02:28 +00006745
Hanno Becker34106f62019-02-08 14:59:05 +00006746 pk_start = chain->pk_raw.p;
6747 pk_len = chain->pk_raw.len;
6748
6749 /* Free the CRT structures before computing
6750 * digest and copying the peer's public key. */
6751 mbedtls_x509_crt_free( chain );
6752 mbedtls_free( chain );
6753 chain = NULL;
6754
6755 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckercf291d62019-02-06 16:19:04 +00006756 if( ret != 0 )
Hanno Beckercf291d62019-02-06 16:19:04 +00006757 goto exit;
Hanno Becker34106f62019-02-08 14:59:05 +00006758
6759 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6760 if( ret != 0 )
6761 goto exit;
Hanno Beckercf291d62019-02-06 16:19:04 +00006762 }
Hanno Becker34106f62019-02-08 14:59:05 +00006763#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6764 /* Pass ownership to session structure. */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006765 ssl->session_negotiate->peer_cert = chain;
6766 chain = NULL;
Hanno Becker34106f62019-02-08 14:59:05 +00006767#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Beckere4aeb762019-02-05 17:19:52 +00006768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006770
Hanno Becker613d4902019-02-05 13:11:17 +00006771exit:
6772
Hanno Beckere4aeb762019-02-05 17:19:52 +00006773 if( ret == 0 )
6774 ssl->state++;
6775
6776#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6777 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6778 {
6779 ssl->handshake->ecrs_peer_cert = chain;
6780 chain = NULL;
6781 }
6782#endif
6783
6784 if( chain != NULL )
6785 {
6786 mbedtls_x509_crt_free( chain );
6787 mbedtls_free( chain );
6788 }
6789
Paul Bakker5121ce52009-01-03 21:22:43 +00006790 return( ret );
6791}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006792#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006795{
6796 int ret;
6797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006800 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006801 ssl->out_msglen = 1;
6802 ssl->out_msg[0] = 1;
6803
Paul Bakker5121ce52009-01-03 21:22:43 +00006804 ssl->state++;
6805
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006806 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006809 return( ret );
6810 }
6811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006813
6814 return( 0 );
6815}
6816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006818{
6819 int ret;
6820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006822
Hanno Becker327c93b2018-08-15 13:56:18 +01006823 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006826 return( ret );
6827 }
6828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6833 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006834 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006835 }
6836
Hanno Beckere678eaa2018-08-21 14:57:46 +01006837 /* CCS records are only accepted if they have length 1 and content '1',
6838 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006839
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006840 /*
6841 * Switch to our negotiated transform and session parameters for inbound
6842 * data.
6843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006845 ssl->transform_in = ssl->transform_negotiate;
6846 ssl->session_in = ssl->session_negotiate;
6847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006849 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006852 ssl_dtls_replay_reset( ssl );
6853#endif
6854
6855 /* Increment epoch */
6856 if( ++ssl->in_epoch == 0 )
6857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006859 /* This is highly unlikely to happen for legitimate reasons, so
6860 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006862 }
6863 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006864 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006866#if defined(MBEDTLS_SSL_PROTO_TLS)
6867 {
6868 memset( ssl->in_ctr, 0, 8 );
6869 }
6870#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006871
Hanno Beckerf5970a02019-05-08 09:38:41 +01006872 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006874#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6875 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006880 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6881 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006883 }
6884 }
6885#endif
6886
Paul Bakker5121ce52009-01-03 21:22:43 +00006887 ssl->state++;
6888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006890
6891 return( 0 );
6892}
6893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006894void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6895 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006896{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006897 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006899#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6900 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6901 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006902 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006903 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006904#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006905#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6906#if defined(MBEDTLS_SHA512_C)
6907 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006908 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6909 else
6910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006911#if defined(MBEDTLS_SHA256_C)
6912 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006913 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006914 else
6915#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006916#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006919 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006920 }
Paul Bakker380da532012-04-18 16:10:25 +00006921}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006923void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006924{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006925#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6926 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006927 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6928 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006930#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6931#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006932 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006935 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006936#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006937#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006938}
6939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006940static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006941 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006943#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6944 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006945 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6946 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006947#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6949#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006950 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006951#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006952#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006953 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006954#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006955#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006956}
6957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006958#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6959 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6960static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006961 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006962{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006963 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6964 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006965}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006966#endif
Paul Bakker380da532012-04-18 16:10:25 +00006967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006968#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6969#if defined(MBEDTLS_SHA256_C)
6970static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006972{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006973 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006974}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006975#endif
Paul Bakker380da532012-04-18 16:10:25 +00006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977#if defined(MBEDTLS_SHA512_C)
6978static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006979 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006980{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006981 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006982}
Paul Bakker769075d2012-11-24 11:26:46 +01006983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006987static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006989{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006990 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006991 mbedtls_md5_context md5;
6992 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006993
Paul Bakker5121ce52009-01-03 21:22:43 +00006994 unsigned char padbuf[48];
6995 unsigned char md5sum[16];
6996 unsigned char sha1sum[20];
6997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006999 if( !session )
7000 session = ssl->session;
7001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007003
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007004 mbedtls_md5_init( &md5 );
7005 mbedtls_sha1_init( &sha1 );
7006
7007 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7008 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007009
7010 /*
7011 * SSLv3:
7012 * hash =
7013 * MD5( master + pad2 +
7014 * MD5( handshake + sender + master + pad1 ) )
7015 * + SHA1( master + pad2 +
7016 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007017 */
7018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007019#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007020 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7021 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007022#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007024#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007025 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7026 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007029 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007030 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007031
Paul Bakker1ef83d62012-04-11 12:09:53 +00007032 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007033
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007034 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7035 mbedtls_md5_update_ret( &md5, session->master, 48 );
7036 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7037 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007038
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007039 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7040 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7041 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7042 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007043
Paul Bakker1ef83d62012-04-11 12:09:53 +00007044 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007045
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007046 mbedtls_md5_starts_ret( &md5 );
7047 mbedtls_md5_update_ret( &md5, session->master, 48 );
7048 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7049 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7050 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007052 mbedtls_sha1_starts_ret( &sha1 );
7053 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7054 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7055 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7056 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007058 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007060 mbedtls_md5_free( &md5 );
7061 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007062
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007063 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7064 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7065 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007072static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007073 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007074{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007075 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007076 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007077 mbedtls_md5_context md5;
7078 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007079 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007082 if( !session )
7083 session = ssl->session;
7084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007087 mbedtls_md5_init( &md5 );
7088 mbedtls_sha1_init( &sha1 );
7089
7090 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7091 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Paul Bakker1ef83d62012-04-11 12:09:53 +00007093 /*
7094 * TLSv1:
7095 * hash = PRF( master, finished_label,
7096 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7097 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007099#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007100 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7101 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007102#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007104#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007105 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7106 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007107#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007110 ? "client finished"
7111 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007112
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007113 mbedtls_md5_finish_ret( &md5, padbuf );
7114 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007115
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007116 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007117 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007120
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007121 mbedtls_md5_free( &md5 );
7122 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007123
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007124 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007127}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7131#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007132static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007134{
7135 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007136 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007137 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007138 unsigned char padbuf[32];
7139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007141 if( !session )
7142 session = ssl->session;
7143
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007144 mbedtls_sha256_init( &sha256 );
7145
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007147
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007148 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007149
7150 /*
7151 * TLSv1.2:
7152 * hash = PRF( master, finished_label,
7153 * Hash( handshake ) )[0.11]
7154 */
7155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156#if !defined(MBEDTLS_SHA256_ALT)
7157 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007158 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007159#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007162 ? "client finished"
7163 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007164
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007165 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007166
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007167 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007168 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007171
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007172 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007174 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007181static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007183{
7184 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007185 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007186 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007187 unsigned char padbuf[48];
7188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007190 if( !session )
7191 session = ssl->session;
7192
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007193 mbedtls_sha512_init( &sha512 );
7194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007196
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007197 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007198
7199 /*
7200 * TLSv1.2:
7201 * hash = PRF( master, finished_label,
7202 * Hash( handshake ) )[0.11]
7203 */
7204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007206 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7207 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007208#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007211 ? "client finished"
7212 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007213
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007214 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007215
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007216 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007217 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007220
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007221 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007222
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007223 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227#endif /* MBEDTLS_SHA512_C */
7228#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007233
7234 /*
7235 * Free our handshake params
7236 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007237 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007239 ssl->handshake = NULL;
7240
7241 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007242 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007243 */
7244 if( ssl->transform )
7245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246 mbedtls_ssl_transform_free( ssl->transform );
7247 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007248 }
7249 ssl->transform = ssl->transform_negotiate;
7250 ssl->transform_negotiate = NULL;
7251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007253}
7254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007256{
7257 int resume = ssl->handshake->resume;
7258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_RENEGOTIATION)
7262 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007265 ssl->renego_records_seen = 0;
7266 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007267#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007268
7269 /*
7270 * Free the previous session and switch in the current one
7271 */
Paul Bakker0a597072012-09-25 21:55:46 +00007272 if( ssl->session )
7273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007275 /* RFC 7366 3.1: keep the EtM state */
7276 ssl->session_negotiate->encrypt_then_mac =
7277 ssl->session->encrypt_then_mac;
7278#endif
7279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 mbedtls_ssl_session_free( ssl->session );
7281 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007282 }
7283 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007284 ssl->session_negotiate = NULL;
7285
Paul Bakker0a597072012-09-25 21:55:46 +00007286 /*
7287 * Add cache entry
7288 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007289 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007290 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007291 resume == 0 )
7292 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007293 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007295 }
Paul Bakker0a597072012-09-25 21:55:46 +00007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007298 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007299 ssl->handshake->flight != NULL )
7300 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007301 /* Cancel handshake timer */
7302 ssl_set_timer( ssl, 0 );
7303
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007304 /* Keep last flight around in case we need to resend it:
7305 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007307 }
7308 else
7309#endif
7310 ssl_handshake_wrapup_free_hs_transform( ssl );
7311
Paul Bakker48916f92012-09-16 19:57:18 +00007312 ssl->state++;
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007315}
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007319 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007322
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007323 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007324
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007325 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007326
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007327 /*
7328 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7329 * may define some other value. Currently (early 2016), no defined
7330 * ciphersuite does this (and this is unlikely to change as activity has
7331 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007333 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007336 ssl->verify_data_len = hash_len;
7337 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007338#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007339
Paul Bakker5121ce52009-01-03 21:22:43 +00007340 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7342 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
7344 /*
7345 * In case of session resuming, invert the client and server
7346 * ChangeCipherSpec messages order.
7347 */
Paul Bakker0a597072012-09-25 21:55:46 +00007348 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007350#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007351 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007353#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007354#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007355 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007357#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007358 }
7359 else
7360 ssl->state++;
7361
Paul Bakker48916f92012-09-16 19:57:18 +00007362 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007363 * Switch to our negotiated transform and session parameters for outbound
7364 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007368#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007369 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007370 {
7371 unsigned char i;
7372
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007373 /* Remember current epoch settings for resending */
7374 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007375 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007376
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007377 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007378 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007379
7380 /* Increment epoch */
7381 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007382 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007383 break;
7384
7385 /* The loop goes to its end iff the counter is wrapping */
7386 if( i == 0 )
7387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7389 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007390 }
7391 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007392 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007394#if defined(MBEDTLS_SSL_PROTO_TLS)
7395 {
7396 memset( ssl->cur_out_ctr, 0, 8 );
7397 }
7398#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007400 ssl->transform_out = ssl->transform_negotiate;
7401 ssl->session_out = ssl->session_negotiate;
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7404 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7409 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007410 }
7411 }
7412#endif
7413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007415 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007417#endif
7418
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007419 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007422 return( ret );
7423 }
7424
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007425#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007426 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007427 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7428 {
7429 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7430 return( ret );
7431 }
7432#endif
7433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007435
7436 return( 0 );
7437}
7438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007440#define SSL_MAX_HASH_LEN 36
7441#else
7442#define SSL_MAX_HASH_LEN 12
7443#endif
7444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007446{
Paul Bakker23986e52011-04-24 08:57:21 +00007447 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007448 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007449 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007452
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007453 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007454
Hanno Becker327c93b2018-08-15 13:56:18 +01007455 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007458 return( ret );
7459 }
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7465 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007467 }
7468
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007469 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470#if defined(MBEDTLS_SSL_PROTO_SSL3)
7471 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007472 hash_len = 36;
7473 else
7474#endif
7475 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7478 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007481 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7482 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007484 }
7485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007487 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007490 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7491 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007492 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 }
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007496 ssl->verify_data_len = hash_len;
7497 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007498#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007499
Paul Bakker0a597072012-09-25 21:55:46 +00007500 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007503 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007505#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007507 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007510 }
7511 else
7512 ssl->state++;
7513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007515 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007517#endif
7518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007520
7521 return( 0 );
7522}
7523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7529 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7530 mbedtls_md5_init( &handshake->fin_md5 );
7531 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007532 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7533 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7536#if defined(MBEDTLS_SHA256_C)
7537 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007538 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540#if defined(MBEDTLS_SHA512_C)
7541 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007542 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007545
7546 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007547
7548#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7549 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7550 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7551#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#if defined(MBEDTLS_DHM_C)
7554 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556#if defined(MBEDTLS_ECDH_C)
7557 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007558#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007559#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007560 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007561#if defined(MBEDTLS_SSL_CLI_C)
7562 handshake->ecjpake_cache = NULL;
7563 handshake->ecjpake_cache_len = 0;
7564#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007565#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007566
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007567#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007568 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007569#endif
7570
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007571#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7572 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7573#endif
Hanno Becker3bf8cdf2019-02-06 16:18:31 +00007574
7575#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7576 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7577 mbedtls_pk_init( &handshake->peer_pubkey );
7578#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007579}
7580
Hanno Becker611a83b2018-01-03 14:27:32 +00007581void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7586 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007587
Hanno Becker92231322018-01-03 15:32:51 +00007588#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 mbedtls_md_init( &transform->md_ctx_enc );
7590 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007591#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007592}
7593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007597}
7598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007600{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007601 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007602 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007604 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007606 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007607 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007608
7609 /*
7610 * Either the pointers are now NULL or cleared properly and can be freed.
7611 * Now allocate missing structures.
7612 */
7613 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007614 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007615 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007616 }
Paul Bakker48916f92012-09-16 19:57:18 +00007617
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007618 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007619 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007620 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007621 }
Paul Bakker48916f92012-09-16 19:57:18 +00007622
Paul Bakker82788fb2014-10-20 13:59:19 +02007623 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007624 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007625 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007626 }
Paul Bakker48916f92012-09-16 19:57:18 +00007627
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007628 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007629 if( ssl->handshake == NULL ||
7630 ssl->transform_negotiate == NULL ||
7631 ssl->session_negotiate == NULL )
7632 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 mbedtls_free( ssl->handshake );
7636 mbedtls_free( ssl->transform_negotiate );
7637 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007638
7639 ssl->handshake = NULL;
7640 ssl->transform_negotiate = NULL;
7641 ssl->session_negotiate = NULL;
7642
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007643 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007644 }
7645
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007646 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007648 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007649 ssl_handshake_params_init( ssl->handshake );
7650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007652 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007653 {
7654 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007655
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7657 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7658 else
7659 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007660
7661 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007662 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007663#endif
7664
Paul Bakker48916f92012-09-16 19:57:18 +00007665 return( 0 );
7666}
7667
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007668#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007669/* Dummy cookie callbacks for defaults */
7670static int ssl_cookie_write_dummy( void *ctx,
7671 unsigned char **p, unsigned char *end,
7672 const unsigned char *cli_id, size_t cli_id_len )
7673{
7674 ((void) ctx);
7675 ((void) p);
7676 ((void) end);
7677 ((void) cli_id);
7678 ((void) cli_id_len);
7679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007680 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007681}
7682
7683static int ssl_cookie_check_dummy( void *ctx,
7684 const unsigned char *cookie, size_t cookie_len,
7685 const unsigned char *cli_id, size_t cli_id_len )
7686{
7687 ((void) ctx);
7688 ((void) cookie);
7689 ((void) cookie_len);
7690 ((void) cli_id);
7691 ((void) cli_id_len);
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007694}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007695#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007696
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007697/* Once ssl->out_hdr as the address of the beginning of the
7698 * next outgoing record is set, deduce the other pointers.
7699 *
7700 * Note: For TLS, we save the implicit record sequence number
7701 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7702 * and the caller has to make sure there's space for this.
7703 */
7704
7705static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7706 mbedtls_ssl_transform *transform )
7707{
7708#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007709 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007710 {
7711 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007712#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007713 ssl->out_cid = ssl->out_ctr + 8;
7714 ssl->out_len = ssl->out_cid;
7715 if( transform != NULL )
7716 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007717#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007718 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007719#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007720 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007721 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007722 MBEDTLS_SSL_TRANSPORT_ELSE
7723#endif /* MBEDTLS_SSL_PROTO_DTLS */
7724#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007725 {
7726 ssl->out_ctr = ssl->out_hdr - 8;
7727 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007728#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007729 ssl->out_cid = ssl->out_len;
7730#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007731 ssl->out_iv = ssl->out_hdr + 5;
7732 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007733#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007734
7735 /* Adjust out_msg to make space for explicit IV, if used. */
7736 if( transform != NULL &&
7737 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7738 {
7739 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7740 }
7741 else
7742 ssl->out_msg = ssl->out_iv;
7743}
7744
7745/* Once ssl->in_hdr as the address of the beginning of the
7746 * next incoming record is set, deduce the other pointers.
7747 *
7748 * Note: For TLS, we save the implicit record sequence number
7749 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7750 * and the caller has to make sure there's space for this.
7751 */
7752
Hanno Beckerf5970a02019-05-08 09:38:41 +01007753static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007754{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007755 /* This function sets the pointers to match the case
7756 * of unprotected TLS/DTLS records, with both ssl->in_iv
7757 * and ssl->in_msg pointing to the beginning of the record
7758 * content.
7759 *
7760 * When decrypting a protected record, ssl->in_msg
7761 * will be shifted to point to the beginning of the
7762 * record plaintext.
7763 */
7764
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007765#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007766 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007767 {
Hanno Becker70e79282019-05-03 14:34:53 +01007768 /* This sets the header pointers to match records
7769 * without CID. When we receive a record containing
7770 * a CID, the fields are shifted accordingly in
7771 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007772 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007773#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007774 ssl->in_cid = ssl->in_ctr + 8;
7775 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007776#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007777 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007778#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007779 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007780 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007781 MBEDTLS_SSL_TRANSPORT_ELSE
7782#endif /* MBEDTLS_SSL_PROTO_DTLS */
7783#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007784 {
7785 ssl->in_ctr = ssl->in_hdr - 8;
7786 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007787#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007788 ssl->in_cid = ssl->in_len;
7789#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007790 ssl->in_iv = ssl->in_hdr + 5;
7791 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007792#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007793
Hanno Beckerf5970a02019-05-08 09:38:41 +01007794 /* This will be adjusted at record decryption time. */
7795 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007796}
7797
Paul Bakker5121ce52009-01-03 21:22:43 +00007798/*
7799 * Initialize an SSL context
7800 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007801void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7802{
7803 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7804}
7805
7806/*
7807 * Setup an SSL context
7808 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007809
7810static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7811{
7812 /* Set the incoming and outgoing record pointers. */
7813#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007814 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007815 {
7816 ssl->out_hdr = ssl->out_buf;
7817 ssl->in_hdr = ssl->in_buf;
7818 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007819 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007821#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007822 {
7823 ssl->out_hdr = ssl->out_buf + 8;
7824 ssl->in_hdr = ssl->in_buf + 8;
7825 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007826#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007827
7828 /* Derive other internal pointers. */
7829 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007830 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007831}
7832
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007833int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007834 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835{
Paul Bakker48916f92012-09-16 19:57:18 +00007836 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007837
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007838 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007839
7840 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007841 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007842 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007843
7844 /* Set to NULL in case of an error condition */
7845 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007846
Angus Grattond8213d02016-05-25 20:56:48 +10007847 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7848 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 {
Angus Grattond8213d02016-05-25 20:56:48 +10007850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007851 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007852 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007853 }
7854
7855 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7856 if( ssl->out_buf == NULL )
7857 {
7858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007859 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007860 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 }
7862
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007863 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007864
Paul Bakker48916f92012-09-16 19:57:18 +00007865 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007866 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007867
7868 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007869
7870error:
7871 mbedtls_free( ssl->in_buf );
7872 mbedtls_free( ssl->out_buf );
7873
7874 ssl->conf = NULL;
7875
7876 ssl->in_buf = NULL;
7877 ssl->out_buf = NULL;
7878
7879 ssl->in_hdr = NULL;
7880 ssl->in_ctr = NULL;
7881 ssl->in_len = NULL;
7882 ssl->in_iv = NULL;
7883 ssl->in_msg = NULL;
7884
7885 ssl->out_hdr = NULL;
7886 ssl->out_ctr = NULL;
7887 ssl->out_len = NULL;
7888 ssl->out_iv = NULL;
7889 ssl->out_msg = NULL;
7890
k-stachowiak9f7798e2018-07-31 16:52:32 +02007891 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007892}
7893
7894/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007895 * Reset an initialized and used SSL context for re-use while retaining
7896 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007897 *
7898 * If partial is non-zero, keep data in the input buffer and client ID.
7899 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007900 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007901static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007902{
Paul Bakker48916f92012-09-16 19:57:18 +00007903 int ret;
7904
Hanno Becker7e772132018-08-10 12:38:21 +01007905#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7906 !defined(MBEDTLS_SSL_SRV_C)
7907 ((void) partial);
7908#endif
7909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007911
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007912 /* Cancel any possibly running timer */
7913 ssl_set_timer( ssl, 0 );
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_SSL_RENEGOTIATION)
7916 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007917 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007918
7919 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7921 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007922#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007924
Paul Bakker7eb013f2011-10-06 12:37:39 +00007925 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007926 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007927
7928 ssl->in_msgtype = 0;
7929 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007931 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007932 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007935 ssl_dtls_replay_reset( ssl );
7936#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007937
7938 ssl->in_hslen = 0;
7939 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007940
7941 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007942
7943 ssl->out_msgtype = 0;
7944 ssl->out_msglen = 0;
7945 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7947 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007948 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007949#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007950
Hanno Becker19859472018-08-06 09:40:20 +01007951 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7952
Paul Bakker48916f92012-09-16 19:57:18 +00007953 ssl->transform_in = NULL;
7954 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007955
Hanno Becker78640902018-08-13 16:35:15 +01007956 ssl->session_in = NULL;
7957 ssl->session_out = NULL;
7958
Angus Grattond8213d02016-05-25 20:56:48 +10007959 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007960
7961#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007962 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007963#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7964 {
7965 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007966 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007967 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7970 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7973 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7976 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007977 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007978 }
7979#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007980
Paul Bakker48916f92012-09-16 19:57:18 +00007981 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 mbedtls_ssl_transform_free( ssl->transform );
7984 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007985 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007986 }
Paul Bakker48916f92012-09-16 19:57:18 +00007987
Paul Bakkerc0463502013-02-14 11:19:38 +01007988 if( ssl->session )
7989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990 mbedtls_ssl_session_free( ssl->session );
7991 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007992 ssl->session = NULL;
7993 }
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007996 ssl->alpn_chosen = NULL;
7997#endif
7998
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007999#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008000#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008001 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008002#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008003 {
8004 mbedtls_free( ssl->cli_id );
8005 ssl->cli_id = NULL;
8006 ssl->cli_id_len = 0;
8007 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008008#endif
8009
Paul Bakker48916f92012-09-16 19:57:18 +00008010 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8011 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008012
8013 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008014}
8015
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008016/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008017 * Reset an initialized and used SSL context for re-use while retaining
8018 * all application-set variables, function pointers and data.
8019 */
8020int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8021{
8022 return( ssl_session_reset_int( ssl, 0 ) );
8023}
8024
8025/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008026 * SSL set accessors
8027 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008028void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008029{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008030 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008031}
8032
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008033void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008034{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008035 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008036}
8037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008039void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008040{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008041 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008042}
8043#endif
8044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008045#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008046void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008048 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008049}
8050#endif
8051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008053
Hanno Becker1841b0a2018-08-24 11:13:57 +01008054void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8055 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008056{
8057 ssl->disable_datagram_packing = !allow_packing;
8058}
8059
8060void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8061 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008062{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008063 conf->hs_timeout_min = min;
8064 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008065}
8066#endif
8067
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008068void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008069{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008070 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008071}
8072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008074void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008075 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008076 void *p_vrfy )
8077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008078 conf->f_vrfy = f_vrfy;
8079 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008081#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008082
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008083void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008084 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008085 void *p_rng )
8086{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008087 conf->f_rng = f_rng;
8088 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008089}
8090
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008091void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008092 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008093 void *p_dbg )
8094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008095 conf->f_dbg = f_dbg;
8096 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008097}
8098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008100 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008101 mbedtls_ssl_send_t *f_send,
8102 mbedtls_ssl_recv_t *f_recv,
8103 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008104{
8105 ssl->p_bio = p_bio;
8106 ssl->f_send = f_send;
8107 ssl->f_recv = f_recv;
8108 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008109}
8110
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008111#if defined(MBEDTLS_SSL_PROTO_DTLS)
8112void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8113{
8114 ssl->mtu = mtu;
8115}
8116#endif
8117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008118void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008119{
8120 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008121}
8122
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008123void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8124 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008125 mbedtls_ssl_set_timer_t *f_set_timer,
8126 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008127{
8128 ssl->p_timer = p_timer;
8129 ssl->f_set_timer = f_set_timer;
8130 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008131
8132 /* Make sure we start with no timer running */
8133 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008134}
8135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008137void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008138 void *p_cache,
8139 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8140 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008141{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008142 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008143 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008144 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008146#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148#if defined(MBEDTLS_SSL_CLI_C)
8149int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008150{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008151 int ret;
8152
8153 if( ssl == NULL ||
8154 session == NULL ||
8155 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008156 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008159 }
8160
Hanno Becker58fccf22019-02-06 14:30:46 +00008161 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8162 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008163 return( ret );
8164
Paul Bakker0a597072012-09-25 21:55:46 +00008165 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008166
8167 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008169#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008170
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008171void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008172 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008173{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008174 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8175 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8176 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8177 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008178}
8179
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008180void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008181 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008182 int major, int minor )
8183{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008185 return;
8186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008188 return;
8189
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008190 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008191}
8192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008193#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008194void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008195 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008196{
8197 conf->cert_profile = profile;
8198}
8199
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008200/* Append a new keycert entry to a (possibly empty) list */
8201static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8202 mbedtls_x509_crt *cert,
8203 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008204{
niisato8ee24222018-06-25 19:05:48 +09008205 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008206
niisato8ee24222018-06-25 19:05:48 +09008207 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8208 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008209 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008210
niisato8ee24222018-06-25 19:05:48 +09008211 new_cert->cert = cert;
8212 new_cert->key = key;
8213 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008214
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008215 /* Update head is the list was null, else add to the end */
8216 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008217 {
niisato8ee24222018-06-25 19:05:48 +09008218 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008219 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008220 else
8221 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008222 mbedtls_ssl_key_cert *cur = *head;
8223 while( cur->next != NULL )
8224 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008225 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008226 }
8227
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008228 return( 0 );
8229}
8230
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008231int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008232 mbedtls_x509_crt *own_cert,
8233 mbedtls_pk_context *pk_key )
8234{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008235 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008236}
8237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008238void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008239 mbedtls_x509_crt *ca_chain,
8240 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008241{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008242 conf->ca_chain = ca_chain;
8243 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008246
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008247#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8248int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8249 mbedtls_x509_crt *own_cert,
8250 mbedtls_pk_context *pk_key )
8251{
8252 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8253 own_cert, pk_key ) );
8254}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008255
8256void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8257 mbedtls_x509_crt *ca_chain,
8258 mbedtls_x509_crl *ca_crl )
8259{
8260 ssl->handshake->sni_ca_chain = ca_chain;
8261 ssl->handshake->sni_ca_crl = ca_crl;
8262}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008263
8264void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8265 int authmode )
8266{
8267 ssl->handshake->sni_authmode = authmode;
8268}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008269#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8270
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008271#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008272/*
8273 * Set EC J-PAKE password for current handshake
8274 */
8275int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8276 const unsigned char *pw,
8277 size_t pw_len )
8278{
8279 mbedtls_ecjpake_role role;
8280
Janos Follath8eb64132016-06-03 15:40:57 +01008281 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8283
8284 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8285 role = MBEDTLS_ECJPAKE_SERVER;
8286 else
8287 role = MBEDTLS_ECJPAKE_CLIENT;
8288
8289 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8290 role,
8291 MBEDTLS_MD_SHA256,
8292 MBEDTLS_ECP_DP_SECP256R1,
8293 pw, pw_len ) );
8294}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008295#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008298int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008299 const unsigned char *psk, size_t psk_len,
8300 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008301{
Paul Bakker6db455e2013-09-18 17:29:31 +02008302 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008307
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008308 /* Identity len will be encoded on two bytes */
8309 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008310 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008311 {
8312 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8313 }
8314
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008315 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008316 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008317 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008318
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008319 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008320 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008321 conf->psk_len = 0;
8322 }
8323 if( conf->psk_identity != NULL )
8324 {
8325 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008326 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008327 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008328 }
8329
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008330 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8331 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008332 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008333 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008334 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008335 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008336 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008337 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008338 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008339
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008340 conf->psk_len = psk_len;
8341 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008342
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008343 memcpy( conf->psk, psk, conf->psk_len );
8344 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008345
8346 return( 0 );
8347}
8348
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008349int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8350 const unsigned char *psk, size_t psk_len )
8351{
8352 if( psk == NULL || ssl->handshake == NULL )
8353 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8354
8355 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8356 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8357
8358 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008359 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008360 mbedtls_platform_zeroize( ssl->handshake->psk,
8361 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008362 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008363 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008364 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008365
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008366 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008367 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008368
8369 ssl->handshake->psk_len = psk_len;
8370 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8371
8372 return( 0 );
8373}
8374
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008375void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008376 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008377 size_t),
8378 void *p_psk )
8379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008380 conf->f_psk = f_psk;
8381 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008383#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008384
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008385#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008386
8387#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008388int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008389{
8390 int ret;
8391
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008392 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8393 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8394 {
8395 mbedtls_mpi_free( &conf->dhm_P );
8396 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008397 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008398 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008399
8400 return( 0 );
8401}
Hanno Becker470a8c42017-10-04 15:28:46 +01008402#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008403
Hanno Beckera90658f2017-10-04 15:29:08 +01008404int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8405 const unsigned char *dhm_P, size_t P_len,
8406 const unsigned char *dhm_G, size_t G_len )
8407{
8408 int ret;
8409
8410 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8411 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8412 {
8413 mbedtls_mpi_free( &conf->dhm_P );
8414 mbedtls_mpi_free( &conf->dhm_G );
8415 return( ret );
8416 }
8417
8418 return( 0 );
8419}
Paul Bakker5121ce52009-01-03 21:22:43 +00008420
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008421int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008422{
8423 int ret;
8424
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008425 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8426 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8427 {
8428 mbedtls_mpi_free( &conf->dhm_P );
8429 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008430 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008431 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008432
8433 return( 0 );
8434}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008435#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008436
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008437#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8438/*
8439 * Set the minimum length for Diffie-Hellman parameters
8440 */
8441void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8442 unsigned int bitlen )
8443{
8444 conf->dhm_min_bitlen = bitlen;
8445}
8446#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8447
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008448#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008449/*
8450 * Set allowed/preferred hashes for handshake signatures
8451 */
8452void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8453 const int *hashes )
8454{
8455 conf->sig_hashes = hashes;
8456}
Hanno Becker947194e2017-04-07 13:25:49 +01008457#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008458
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008459#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008460/*
8461 * Set the allowed elliptic curves
8462 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008463void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008464 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008466 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008467}
Hanno Becker947194e2017-04-07 13:25:49 +01008468#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008469
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008470#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008472{
Hanno Becker947194e2017-04-07 13:25:49 +01008473 /* Initialize to suppress unnecessary compiler warning */
8474 size_t hostname_len = 0;
8475
8476 /* Check if new hostname is valid before
8477 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008478 if( hostname != NULL )
8479 {
8480 hostname_len = strlen( hostname );
8481
8482 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8483 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8484 }
8485
8486 /* Now it's clear that we will overwrite the old hostname,
8487 * so we can free it safely */
8488
8489 if( ssl->hostname != NULL )
8490 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008491 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008492 mbedtls_free( ssl->hostname );
8493 }
8494
8495 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008496
Paul Bakker5121ce52009-01-03 21:22:43 +00008497 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008498 {
8499 ssl->hostname = NULL;
8500 }
8501 else
8502 {
8503 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008504 if( ssl->hostname == NULL )
8505 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008506
Hanno Becker947194e2017-04-07 13:25:49 +01008507 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008508
Hanno Becker947194e2017-04-07 13:25:49 +01008509 ssl->hostname[hostname_len] = '\0';
8510 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008511
8512 return( 0 );
8513}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008514#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008515
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008516#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008517void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008519 const unsigned char *, size_t),
8520 void *p_sni )
8521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008522 conf->f_sni = f_sni;
8523 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008528int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008529{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008530 size_t cur_len, tot_len;
8531 const char **p;
8532
8533 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008534 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8535 * MUST NOT be truncated."
8536 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008537 */
8538 tot_len = 0;
8539 for( p = protos; *p != NULL; p++ )
8540 {
8541 cur_len = strlen( *p );
8542 tot_len += cur_len;
8543
8544 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008545 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008546 }
8547
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008548 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008549
8550 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008551}
8552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008554{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008555 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008558
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008559void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008560{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008561 conf->max_major_ver = major;
8562 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008563}
8564
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008565void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008566{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008567 conf->min_major_ver = major;
8568 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008569}
8570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008571#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008573{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008574 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008575}
8576#endif
8577
Janos Follath088ce432017-04-10 12:42:31 +01008578#if defined(MBEDTLS_SSL_SRV_C)
8579void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8580 char cert_req_ca_list )
8581{
8582 conf->cert_req_ca_list = cert_req_ca_list;
8583}
8584#endif
8585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008587void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008588{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008589 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008590}
8591#endif
8592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008594void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008595{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008596 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008597}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008598
8599void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008600 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008601{
8602 conf->enforce_extended_master_secret = ems_enf;
8603}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008604#endif
8605
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008606#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008607void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008608{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008609 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008610}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008611#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008613#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008614int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008617 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008620 }
8621
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008622 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008623
8624 return( 0 );
8625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008626#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008628#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008629void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008630{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008631 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008633#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008636void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008637{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008638 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008639}
8640#endif
8641
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008642void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008643{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008644 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008645}
8646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008647#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008648void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008649{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008650 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008651}
8652
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008653void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008655 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008656}
8657
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008658void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008659 const unsigned char period[8] )
8660{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008661 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008663#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008665#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008666#if defined(MBEDTLS_SSL_CLI_C)
8667void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008668{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008669 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008670}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008671#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008672
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008673#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008674void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8675 mbedtls_ssl_ticket_write_t *f_ticket_write,
8676 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8677 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008678{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008679 conf->f_ticket_write = f_ticket_write;
8680 conf->f_ticket_parse = f_ticket_parse;
8681 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008682}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008683#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008685
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008686#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8687void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8688 mbedtls_ssl_export_keys_t *f_export_keys,
8689 void *p_export_keys )
8690{
8691 conf->f_export_keys = f_export_keys;
8692 conf->p_export_keys = p_export_keys;
8693}
8694#endif
8695
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008696#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008697void mbedtls_ssl_conf_async_private_cb(
8698 mbedtls_ssl_config *conf,
8699 mbedtls_ssl_async_sign_t *f_async_sign,
8700 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8701 mbedtls_ssl_async_resume_t *f_async_resume,
8702 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008703 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008704{
8705 conf->f_async_sign_start = f_async_sign;
8706 conf->f_async_decrypt_start = f_async_decrypt;
8707 conf->f_async_resume = f_async_resume;
8708 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008709 conf->p_async_config_data = async_config_data;
8710}
8711
Gilles Peskine8f97af72018-04-26 11:46:10 +02008712void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8713{
8714 return( conf->p_async_config_data );
8715}
8716
Gilles Peskine1febfef2018-04-30 11:54:39 +02008717void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008718{
8719 if( ssl->handshake == NULL )
8720 return( NULL );
8721 else
8722 return( ssl->handshake->user_async_ctx );
8723}
8724
Gilles Peskine1febfef2018-04-30 11:54:39 +02008725void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008726 void *ctx )
8727{
8728 if( ssl->handshake != NULL )
8729 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008730}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008731#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008732
Paul Bakker5121ce52009-01-03 21:22:43 +00008733/*
8734 * SSL get accessors
8735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008737{
8738 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8739}
8740
Hanno Becker8b170a02017-10-10 11:51:19 +01008741int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8742{
8743 /*
8744 * Case A: We're currently holding back
8745 * a message for further processing.
8746 */
8747
8748 if( ssl->keep_current_message == 1 )
8749 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008750 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008751 return( 1 );
8752 }
8753
8754 /*
8755 * Case B: Further records are pending in the current datagram.
8756 */
8757
8758#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008759 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008760 ssl->in_left > ssl->next_record_offset )
8761 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008762 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008763 return( 1 );
8764 }
8765#endif /* MBEDTLS_SSL_PROTO_DTLS */
8766
8767 /*
8768 * Case C: A handshake message is being processed.
8769 */
8770
Hanno Becker8b170a02017-10-10 11:51:19 +01008771 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8772 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008774 return( 1 );
8775 }
8776
8777 /*
8778 * Case D: An application data message is being processed
8779 */
8780 if( ssl->in_offt != NULL )
8781 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008783 return( 1 );
8784 }
8785
8786 /*
8787 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008788 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008789 * we implement support for multiple alerts in single records.
8790 */
8791
8792 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8793 return( 0 );
8794}
8795
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008796uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008797{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008798 if( ssl->session != NULL )
8799 return( ssl->session->verify_result );
8800
8801 if( ssl->session_negotiate != NULL )
8802 return( ssl->session_negotiate->verify_result );
8803
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008804 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008805}
8806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008807const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008808{
Paul Bakker926c8e42013-03-06 10:23:34 +01008809 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008810 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008813}
8814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008815const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008816{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008818 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008819 {
8820 switch( ssl->minor_ver )
8821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008823 return( "DTLSv1.0" );
8824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008825 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008826 return( "DTLSv1.2" );
8827
8828 default:
8829 return( "unknown (DTLS)" );
8830 }
8831 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008832 MBEDTLS_SSL_TRANSPORT_ELSE
8833#endif /* MBEDTLS_SSL_PROTO_DTLS */
8834#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008835 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008836 switch( ssl->minor_ver )
8837 {
8838 case MBEDTLS_SSL_MINOR_VERSION_0:
8839 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008840
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008841 case MBEDTLS_SSL_MINOR_VERSION_1:
8842 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008843
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008844 case MBEDTLS_SSL_MINOR_VERSION_2:
8845 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008846
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008847 case MBEDTLS_SSL_MINOR_VERSION_3:
8848 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008849
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008850 default:
8851 return( "unknown" );
8852 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008853 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008854#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008855}
8856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008857int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008858{
Hanno Becker3136ede2018-08-17 15:28:19 +01008859 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008861 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008862
Hanno Becker43395762019-05-03 14:46:38 +01008863 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8864
Hanno Becker78640902018-08-13 16:35:15 +01008865 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008866 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#if defined(MBEDTLS_ZLIB_SUPPORT)
8869 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8870 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008871#endif
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008875 case MBEDTLS_MODE_GCM:
8876 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008877 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008879 transform_expansion = transform->minlen;
8880 break;
8881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008882 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008883
8884 block_size = mbedtls_cipher_get_block_size(
8885 &transform->cipher_ctx_enc );
8886
Hanno Becker3136ede2018-08-17 15:28:19 +01008887 /* Expansion due to the addition of the MAC. */
8888 transform_expansion += transform->maclen;
8889
8890 /* Expansion due to the addition of CBC padding;
8891 * Theoretically up to 256 bytes, but we never use
8892 * more than the block size of the underlying cipher. */
8893 transform_expansion += block_size;
8894
8895 /* For TLS 1.1 or higher, an explicit IV is added
8896 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008897#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8898 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008899 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008900#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008901
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008902 break;
8903
8904 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008907 }
8908
Hanno Beckera5a2b082019-05-15 14:03:01 +01008909#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008910 if( transform->out_cid_len != 0 )
8911 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008912#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008913
Hanno Becker43395762019-05-03 14:46:38 +01008914 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008915}
8916
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008917#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8918size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8919{
8920 size_t max_len;
8921
8922 /*
8923 * Assume mfl_code is correct since it was checked when set
8924 */
Angus Grattond8213d02016-05-25 20:56:48 +10008925 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008926
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008927 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008928 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008929 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008930 {
Angus Grattond8213d02016-05-25 20:56:48 +10008931 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008932 }
8933
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008934 /* During a handshake, use the value being negotiated */
8935 if( ssl->session_negotiate != NULL &&
8936 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8937 {
8938 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8939 }
8940
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008941 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008942}
8943#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8944
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008945#if defined(MBEDTLS_SSL_PROTO_DTLS)
8946static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8947{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008948 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8949 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8950 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8951 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8952 return ( 0 );
8953
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008954 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8955 return( ssl->mtu );
8956
8957 if( ssl->mtu == 0 )
8958 return( ssl->handshake->mtu );
8959
8960 return( ssl->mtu < ssl->handshake->mtu ?
8961 ssl->mtu : ssl->handshake->mtu );
8962}
8963#endif /* MBEDTLS_SSL_PROTO_DTLS */
8964
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008965int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8966{
8967 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8968
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008969#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8970 !defined(MBEDTLS_SSL_PROTO_DTLS)
8971 (void) ssl;
8972#endif
8973
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008974#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8975 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8976
8977 if( max_len > mfl )
8978 max_len = mfl;
8979#endif
8980
8981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008982 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008983 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008984 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008985 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8986 const size_t overhead = (size_t) ret;
8987
8988 if( ret < 0 )
8989 return( ret );
8990
8991 if( mtu <= overhead )
8992 {
8993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8994 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8995 }
8996
8997 if( max_len > mtu - overhead )
8998 max_len = mtu - overhead;
8999 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009000#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009001
Hanno Becker0defedb2018-08-10 12:35:02 +01009002#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9003 !defined(MBEDTLS_SSL_PROTO_DTLS)
9004 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009005#endif
9006
9007 return( (int) max_len );
9008}
9009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#if defined(MBEDTLS_X509_CRT_PARSE_C)
9011const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009012{
9013 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009014 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009015
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009016#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009017 return( ssl->session->peer_cert );
Hanno Beckerbfab9df2019-02-07 13:18:46 +00009018#else
9019 return( NULL );
9020#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009021}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009022#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00009025int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9026 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009027{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009028 if( ssl == NULL ||
9029 dst == NULL ||
9030 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009031 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009034 }
9035
Hanno Becker58fccf22019-02-06 14:30:46 +00009036 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009039
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02009040const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9041{
9042 if( ssl == NULL )
9043 return( NULL );
9044
9045 return( ssl->session );
9046}
9047
Paul Bakker5121ce52009-01-03 21:22:43 +00009048/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009049 * Define ticket header determining Mbed TLS version
9050 * and structure of the ticket.
9051 */
9052
Hanno Becker41527622019-05-16 12:50:45 +01009053/*
Hanno Becker26829e92019-05-28 14:30:45 +01009054 * Define bitflag determining compile-time settings influencing
9055 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01009056 */
9057
Hanno Becker26829e92019-05-28 14:30:45 +01009058#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009059#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01009060#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009061#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01009062#endif /* MBEDTLS_HAVE_TIME */
9063
9064#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009065#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01009066#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009067#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01009068#endif /* MBEDTLS_X509_CRT_PARSE_C */
9069
9070#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009071#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01009072#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009073#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01009074#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
9075
9076#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009077#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01009078#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009079#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01009080#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9081
9082#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009083#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01009084#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009085#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01009086#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
9087
9088#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009089#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01009090#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009091#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01009092#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
9093
Hanno Becker41527622019-05-16 12:50:45 +01009094#if defined(MBEDTLS_SSL_SESSION_TICKETS)
9095#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
9096#else
9097#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
9098#endif /* MBEDTLS_SSL_SESSION_TICKETS */
9099
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009100#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9101#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 1
9102#else
9103#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT 0
9104#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9105
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009106#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
9107#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
9108#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
9109#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
9110#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
9111#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
9112#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009113#define SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT 7
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009114
Hanno Becker26829e92019-05-28 14:30:45 +01009115#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01009116 ( (uint16_t) ( \
9117 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
9118 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
9119 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
9120 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
9121 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
9122 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009123 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) | \
9124 ( SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT << SSL_SERIALIZED_SESSION_CONFIG_KEEP_CRT_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01009125
Hanno Becker557fe9f2019-05-16 12:41:07 +01009126static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01009127 MBEDTLS_VERSION_MAJOR,
9128 MBEDTLS_VERSION_MINOR,
9129 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01009130 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
9131 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01009132};
Hanno Beckerb5352f02019-05-16 12:39:07 +01009133
9134/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009135 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009136 * (in the presentation language of TLS, RFC 8446 section 3)
9137 *
Hanno Becker26829e92019-05-28 14:30:45 +01009138 * opaque mbedtls_version[3]; // major, minor, patch
9139 * opaque session_format[2]; // version-specific 16-bit field determining
9140 * // the format of the remaining
9141 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01009142 *
9143 * Note: When updating the format, remember to keep
9144 * these version+format bytes.
9145 *
Hanno Becker7bf77102019-06-04 09:43:16 +01009146 * // In this version, `session_format` determines
9147 * // the setting of those compile-time
9148 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01009149 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009150 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01009151 * uint8 ciphersuite[2]; // defined by the standard
9152 * uint8 compression; // 0 or 1
9153 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009154 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01009155 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009156 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01009157 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009158 * uint8_t peer_cert_digest_type;
9159 * opaque peer_cert_digest<0..2^8-1>;
Hanno Becker26829e92019-05-28 14:30:45 +01009160 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009161 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01009162 * uint8 mfl_code; // up to 255 according to standard
9163 * uint8 trunc_hmac; // 0 or 1
9164 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009165 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009166 * The order is the same as in the definition of the structure, except
9167 * verify_result is put before peer_cert so that all mandatory fields come
9168 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009169 */
9170int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9171 unsigned char *buf,
9172 size_t buf_len,
9173 size_t *olen )
9174{
9175 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009176 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009177#if defined(MBEDTLS_HAVE_TIME)
9178 uint64_t start;
9179#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009180#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009181#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009182 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009183#endif
Hanno Becker2e6d3472019-02-06 15:40:27 +00009184#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009185
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009186 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009187 * Add version identifier
9188 */
9189
9190 used += sizeof( ssl_serialized_session_header );
9191
9192 if( used <= buf_len )
9193 {
9194 memcpy( p, ssl_serialized_session_header,
9195 sizeof( ssl_serialized_session_header ) );
9196 p += sizeof( ssl_serialized_session_header );
9197 }
9198
9199 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009200 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009201 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009202#if defined(MBEDTLS_HAVE_TIME)
9203 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009204
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009205 if( used <= buf_len )
9206 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009207 start = (uint64_t) session->start;
9208
9209 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9210 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9211 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9212 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9213 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9214 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9215 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9216 *p++ = (unsigned char)( ( start ) & 0xFF );
9217 }
9218#endif /* MBEDTLS_HAVE_TIME */
9219
9220 /*
9221 * Basic mandatory fields
9222 */
9223 used += 2 /* ciphersuite */
9224 + 1 /* compression */
9225 + 1 /* id_len */
9226 + sizeof( session->id )
9227 + sizeof( session->master )
9228 + 4; /* verify_result */
9229
9230 if( used <= buf_len )
9231 {
9232 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9233 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9234
9235 *p++ = (unsigned char)( session->compression & 0xFF );
9236
9237 *p++ = (unsigned char)( session->id_len & 0xFF );
9238 memcpy( p, session->id, 32 );
9239 p += 32;
9240
9241 memcpy( p, session->master, 48 );
9242 p += 48;
9243
9244 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9245 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9246 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9247 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009248 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009249
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009250 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009251 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009252 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009253#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009254#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009255 if( session->peer_cert == NULL )
9256 cert_len = 0;
9257 else
9258 cert_len = session->peer_cert->raw.len;
9259
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009260 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009261
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009262 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009263 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009264 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9265 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9266 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9267
9268 if( session->peer_cert != NULL )
9269 {
9270 memcpy( p, session->peer_cert->raw.p, cert_len );
9271 p += cert_len;
9272 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009273 }
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009274
Hanno Becker2e6d3472019-02-06 15:40:27 +00009275#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009276 /* Digest of peer certificate */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009277 if( session->peer_cert_digest != NULL )
9278 {
9279 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9280 if( used <= buf_len )
9281 {
9282 *p++ = (unsigned char) session->peer_cert_digest_type;
9283 *p++ = (unsigned char) session->peer_cert_digest_len;
9284 memcpy( p, session->peer_cert_digest,
9285 session->peer_cert_digest_len );
9286 p += session->peer_cert_digest_len;
9287 }
9288 }
9289 else
9290 {
9291 used += 2;
9292 if( used <= buf_len )
9293 {
9294 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9295 *p++ = 0;
9296 }
9297 }
9298#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009299#endif /* MBEDTLS_X509_CRT_PARSE_C */
9300
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009301 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009302 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009303 */
9304#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009305 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009306
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009307 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009308 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009309 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9310 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9311 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9312
9313 if( session->ticket != NULL )
9314 {
9315 memcpy( p, session->ticket, session->ticket_len );
9316 p += session->ticket_len;
9317 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009318
9319 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9320 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9321 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9322 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009323 }
9324#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9325
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009326 /*
9327 * Misc extension-related info
9328 */
9329#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9330 used += 1;
9331
9332 if( used <= buf_len )
9333 *p++ = session->mfl_code;
9334#endif
9335
9336#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9337 used += 1;
9338
9339 if( used <= buf_len )
9340 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9341#endif
9342
9343#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9344 used += 1;
9345
9346 if( used <= buf_len )
9347 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9348#endif
9349
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009350 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009351 *olen = used;
9352
9353 if( used > buf_len )
9354 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009355
9356 return( 0 );
9357}
9358
9359/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009360 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009361 *
9362 * This internal version is wrapped by a public function that cleans up in
9363 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009364 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009365static int ssl_session_load( mbedtls_ssl_session *session,
9366 const unsigned char *buf,
9367 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009368{
9369 const unsigned char *p = buf;
9370 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009371#if defined(MBEDTLS_HAVE_TIME)
9372 uint64_t start;
9373#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009374#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009375#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009376 size_t cert_len;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009377#endif
9378#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009379
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009380 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009381 * Check version identifier
9382 */
9383
9384 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009386
9387 if( memcmp( p, ssl_serialized_session_header,
9388 sizeof( ssl_serialized_session_header ) ) != 0 )
9389 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009390 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009391 }
9392 p += sizeof( ssl_serialized_session_header );
9393
9394 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009395 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009396 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009397#if defined(MBEDTLS_HAVE_TIME)
9398 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9400
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009401 start = ( (uint64_t) p[0] << 56 ) |
9402 ( (uint64_t) p[1] << 48 ) |
9403 ( (uint64_t) p[2] << 40 ) |
9404 ( (uint64_t) p[3] << 32 ) |
9405 ( (uint64_t) p[4] << 24 ) |
9406 ( (uint64_t) p[5] << 16 ) |
9407 ( (uint64_t) p[6] << 8 ) |
9408 ( (uint64_t) p[7] );
9409 p += 8;
9410
9411 session->start = (time_t) start;
9412#endif /* MBEDTLS_HAVE_TIME */
9413
9414 /*
9415 * Basic mandatory fields
9416 */
9417 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9419
9420 session->ciphersuite = ( p[0] << 8 ) | p[1];
9421 p += 2;
9422
9423 session->compression = *p++;
9424
9425 session->id_len = *p++;
9426 memcpy( session->id, p, 32 );
9427 p += 32;
9428
9429 memcpy( session->master, p, 48 );
9430 p += 48;
9431
9432 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9433 ( (uint32_t) p[1] << 16 ) |
9434 ( (uint32_t) p[2] << 8 ) |
9435 ( (uint32_t) p[3] );
9436 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009437
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009438 /* Immediately clear invalid pointer values that have been read, in case
9439 * we exit early before we replaced them with valid ones. */
9440#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009441#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009442 session->peer_cert = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009443#else
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009444 session->peer_cert_digest = NULL;
Hanno Becker2e6d3472019-02-06 15:40:27 +00009445#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009446#endif
9447#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9448 session->ticket = NULL;
9449#endif
9450
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009451 /*
9452 * Peer certificate
9453 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009454#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker2e6d3472019-02-06 15:40:27 +00009455#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009456 if( 3 > (size_t)( end - p ) )
9457 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9458
9459 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9460 p += 3;
9461
9462 if( cert_len == 0 )
9463 {
9464 session->peer_cert = NULL;
9465 }
9466 else
9467 {
9468 int ret;
9469
9470 if( cert_len > (size_t)( end - p ) )
9471 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9472
9473 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9474
9475 if( session->peer_cert == NULL )
9476 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9477
9478 mbedtls_x509_crt_init( session->peer_cert );
9479
9480 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9481 p, cert_len ) ) != 0 )
9482 {
9483 mbedtls_x509_crt_free( session->peer_cert );
9484 mbedtls_free( session->peer_cert );
9485 session->peer_cert = NULL;
9486 return( ret );
9487 }
9488
9489 p += cert_len;
9490 }
Hanno Becker2e6d3472019-02-06 15:40:27 +00009491#else /* defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009492 /* Deserialize CRT digest from the end of the ticket. */
9493 if( 2 > (size_t)( end - p ) )
9494 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9495
9496 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9497 session->peer_cert_digest_len = (size_t) *p++;
9498
9499 if( session->peer_cert_digest_len != 0 )
9500 {
Hanno Becker2326d202019-06-06 14:54:55 +01009501 const mbedtls_md_info_t *md_info =
9502 mbedtls_md_info_from_type( session->peer_cert_digest_type );
9503 if( md_info == NULL )
9504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9505 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
9506 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9507
Hanno Becker4a2f8e52019-02-06 15:23:38 +00009508 if( session->peer_cert_digest_len > (size_t)( end - p ) )
9509 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9510
9511 session->peer_cert_digest =
9512 mbedtls_calloc( 1, session->peer_cert_digest_len );
9513 if( session->peer_cert_digest == NULL )
9514 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9515
9516 memcpy( session->peer_cert_digest, p,
9517 session->peer_cert_digest_len );
9518 p += session->peer_cert_digest_len;
9519 }
9520#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009521#endif /* MBEDTLS_X509_CRT_PARSE_C */
9522
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009523 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009524 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009525 */
9526#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9527 if( 3 > (size_t)( end - p ) )
9528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9529
9530 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9531 p += 3;
9532
9533 if( session->ticket_len != 0 )
9534 {
9535 if( session->ticket_len > (size_t)( end - p ) )
9536 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9537
9538 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9539 if( session->ticket == NULL )
9540 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9541
9542 memcpy( session->ticket, p, session->ticket_len );
9543 p += session->ticket_len;
9544 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009545
9546 if( 4 > (size_t)( end - p ) )
9547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9548
9549 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9550 ( (uint32_t) p[1] << 16 ) |
9551 ( (uint32_t) p[2] << 8 ) |
9552 ( (uint32_t) p[3] );
9553 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009554#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9555
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009556 /*
9557 * Misc extension-related info
9558 */
9559#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9560 if( 1 > (size_t)( end - p ) )
9561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9562
9563 session->mfl_code = *p++;
9564#endif
9565
9566#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9567 if( 1 > (size_t)( end - p ) )
9568 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9569
9570 session->trunc_hmac = *p++;
9571#endif
9572
9573#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9574 if( 1 > (size_t)( end - p ) )
9575 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9576
9577 session->encrypt_then_mac = *p++;
9578#endif
9579
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009580 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009581 if( p != end )
9582 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9583
9584 return( 0 );
9585}
9586
9587/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009588 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009589 */
9590int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9591 const unsigned char *buf,
9592 size_t len )
9593{
9594 int ret = ssl_session_load( session, buf, len );
9595
9596 if( ret != 0 )
9597 mbedtls_ssl_session_free( session );
9598
9599 return( ret );
9600}
9601
9602/*
Paul Bakker1961b702013-01-25 14:49:24 +01009603 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009608
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009609 if( ssl == NULL || ssl->conf == NULL )
9610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009613 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009617 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009619#endif
9620
Paul Bakker1961b702013-01-25 14:49:24 +01009621 return( ret );
9622}
9623
9624/*
9625 * Perform the SSL handshake
9626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009628{
9629 int ret = 0;
9630
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009631 if( ssl == NULL || ssl->conf == NULL )
9632 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009639
9640 if( ret != 0 )
9641 break;
9642 }
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009645
9646 return( ret );
9647}
9648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649#if defined(MBEDTLS_SSL_RENEGOTIATION)
9650#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009651/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009652 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009655{
9656 int ret;
9657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009659
9660 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9662 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009663
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009664 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009665 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009667 return( ret );
9668 }
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009671
9672 return( 0 );
9673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009675
9676/*
9677 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 * - any side: calling mbedtls_ssl_renegotiate(),
9679 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9680 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009681 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009686{
9687 int ret;
9688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009690
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009691 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9692 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009693
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009694 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9695 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009697 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009699 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009700 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009701 ssl->handshake->out_msg_seq = 1;
9702 else
9703 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009704 }
9705#endif
9706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9708 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009713 return( ret );
9714 }
9715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009717
9718 return( 0 );
9719}
9720
9721/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009722 * Renegotiate current connection on client,
9723 * or request renegotiation on server
9724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009728
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009729 if( ssl == NULL || ssl->conf == NULL )
9730 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009733 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009734 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009740
9741 /* Did we already try/start sending HelloRequest? */
9742 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009744
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009745 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009746 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009750 /*
9751 * On client, either start the renegotiation process or,
9752 * if already in progress, continue the handshake
9753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009758
9759 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009761 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009762 return( ret );
9763 }
9764 }
9765 else
9766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009770 return( ret );
9771 }
9772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009774
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009775 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009776}
9777
9778/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009779 * Check record counters and renegotiate if they're above the limit.
9780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009782{
Andres AG2196c7f2016-12-15 17:01:16 +00009783 size_t ep_len = ssl_ep_len( ssl );
9784 int in_ctr_cmp;
9785 int out_ctr_cmp;
9786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9788 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009789 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009790 {
9791 return( 0 );
9792 }
9793
Andres AG2196c7f2016-12-15 17:01:16 +00009794 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9795 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009796 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009797 ssl->conf->renego_period + ep_len, 8 - ep_len );
9798
9799 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009800 {
9801 return( 0 );
9802 }
9803
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009808
9809/*
9810 * Receive application data decrypted from the SSL layer
9811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009813{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009814 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009815 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009816
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009817 if( ssl == NULL || ssl->conf == NULL )
9818 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009823 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009826 return( ret );
9827
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009828 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009830 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009831 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009832 return( ret );
9833 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009834 }
9835#endif
9836
Hanno Becker4a810fb2017-05-24 16:27:30 +01009837 /*
9838 * Check if renegotiation is necessary and/or handshake is
9839 * in process. If yes, perform/continue, and fall through
9840 * if an unexpected packet is received while the client
9841 * is waiting for the ServerHello.
9842 *
9843 * (There is no equivalent to the last condition on
9844 * the server-side as it is not treated as within
9845 * a handshake while waiting for the ClientHello
9846 * after a renegotiation request.)
9847 */
9848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009850 ret = ssl_check_ctr_renegotiate( ssl );
9851 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9852 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009855 return( ret );
9856 }
9857#endif
9858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009862 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9863 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009866 return( ret );
9867 }
9868 }
9869
Hanno Beckere41158b2017-10-23 13:30:32 +01009870 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009871 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009872 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009873 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009874 if( ssl->f_get_timer != NULL &&
9875 ssl->f_get_timer( ssl->p_timer ) == -1 )
9876 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009877 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009878 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009879
Hanno Becker327c93b2018-08-15 13:56:18 +01009880 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009881 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009882 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9883 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009884
Hanno Becker4a810fb2017-05-24 16:27:30 +01009885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9886 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009887 }
9888
9889 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009891 {
9892 /*
9893 * OpenSSL sends empty messages to randomize the IV
9894 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009895 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009898 return( 0 );
9899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009901 return( ret );
9902 }
9903 }
9904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009908
Hanno Becker4a810fb2017-05-24 16:27:30 +01009909 /*
9910 * - For client-side, expect SERVER_HELLO_REQUEST.
9911 * - For server-side, expect CLIENT_HELLO.
9912 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9913 */
9914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009916 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009918 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009921
9922 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009924 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009925 {
9926 continue;
9927 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009928 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009929#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009930#if defined(MBEDTLS_SSL_PROTO_TLS)
9931 {
9932 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9933 }
9934#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009935 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009936#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009937
Hanno Becker4a810fb2017-05-24 16:27:30 +01009938#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009939 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009943
9944 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009946 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009947 {
9948 continue;
9949 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009950 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009951#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009952#if defined(MBEDTLS_SSL_PROTO_TLS)
9953 {
9954 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9955 }
9956#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009957 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009958#endif /* MBEDTLS_SSL_SRV_C */
9959
Hanno Becker21df7f92017-10-17 11:03:26 +01009960#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009961 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009962 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9963 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9964 ssl->conf->allow_legacy_renegotiation ==
9965 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9966 {
9967 /*
9968 * Accept renegotiation request
9969 */
Paul Bakker48916f92012-09-16 19:57:18 +00009970
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009971 /* DTLS clients need to know renego is server-initiated */
9972#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009973 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009974 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9975 {
9976 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9977 }
9978#endif
9979 ret = ssl_start_renegotiation( ssl );
9980 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9981 ret != 0 )
9982 {
9983 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9984 return( ret );
9985 }
9986 }
9987 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009988#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009989 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009990 /*
9991 * Refuse renegotiation
9992 */
9993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_SSL_PROTO_SSL3)
9997 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009998 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009999 /* SSLv3 does not have a "no_renegotiation" warning, so
10000 we send a fatal alert and abort the connection. */
10001 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10002 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10003 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010004 }
10005 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10007#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10008 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10009 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010011 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10012 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10013 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010014 {
10015 return( ret );
10016 }
Paul Bakker48916f92012-09-16 19:57:18 +000010017 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010018 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10020 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010024 }
Paul Bakker48916f92012-09-16 19:57:18 +000010025 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010026
Hanno Becker90333da2017-10-10 11:27:13 +010010027 /* At this point, we don't know whether the renegotiation has been
10028 * completed or not. The cases to consider are the following:
10029 * 1) The renegotiation is complete. In this case, no new record
10030 * has been read yet.
10031 * 2) The renegotiation is incomplete because the client received
10032 * an application data record while awaiting the ServerHello.
10033 * 3) The renegotiation is incomplete because the client received
10034 * a non-handshake, non-application data message while awaiting
10035 * the ServerHello.
10036 * In each of these case, looping will be the proper action:
10037 * - For 1), the next iteration will read a new record and check
10038 * if it's application data.
10039 * - For 2), the loop condition isn't satisfied as application data
10040 * is present, hence continue is the same as break
10041 * - For 3), the loop condition is satisfied and read_record
10042 * will re-deliver the message that was held back by the client
10043 * when expecting the ServerHello.
10044 */
10045 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010046 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010047#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010049 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010050 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010051 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010052 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010055 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010057 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010058 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10063 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010066 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010067 }
10068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10072 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 }
10074
10075 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010076
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010077 /* We're going to return something now, cancel timer,
10078 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010080 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010081
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010083 /* If we requested renego but received AppData, resend HelloRequest.
10084 * Do it now, after setting in_offt, to avoid taking this branch
10085 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010086#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010087 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010089 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010090 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010093 return( ret );
10094 }
10095 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010097#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010098 }
10099
10100 n = ( len < ssl->in_msglen )
10101 ? len : ssl->in_msglen;
10102
10103 memcpy( buf, ssl->in_offt, n );
10104 ssl->in_msglen -= n;
10105
10106 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010107 {
10108 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010109 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010110 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010111 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010112 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010113 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010114 /* more data available */
10115 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010116 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010119
Paul Bakker23986e52011-04-24 08:57:21 +000010120 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010121}
10122
10123/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010124 * Send application data to be encrypted by the SSL layer, taking care of max
10125 * fragment length and buffer size.
10126 *
10127 * According to RFC 5246 Section 6.2.1:
10128 *
10129 * Zero-length fragments of Application data MAY be sent as they are
10130 * potentially useful as a traffic analysis countermeasure.
10131 *
10132 * Therefore, it is possible that the input message length is 0 and the
10133 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010134 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010135static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010136 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010137{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010138 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10139 const size_t max_len = (size_t) ret;
10140
10141 if( ret < 0 )
10142 {
10143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10144 return( ret );
10145 }
10146
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010147 if( len > max_len )
10148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010150 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010153 "maximum fragment length: %d > %d",
10154 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010156 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010157 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010158#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010159#if defined(MBEDTLS_SSL_PROTO_TLS)
10160 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010161 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010162 }
10163#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010164 }
Paul Bakker887bd502011-06-08 13:10:54 +000010165
Paul Bakker5121ce52009-01-03 21:22:43 +000010166 if( ssl->out_left != 0 )
10167 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010168 /*
10169 * The user has previously tried to send the data and
10170 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10171 * written. In this case, we expect the high-level write function
10172 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010177 return( ret );
10178 }
10179 }
Paul Bakker887bd502011-06-08 13:10:54 +000010180 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010181 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010182 /*
10183 * The user is trying to send a message the first time, so we need to
10184 * copy the data into the internal buffers and setup the data structure
10185 * to keep track of partial writes
10186 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010187 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010189 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010190
Hanno Becker67bc7c32018-08-06 11:33:50 +010010191 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010194 return( ret );
10195 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 }
10197
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010198 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199}
10200
10201/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010202 * Write application data, doing 1/n-1 splitting if necessary.
10203 *
10204 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010205 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010206 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010209static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010210 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010211{
10212 int ret;
10213
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010214 if( ssl->conf->cbc_record_splitting ==
10215 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010216 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10218 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10219 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010220 {
10221 return( ssl_write_real( ssl, buf, len ) );
10222 }
10223
10224 if( ssl->split_done == 0 )
10225 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010226 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010227 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010228 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010229 }
10230
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010231 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10232 return( ret );
10233 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010234
10235 return( ret + 1 );
10236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010238
10239/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010240 * Write application data (public-facing wrapper)
10241 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010242int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010243{
10244 int ret;
10245
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010247
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010248 if( ssl == NULL || ssl->conf == NULL )
10249 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10250
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010251#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010252 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10253 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010254 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010255 return( ret );
10256 }
10257#endif
10258
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010259 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010260 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010261 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010262 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010263 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010264 return( ret );
10265 }
10266 }
10267
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010268#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010269 ret = ssl_write_split( ssl, buf, len );
10270#else
10271 ret = ssl_write_real( ssl, buf, len );
10272#endif
10273
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010275
10276 return( ret );
10277}
10278
10279/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010280 * Notify the peer that the connection is being closed
10281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010283{
10284 int ret;
10285
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010286 if( ssl == NULL || ssl->conf == NULL )
10287 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010290
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010291 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010292 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010296 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10297 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10298 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010301 return( ret );
10302 }
10303 }
10304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010306
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010307 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010308}
10309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010311{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010312 if( transform == NULL )
10313 return;
10314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010316 deflateEnd( &transform->ctx_deflate );
10317 inflateEnd( &transform->ctx_inflate );
10318#endif
10319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10321 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010322
Hanno Becker92231322018-01-03 15:32:51 +000010323#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 mbedtls_md_free( &transform->md_ctx_enc );
10325 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010326#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010327
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010328 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010329}
10330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331#if defined(MBEDTLS_X509_CRT_PARSE_C)
10332static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010335
10336 while( cur != NULL )
10337 {
10338 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010340 cur = next;
10341 }
10342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010344
Hanno Becker0271f962018-08-16 13:23:47 +010010345#if defined(MBEDTLS_SSL_PROTO_DTLS)
10346
10347static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10348{
10349 unsigned offset;
10350 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10351
10352 if( hs == NULL )
10353 return;
10354
Hanno Becker283f5ef2018-08-24 09:34:47 +010010355 ssl_free_buffered_record( ssl );
10356
Hanno Becker0271f962018-08-16 13:23:47 +010010357 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010358 ssl_buffering_free_slot( ssl, offset );
10359}
10360
10361static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10362 uint8_t slot )
10363{
10364 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10365 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010366
10367 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10368 return;
10369
Hanno Beckere605b192018-08-21 15:59:07 +010010370 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010371 {
Hanno Beckere605b192018-08-21 15:59:07 +010010372 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010373 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010374 mbedtls_free( hs_buf->data );
10375 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010376 }
10377}
10378
10379#endif /* MBEDTLS_SSL_PROTO_DTLS */
10380
Gilles Peskine9b562d52018-04-25 20:32:43 +020010381void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010382{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010383 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10384
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010385 if( handshake == NULL )
10386 return;
10387
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010388#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10389 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10390 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010391 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010392 handshake->async_in_progress = 0;
10393 }
10394#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10395
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010396#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10397 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10398 mbedtls_md5_free( &handshake->fin_md5 );
10399 mbedtls_sha1_free( &handshake->fin_sha1 );
10400#endif
10401#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10402#if defined(MBEDTLS_SHA256_C)
10403 mbedtls_sha256_free( &handshake->fin_sha256 );
10404#endif
10405#if defined(MBEDTLS_SHA512_C)
10406 mbedtls_sha512_free( &handshake->fin_sha512 );
10407#endif
10408#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410#if defined(MBEDTLS_DHM_C)
10411 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010412#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413#if defined(MBEDTLS_ECDH_C)
10414 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010415#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010416#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010417 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010418#if defined(MBEDTLS_SSL_CLI_C)
10419 mbedtls_free( handshake->ecjpake_cache );
10420 handshake->ecjpake_cache = NULL;
10421 handshake->ecjpake_cache_len = 0;
10422#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010423#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010424
Janos Follath4ae5c292016-02-10 11:27:43 +000010425#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10426 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010427 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010429#endif
10430
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010431#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10432 if( handshake->psk != NULL )
10433 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010434 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010435 mbedtls_free( handshake->psk );
10436 }
10437#endif
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10440 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010441 /*
10442 * Free only the linked list wrapper, not the keys themselves
10443 * since the belong to the SNI callback
10444 */
10445 if( handshake->sni_key_cert != NULL )
10446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010448
10449 while( cur != NULL )
10450 {
10451 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010453 cur = next;
10454 }
10455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010457
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010458#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010459 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Beckere4aeb762019-02-05 17:19:52 +000010460 if( handshake->ecrs_peer_cert != NULL )
10461 {
10462 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10463 mbedtls_free( handshake->ecrs_peer_cert );
10464 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010465#endif
10466
Hanno Becker3bf8cdf2019-02-06 16:18:31 +000010467#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10468 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10469 mbedtls_pk_free( &handshake->peer_pubkey );
10470#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472#if defined(MBEDTLS_SSL_PROTO_DTLS)
10473 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010474 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010475 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010476#endif
10477
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010478 mbedtls_platform_zeroize( handshake,
10479 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010480}
10481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010483{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010484 if( session == NULL )
10485 return;
10486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010488 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010489#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010490
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010491#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010493#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010494
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010495 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010496}
10497
Paul Bakker5121ce52009-01-03 21:22:43 +000010498/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010499 * Serialize a full SSL context
10500 */
10501int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10502 unsigned char *buf,
10503 size_t buf_len,
10504 size_t *olen )
10505{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010506 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010507 (void) ssl;
10508
10509 if( buf != NULL )
10510 memset( buf, 0, buf_len );
10511
10512 *olen = 0;
10513
10514 return( 0 );
10515}
10516
10517/*
10518 * Deserialize a full SSL context
10519 */
10520int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10521 const unsigned char *buf,
10522 size_t len )
10523{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010524 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010525 (void) ssl;
10526 (void) buf;
10527 (void) len;
10528
10529 return( 0 );
10530}
10531
10532/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010533 * Free an SSL context
10534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010535void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010536{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010537 if( ssl == NULL )
10538 return;
10539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010541
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010542 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010543 {
Angus Grattond8213d02016-05-25 20:56:48 +100010544 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010545 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010546 }
10547
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010548 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010549 {
Angus Grattond8213d02016-05-25 20:56:48 +100010550 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010551 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010552 }
10553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010555 if( ssl->compress_buf != NULL )
10556 {
Angus Grattond8213d02016-05-25 20:56:48 +100010557 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010558 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010559 }
10560#endif
10561
Paul Bakker48916f92012-09-16 19:57:18 +000010562 if( ssl->transform )
10563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010564 mbedtls_ssl_transform_free( ssl->transform );
10565 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010566 }
10567
10568 if( ssl->handshake )
10569 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010570 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10572 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 mbedtls_free( ssl->handshake );
10575 mbedtls_free( ssl->transform_negotiate );
10576 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010577 }
10578
Paul Bakkerc0463502013-02-14 11:19:38 +010010579 if( ssl->session )
10580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581 mbedtls_ssl_session_free( ssl->session );
10582 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010583 }
10584
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010585#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010586 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010587 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010588 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010589 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010590 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010591#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010593#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10594 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10597 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010598 }
10599#endif
10600
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010601#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010602 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010603#endif
10604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010606
Paul Bakker86f04f42013-02-14 11:20:09 +010010607 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010608 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010609}
10610
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611/*
10612 * Initialze mbedtls_ssl_config
10613 */
10614void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10615{
10616 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010617
10618#if !defined(MBEDTLS_SSL_PROTO_TLS)
10619 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10620#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010621}
10622
Simon Butcherc97b6972015-12-27 23:48:17 +000010623#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010624static int ssl_preset_default_hashes[] = {
10625#if defined(MBEDTLS_SHA512_C)
10626 MBEDTLS_MD_SHA512,
10627 MBEDTLS_MD_SHA384,
10628#endif
10629#if defined(MBEDTLS_SHA256_C)
10630 MBEDTLS_MD_SHA256,
10631 MBEDTLS_MD_SHA224,
10632#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010633#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010634 MBEDTLS_MD_SHA1,
10635#endif
10636 MBEDTLS_MD_NONE
10637};
Simon Butcherc97b6972015-12-27 23:48:17 +000010638#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010639
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010640static int ssl_preset_suiteb_ciphersuites[] = {
10641 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10642 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10643 0
10644};
10645
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010646#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010647static int ssl_preset_suiteb_hashes[] = {
10648 MBEDTLS_MD_SHA256,
10649 MBEDTLS_MD_SHA384,
10650 MBEDTLS_MD_NONE
10651};
10652#endif
10653
10654#if defined(MBEDTLS_ECP_C)
10655static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10656 MBEDTLS_ECP_DP_SECP256R1,
10657 MBEDTLS_ECP_DP_SECP384R1,
10658 MBEDTLS_ECP_DP_NONE
10659};
10660#endif
10661
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010662/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010663 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010664 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010665int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010666 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010667{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010668#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010669 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010670#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010671
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010672 /* Use the functions here so that they are covered in tests,
10673 * but otherwise access member directly for efficiency */
10674 mbedtls_ssl_conf_endpoint( conf, endpoint );
10675 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010676
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010677 /*
10678 * Things that are common to all presets
10679 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010680#if defined(MBEDTLS_SSL_CLI_C)
10681 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10682 {
10683 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10684#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10685 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10686#endif
10687 }
10688#endif
10689
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010690#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010691 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010692#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010693
10694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10695 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10696#endif
10697
10698#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10699 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010700 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010701 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010702#endif
10703
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010704#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10705 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10706#endif
10707
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010708#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010709 conf->f_cookie_write = ssl_cookie_write_dummy;
10710 conf->f_cookie_check = ssl_cookie_check_dummy;
10711#endif
10712
10713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10714 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10715#endif
10716
Janos Follath088ce432017-04-10 12:42:31 +010010717#if defined(MBEDTLS_SSL_SRV_C)
10718 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10719#endif
10720
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721#if defined(MBEDTLS_SSL_PROTO_DTLS)
10722 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10723 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10724#endif
10725
10726#if defined(MBEDTLS_SSL_RENEGOTIATION)
10727 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010728 memset( conf->renego_period, 0x00, 2 );
10729 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730#endif
10731
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010732#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10733 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10734 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010735 const unsigned char dhm_p[] =
10736 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10737 const unsigned char dhm_g[] =
10738 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10739
Hanno Beckera90658f2017-10-04 15:29:08 +010010740 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10741 dhm_p, sizeof( dhm_p ),
10742 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010743 {
10744 return( ret );
10745 }
10746 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010747#endif
10748
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010749 /*
10750 * Preset-specific defaults
10751 */
10752 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010753 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010754 /*
10755 * NSA Suite B
10756 */
10757 case MBEDTLS_SSL_PRESET_SUITEB:
10758 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10759 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10760 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10761 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10762
10763 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10764 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10765 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10766 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10767 ssl_preset_suiteb_ciphersuites;
10768
10769#if defined(MBEDTLS_X509_CRT_PARSE_C)
10770 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010771#endif
10772
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010773#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010774 conf->sig_hashes = ssl_preset_suiteb_hashes;
10775#endif
10776
10777#if defined(MBEDTLS_ECP_C)
10778 conf->curve_list = ssl_preset_suiteb_curves;
10779#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010780 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010781
10782 /*
10783 * Default
10784 */
10785 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010786 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10787 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10788 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10789 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10790 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10791 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10792 MBEDTLS_SSL_MIN_MINOR_VERSION :
10793 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010794 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10795 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10796
10797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010798 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010799 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10800#endif
10801
10802 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10803 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10804 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10805 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10806 mbedtls_ssl_list_ciphersuites();
10807
10808#if defined(MBEDTLS_X509_CRT_PARSE_C)
10809 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10810#endif
10811
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010812#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010813 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010814#endif
10815
10816#if defined(MBEDTLS_ECP_C)
10817 conf->curve_list = mbedtls_ecp_grp_id_list();
10818#endif
10819
10820#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10821 conf->dhm_min_bitlen = 1024;
10822#endif
10823 }
10824
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010825 return( 0 );
10826}
10827
10828/*
10829 * Free mbedtls_ssl_config
10830 */
10831void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10832{
10833#if defined(MBEDTLS_DHM_C)
10834 mbedtls_mpi_free( &conf->dhm_P );
10835 mbedtls_mpi_free( &conf->dhm_G );
10836#endif
10837
10838#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10839 if( conf->psk != NULL )
10840 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010841 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010842 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010843 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010844 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010845 }
10846
10847 if( conf->psk_identity != NULL )
10848 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010849 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010850 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010851 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010852 conf->psk_identity_len = 0;
10853 }
10854#endif
10855
10856#if defined(MBEDTLS_X509_CRT_PARSE_C)
10857 ssl_key_cert_free( conf->key_cert );
10858#endif
10859
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010860 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010861}
10862
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010863#if defined(MBEDTLS_PK_C) && \
10864 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010865/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#if defined(MBEDTLS_RSA_C)
10871 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10872 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874#if defined(MBEDTLS_ECDSA_C)
10875 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10876 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010877#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010878 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010879}
10880
Hanno Becker7e5437a2017-04-28 17:15:26 +010010881unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10882{
10883 switch( type ) {
10884 case MBEDTLS_PK_RSA:
10885 return( MBEDTLS_SSL_SIG_RSA );
10886 case MBEDTLS_PK_ECDSA:
10887 case MBEDTLS_PK_ECKEY:
10888 return( MBEDTLS_SSL_SIG_ECDSA );
10889 default:
10890 return( MBEDTLS_SSL_SIG_ANON );
10891 }
10892}
10893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010894mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010895{
10896 switch( sig )
10897 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010898#if defined(MBEDTLS_RSA_C)
10899 case MBEDTLS_SSL_SIG_RSA:
10900 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010902#if defined(MBEDTLS_ECDSA_C)
10903 case MBEDTLS_SSL_SIG_ECDSA:
10904 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010905#endif
10906 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010908 }
10909}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010910#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010911
Hanno Becker7e5437a2017-04-28 17:15:26 +010010912#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10913 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10914
10915/* Find an entry in a signature-hash set matching a given hash algorithm. */
10916mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10917 mbedtls_pk_type_t sig_alg )
10918{
10919 switch( sig_alg )
10920 {
10921 case MBEDTLS_PK_RSA:
10922 return( set->rsa );
10923 case MBEDTLS_PK_ECDSA:
10924 return( set->ecdsa );
10925 default:
10926 return( MBEDTLS_MD_NONE );
10927 }
10928}
10929
10930/* Add a signature-hash-pair to a signature-hash set */
10931void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10932 mbedtls_pk_type_t sig_alg,
10933 mbedtls_md_type_t md_alg )
10934{
10935 switch( sig_alg )
10936 {
10937 case MBEDTLS_PK_RSA:
10938 if( set->rsa == MBEDTLS_MD_NONE )
10939 set->rsa = md_alg;
10940 break;
10941
10942 case MBEDTLS_PK_ECDSA:
10943 if( set->ecdsa == MBEDTLS_MD_NONE )
10944 set->ecdsa = md_alg;
10945 break;
10946
10947 default:
10948 break;
10949 }
10950}
10951
10952/* Allow exactly one hash algorithm for each signature. */
10953void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10954 mbedtls_md_type_t md_alg )
10955{
10956 set->rsa = md_alg;
10957 set->ecdsa = md_alg;
10958}
10959
10960#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10961 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10962
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010963/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010964 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010965 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010967{
10968 switch( hash )
10969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970#if defined(MBEDTLS_MD5_C)
10971 case MBEDTLS_SSL_HASH_MD5:
10972 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010973#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974#if defined(MBEDTLS_SHA1_C)
10975 case MBEDTLS_SSL_HASH_SHA1:
10976 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010978#if defined(MBEDTLS_SHA256_C)
10979 case MBEDTLS_SSL_HASH_SHA224:
10980 return( MBEDTLS_MD_SHA224 );
10981 case MBEDTLS_SSL_HASH_SHA256:
10982 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984#if defined(MBEDTLS_SHA512_C)
10985 case MBEDTLS_SSL_HASH_SHA384:
10986 return( MBEDTLS_MD_SHA384 );
10987 case MBEDTLS_SSL_HASH_SHA512:
10988 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010989#endif
10990 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010992 }
10993}
10994
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010995/*
10996 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10997 */
10998unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10999{
11000 switch( md )
11001 {
11002#if defined(MBEDTLS_MD5_C)
11003 case MBEDTLS_MD_MD5:
11004 return( MBEDTLS_SSL_HASH_MD5 );
11005#endif
11006#if defined(MBEDTLS_SHA1_C)
11007 case MBEDTLS_MD_SHA1:
11008 return( MBEDTLS_SSL_HASH_SHA1 );
11009#endif
11010#if defined(MBEDTLS_SHA256_C)
11011 case MBEDTLS_MD_SHA224:
11012 return( MBEDTLS_SSL_HASH_SHA224 );
11013 case MBEDTLS_MD_SHA256:
11014 return( MBEDTLS_SSL_HASH_SHA256 );
11015#endif
11016#if defined(MBEDTLS_SHA512_C)
11017 case MBEDTLS_MD_SHA384:
11018 return( MBEDTLS_SSL_HASH_SHA384 );
11019 case MBEDTLS_MD_SHA512:
11020 return( MBEDTLS_SSL_HASH_SHA512 );
11021#endif
11022 default:
11023 return( MBEDTLS_SSL_HASH_NONE );
11024 }
11025}
11026
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011027#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011028/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011029 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011030 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011031 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011032int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011035
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011036 if( ssl->conf->curve_list == NULL )
11037 return( -1 );
11038
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011039 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011040 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011041 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011042
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011043 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011044}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011045#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011046
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011047#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011048/*
11049 * Check if a hash proposed by the peer is in our list.
11050 * Return 0 if we're willing to use it, -1 otherwise.
11051 */
11052int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11053 mbedtls_md_type_t md )
11054{
11055 const int *cur;
11056
11057 if( ssl->conf->sig_hashes == NULL )
11058 return( -1 );
11059
11060 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11061 if( *cur == (int) md )
11062 return( 0 );
11063
11064 return( -1 );
11065}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011066#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068#if defined(MBEDTLS_X509_CRT_PARSE_C)
11069int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11070 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011071 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011072 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011073{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011074 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011075#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011076 int usage = 0;
11077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011079 const char *ext_oid;
11080 size_t ext_len;
11081#endif
11082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11084 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011085 ((void) cert);
11086 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011087 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011088#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011090#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11091 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011092 {
11093 /* Server part of the key exchange */
11094 switch( ciphersuite->key_exchange )
11095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011096 case MBEDTLS_KEY_EXCHANGE_RSA:
11097 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011098 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011099 break;
11100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011101 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11102 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11103 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11104 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011105 break;
11106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011107 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11108 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011109 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011110 break;
11111
11112 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011113 case MBEDTLS_KEY_EXCHANGE_NONE:
11114 case MBEDTLS_KEY_EXCHANGE_PSK:
11115 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11116 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011117 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011118 usage = 0;
11119 }
11120 }
11121 else
11122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011123 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11124 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011125 }
11126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011127 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011128 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011129 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011130 ret = -1;
11131 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011132#else
11133 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011134#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011136#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11137 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011139 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11140 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011141 }
11142 else
11143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011144 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11145 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011146 }
11147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011149 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011150 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011151 ret = -1;
11152 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011154
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011155 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011156}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011157#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011158
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011159/*
11160 * Convert version numbers to/from wire format
11161 * and, for DTLS, to/from TLS equivalent.
11162 *
11163 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011164 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011165 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11166 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011168void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011169 unsigned char ver[2] )
11170{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011171#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11172 ((void) transport);
11173#endif
11174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011176 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011178 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011179 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11180
11181 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11182 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11183 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011184 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011185#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011186#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011187 {
11188 ver[0] = (unsigned char) major;
11189 ver[1] = (unsigned char) minor;
11190 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011191#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011192}
11193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011195 const unsigned char ver[2] )
11196{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011197#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
11198 ((void) transport);
11199#endif
11200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011202 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011203 {
11204 *major = 255 - ver[0] + 2;
11205 *minor = 255 - ver[1] + 1;
11206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011208 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11209 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011210 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020011211#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020011212#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011213 {
11214 *major = ver[0];
11215 *minor = ver[1];
11216 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020011217#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011218}
11219
Simon Butcher99000142016-10-13 17:21:01 +010011220int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11221{
11222#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11223 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11224 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11225
11226 switch( md )
11227 {
11228#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11229#if defined(MBEDTLS_MD5_C)
11230 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011231 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011232#endif
11233#if defined(MBEDTLS_SHA1_C)
11234 case MBEDTLS_SSL_HASH_SHA1:
11235 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11236 break;
11237#endif
11238#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11239#if defined(MBEDTLS_SHA512_C)
11240 case MBEDTLS_SSL_HASH_SHA384:
11241 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11242 break;
11243#endif
11244#if defined(MBEDTLS_SHA256_C)
11245 case MBEDTLS_SSL_HASH_SHA256:
11246 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11247 break;
11248#endif
11249 default:
11250 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11251 }
11252
11253 return 0;
11254#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11255 (void) ssl;
11256 (void) md;
11257
11258 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11260}
11261
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011262#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11263 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11264int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11265 unsigned char *output,
11266 unsigned char *data, size_t data_len )
11267{
11268 int ret = 0;
11269 mbedtls_md5_context mbedtls_md5;
11270 mbedtls_sha1_context mbedtls_sha1;
11271
11272 mbedtls_md5_init( &mbedtls_md5 );
11273 mbedtls_sha1_init( &mbedtls_sha1 );
11274
11275 /*
11276 * digitally-signed struct {
11277 * opaque md5_hash[16];
11278 * opaque sha_hash[20];
11279 * };
11280 *
11281 * md5_hash
11282 * MD5(ClientHello.random + ServerHello.random
11283 * + ServerParams);
11284 * sha_hash
11285 * SHA(ClientHello.random + ServerHello.random
11286 * + ServerParams);
11287 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011288 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011289 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011291 goto exit;
11292 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011293 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011294 ssl->handshake->randbytes, 64 ) ) != 0 )
11295 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011296 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011297 goto exit;
11298 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011299 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011300 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011301 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011302 goto exit;
11303 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011304 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011305 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011306 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011307 goto exit;
11308 }
11309
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011310 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011311 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011313 goto exit;
11314 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011315 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011316 ssl->handshake->randbytes, 64 ) ) != 0 )
11317 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_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_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011322 data_len ) ) != 0 )
11323 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_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_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011328 output + 16 ) ) != 0 )
11329 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 goto exit;
11332 }
11333
11334exit:
11335 mbedtls_md5_free( &mbedtls_md5 );
11336 mbedtls_sha1_free( &mbedtls_sha1 );
11337
11338 if( ret != 0 )
11339 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11340 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11341
11342 return( ret );
11343
11344}
11345#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11346 MBEDTLS_SSL_PROTO_TLS1_1 */
11347
11348#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11349 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11350int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011351 unsigned char *hash, size_t *hashlen,
11352 unsigned char *data, size_t data_len,
11353 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354{
11355 int ret = 0;
11356 mbedtls_md_context_t ctx;
11357 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011358 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011359
11360 mbedtls_md_init( &ctx );
11361
11362 /*
11363 * digitally-signed struct {
11364 * opaque client_random[32];
11365 * opaque server_random[32];
11366 * ServerDHParams params;
11367 * };
11368 */
11369 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11370 {
11371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11372 goto exit;
11373 }
11374 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11375 {
11376 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11377 goto exit;
11378 }
11379 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11380 {
11381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11382 goto exit;
11383 }
11384 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11385 {
11386 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11387 goto exit;
11388 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011389 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011390 {
11391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11392 goto exit;
11393 }
11394
11395exit:
11396 mbedtls_md_free( &ctx );
11397
11398 if( ret != 0 )
11399 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11400 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11401
11402 return( ret );
11403}
11404#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11405 MBEDTLS_SSL_PROTO_TLS1_2 */
11406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011407#endif /* MBEDTLS_SSL_TLS_C */