blob: 104ed2250b7b2cfa6623c80e122485224c34189e [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
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200371#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200373{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ssl_session_free( dst );
375 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200378 if( src->peer_cert != NULL )
379 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200380 int ret;
381
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200382 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200384 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200389 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392 dst->peer_cert = NULL;
393 return( ret );
394 }
395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200398#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399 if( src->ticket != NULL )
400 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200401 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200402 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
405 memcpy( dst->ticket, src->ticket, src->ticket_len );
406 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200407#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408
409 return( 0 );
410}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +0200411#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
414int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200415 const unsigned char *key_enc, const unsigned char *key_dec,
416 size_t keylen,
417 const unsigned char *iv_enc, const unsigned char *iv_dec,
418 size_t ivlen,
419 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200420 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
422int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
423int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
424int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
425int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
426#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000427
Paul Bakker5121ce52009-01-03 21:22:43 +0000428/*
429 * Key material generation
430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200432static int ssl3_prf( const unsigned char *secret, size_t slen,
433 const char *label,
434 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char *dstbuf, size_t dlen )
436{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100437 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000438 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_context md5;
440 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000441 unsigned char padding[16];
442 unsigned char sha1sum[20];
443 ((void)label);
444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200445 mbedtls_md5_init( &md5 );
446 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200447
Paul Bakker5f70b252012-09-13 14:23:06 +0000448 /*
449 * SSLv3:
450 * block =
451 * MD5( secret + SHA1( 'A' + secret + random ) ) +
452 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
453 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
454 * ...
455 */
456 for( i = 0; i < dlen / 16; i++ )
457 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200458 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000459
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100464 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100465 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100466 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100467 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100468 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100469 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000470
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100473 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100474 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 }
480
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100481exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200482 mbedtls_md5_free( &md5 );
483 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000484
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500485 mbedtls_platform_zeroize( padding, sizeof( padding ) );
486 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000487
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200493static int tls1_prf( const unsigned char *secret, size_t slen,
494 const char *label,
495 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000496 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000497{
Paul Bakker23986e52011-04-24 08:57:21 +0000498 size_t nb, hs;
499 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200500 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 unsigned char tmp[128];
502 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 const mbedtls_md_info_t *md_info;
504 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100505 int ret;
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100528 return( ret );
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
531 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
532 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534 for( i = 0; i < dlen; i += 16 )
535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_md_hmac_reset ( &md_ctx );
537 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
538 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_hmac_reset ( &md_ctx );
541 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
542 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
545
546 for( j = 0; j < k; j++ )
547 dstbuf[i + j] = h_i[j];
548 }
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100551
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 /*
553 * XOR out with P_sha1(secret,label+random)[0..dlen]
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559 return( ret );
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
562 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
563 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565 for( i = 0; i < dlen; i += 20 )
566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_md_hmac_reset ( &md_ctx );
568 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
569 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_reset ( &md_ctx );
572 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
576
577 for( j = 0; j < k; j++ )
578 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
579 }
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100582
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500583 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
584 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 return( 0 );
587}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
591static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100592 const unsigned char *secret, size_t slen,
593 const char *label,
594 const unsigned char *random, size_t rlen,
595 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000596{
597 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100598 size_t i, j, k, md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000599 unsigned char tmp[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
601 const mbedtls_md_info_t *md_info;
602 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100603 int ret;
604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100611
612 if( sizeof( tmp ) < md_len + strlen( label ) + rlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000614
615 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100616 memcpy( tmp + md_len, label, nb );
617 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000618 nb += rlen;
619
620 /*
621 * Compute P_<hash>(secret, label + random)[0..dlen]
622 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100624 return( ret );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
627 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
628 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100629
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100630 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_md_hmac_reset ( &md_ctx );
633 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
634 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_md_hmac_reset ( &md_ctx );
637 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
638 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000639
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100640 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000641
642 for( j = 0; j < k; j++ )
643 dstbuf[i + j] = h_i[j];
644 }
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100647
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500648 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
649 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000650
651 return( 0 );
652}
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100655static int tls_prf_sha256( const unsigned char *secret, size_t slen,
656 const char *label,
657 const unsigned char *random, size_t rlen,
658 unsigned char *dstbuf, size_t dlen )
659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100661 label, random, rlen, dstbuf, dlen ) );
662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200666static int tls_prf_sha384( const unsigned char *secret, size_t slen,
667 const char *label,
668 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000669 unsigned char *dstbuf, size_t dlen )
670{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100672 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674#endif /* MBEDTLS_SHA512_C */
675#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
680 defined(MBEDTLS_SSL_PROTO_TLS1_1)
681static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200682#endif
Paul Bakker380da532012-04-18 16:10:25 +0000683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200685static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200690static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200692#endif
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
695#if defined(MBEDTLS_SHA256_C)
696static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200697static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_SHA512_C)
702static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +0200703static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100705#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000707
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200708/* Type for the TLS PRF */
709typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
710 const unsigned char *, size_t,
711 unsigned char *, size_t);
712
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200713/*
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200714 * Populate a transform structure with session keys and all the other
715 * necessary information.
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200716 *
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200717 * Parameters:
718 * - [in/out]: transform: structure to populate
719 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200720 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200721 * - [in] ciphersuite
722 * - [in] master
723 * - [in] encrypt_then_mac
724 * - [in] trunc_hmac
725 * - [in] compression
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200726 * - [in] tls_prf: pointer to PRF to use for key derivation
727 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200728 * - [in] minor_ver: SSL/TLS minor version
729 * - [in] endpoint: client or server
730 * - [in] ssl: optionally used for:
731 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
732 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
733 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +0200734 */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200735static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200736 int ciphersuite,
737 const unsigned char master[48],
738#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
739 int encrypt_then_mac,
740#endif
741#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
742 int trunc_hmac,
743#endif
744#if defined(MBEDTLS_ZLIB_SUPPORT)
745 int compression,
746#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200747 ssl_tls_prf_t tls_prf,
748 const unsigned char randbytes[64],
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200749 int minor_ver,
750 unsigned endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +0200751 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000752{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 unsigned char keyblk[256];
755 unsigned char *key1;
756 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100757 unsigned char *mac_enc;
758 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000759 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200760 size_t iv_copy_len;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000761 unsigned keylen;
Hanno Becker8759e162017-12-27 21:34:08 +0000762 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 const mbedtls_cipher_info_t *cipher_info;
764 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100765
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200766#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
767 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
768 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +0200769 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200770 (void) ssl;
Hanno Becker3307b532017-12-27 21:37:21 +0000771#endif
Hanno Becker3307b532017-12-27 21:37:21 +0000772
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200773 /* Copy info about negotiated version and extensions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200775 transform->encrypt_then_mac = encrypt_then_mac;
Paul Bakker5121ce52009-01-03 21:22:43 +0000776#endif
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200777 transform->minor_ver = minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +0000778
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200779 /*
780 * Get various info structures
781 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200782 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200783 if( ciphersuite_info == NULL )
784 {
785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200786 ciphersuite ) );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200787 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
788 }
789
Hanno Becker8759e162017-12-27 21:34:08 +0000790 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100791 if( cipher_info == NULL )
792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000794 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100796 }
797
Hanno Becker8759e162017-12-27 21:34:08 +0000798 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100799 if( md_info == NULL )
800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Becker8759e162017-12-27 21:34:08 +0000802 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100804 }
805
Hanno Beckera5a2b082019-05-15 14:03:01 +0100806#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100807 /* Copy own and peer's CID if the use of the CID
808 * extension has been negotiated. */
809 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
810 {
811 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Beckerd91dc372019-04-30 13:52:29 +0100812
Hanno Becker4932f9f2019-05-03 15:23:51 +0100813 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker4932f9f2019-05-03 15:23:51 +0100814 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker8013b272019-05-03 12:55:51 +0100815 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100816 transform->in_cid_len );
Hanno Beckere582d122019-05-15 10:21:55 +0100817
818 transform->out_cid_len = ssl->handshake->peer_cid_len;
819 memcpy( transform->out_cid, ssl->handshake->peer_cid,
820 ssl->handshake->peer_cid_len );
821 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
822 transform->out_cid_len );
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100823 }
Hanno Beckera5a2b082019-05-15 14:03:01 +0100824#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerdd0afca2019-04-26 16:22:27 +0100825
Paul Bakker5121ce52009-01-03 21:22:43 +0000826 /*
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200827 * Compute key block using the PRF
Paul Bakker1ef83d62012-04-11 12:09:53 +0000828 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200829 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100830 if( ret != 0 )
831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +0100833 return( ret );
834 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +0200837 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200838 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +0200839 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Paul Bakker5121ce52009-01-03 21:22:43 +0000842 /*
843 * Determine the appropriate key, IV and MAC length.
844 */
Paul Bakker68884e32013-01-07 18:20:04 +0100845
Hanno Beckere7f2df02017-12-27 08:17:40 +0000846 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200847
Hanno Beckerf1229442018-01-03 15:32:31 +0000848#if defined(MBEDTLS_GCM_C) || \
849 defined(MBEDTLS_CCM_C) || \
850 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200852 cipher_info->mode == MBEDTLS_MODE_CCM ||
853 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +0000854 {
Hanno Becker8759e162017-12-27 21:34:08 +0000855 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200856
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200857 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +0000858 mac_key_len = 0;
Hanno Becker8759e162017-12-27 21:34:08 +0000859 transform->taglen =
860 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200861
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200862 /* All modes haves 96-bit IVs;
863 * GCM and CCM has 4 implicit and 8 explicit bytes
864 * ChachaPoly has all 12 bytes implicit
865 */
Paul Bakker68884e32013-01-07 18:20:04 +0100866 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
868 transform->fixed_ivlen = 12;
869 else
870 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200871
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200872 /* Minimum length of encrypted record */
873 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Becker8759e162017-12-27 21:34:08 +0000874 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +0100875 }
876 else
Hanno Beckerf1229442018-01-03 15:32:31 +0000877#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
878#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
879 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
880 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +0100881 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200882 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
884 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200887 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100888 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000889
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200890 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +0000891 mac_key_len = mbedtls_md_get_size( md_info );
892 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200895 /*
896 * If HMAC is to be truncated, we shall keep the leftmost bytes,
897 * (rfc 6066 page 13 or rfc 2104 section 4),
898 * so we only need to adjust the length here.
899 */
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200900 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +0000901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +0000903
904#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
905 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +0000906 * HMAC implementation which also truncates the key
907 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +0000908 mac_key_len = transform->maclen;
909#endif
910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200912
913 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100914 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200916 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200918 transform->minlen = transform->maclen;
919 else
Paul Bakker68884e32013-01-07 18:20:04 +0100920 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200921 /*
922 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100923 * 1. if EtM is in use: one block plus MAC
924 * otherwise: * first multiple of blocklen greater than maclen
925 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +0200928 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100929 {
930 transform->minlen = transform->maclen
931 + cipher_info->block_size;
932 }
933 else
934#endif
935 {
936 transform->minlen = transform->maclen
937 + cipher_info->block_size
938 - transform->maclen % cipher_info->block_size;
939 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200942 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
943 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200944 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100945 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200948 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
949 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200950 {
951 transform->minlen += transform->ivlen;
952 }
953 else
954#endif
955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200958 }
Paul Bakker68884e32013-01-07 18:20:04 +0100959 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 }
Hanno Beckerf1229442018-01-03 15:32:31 +0000961 else
962#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
966 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Hanno Beckere7f2df02017-12-27 08:17:40 +0000968 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
969 (unsigned) keylen,
970 (unsigned) transform->minlen,
971 (unsigned) transform->ivlen,
972 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000973
974 /*
975 * Finally setup the cipher contexts, IVs and MAC secrets.
976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200978 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 {
Hanno Becker81c7b182017-11-09 18:39:33 +0000980 key1 = keyblk + mac_key_len * 2;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000981 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Paul Bakker68884e32013-01-07 18:20:04 +0100983 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +0000984 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000986 /*
987 * This is not used in TLS v1.1.
988 */
Paul Bakker48916f92012-09-16 19:57:18 +0000989 iv_copy_len = ( transform->fixed_ivlen ) ?
990 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +0000991 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
992 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000994 }
995 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#endif /* MBEDTLS_SSL_CLI_C */
997#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +0200998 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 {
Hanno Beckere7f2df02017-12-27 08:17:40 +00001000 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001001 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001002
Hanno Becker81c7b182017-11-09 18:39:33 +00001003 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001004 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001005
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001006 /*
1007 * This is not used in TLS v1.1.
1008 */
Paul Bakker48916f92012-09-16 19:57:18 +00001009 iv_copy_len = ( transform->fixed_ivlen ) ?
1010 transform->fixed_ivlen : transform->ivlen;
Hanno Beckere7f2df02017-12-27 08:17:40 +00001011 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1012 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001014 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001015 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001020 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Hanno Becker92231322018-01-03 15:32:51 +00001022#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001024 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001025 {
Hanno Becker92231322018-01-03 15:32:51 +00001026 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1029 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001030 }
1031
Hanno Becker81c7b182017-11-09 18:39:33 +00001032 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1033 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001034 }
1035 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001039 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001041 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1042 For AEAD-based ciphersuites, there is nothing to do here. */
1043 if( mac_key_len != 0 )
1044 {
1045 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1046 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1047 }
Paul Bakker68884e32013-01-07 18:20:04 +01001048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049 else
1050#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001054 }
Hanno Becker92231322018-01-03 15:32:51 +00001055#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1058 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001059 {
1060 int ret = 0;
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001063
Hanno Beckere7f2df02017-12-27 08:17:40 +00001064 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001065 transform->iv_enc, transform->iv_dec,
1066 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001067 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001068 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
1071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001072 }
1073 }
Hanno Becker92231322018-01-03 15:32:51 +00001074#else
1075 ((void) mac_dec);
1076 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001078
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001079#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1080 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001081 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001082 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001083 master, keyblk,
Hanno Beckere7f2df02017-12-27 08:17:40 +00001084 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001085 iv_copy_len );
1086 }
1087#endif
1088
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001089 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001090 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001093 return( ret );
1094 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001095
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001096 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001097 cipher_info ) ) != 0 )
1098 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001100 return( ret );
1101 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001104 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001108 return( ret );
1109 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001112 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", 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 defined(MBEDTLS_CIPHER_MODE_CBC)
1120 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1123 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001126 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001127 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1130 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001133 return( ret );
1134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001138 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001140 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001142 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001145
Paul Bakker48916f92012-09-16 19:57:18 +00001146 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1147 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001148
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001149 if( deflateInit( &transform->ctx_deflate,
1150 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001151 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
1154 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001155 }
1156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001158
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 return( 0 );
1160}
1161
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001162/*
Manuel Pégourié-Gonnard42c814f2019-05-20 10:10:17 +02001163 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001164 *
1165 * Inputs:
1166 * - SSL/TLS minor version
1167 * - hash associated with the ciphersuite (only used by TLS 1.2)
1168 *
Manuel Pégourié-Gonnardcf312162019-05-10 10:25:00 +02001169 * Outputs:
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001170 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1171 */
1172static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1173 int minor_ver,
1174 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001175{
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001176#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1177 (void) hash;
1178#endif
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001179
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001180#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001181 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001182 {
1183 handshake->tls_prf = ssl3_prf;
1184 handshake->calc_verify = ssl_calc_verify_ssl;
1185 handshake->calc_finished = ssl_calc_finished_ssl;
1186 }
1187 else
1188#endif
1189#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001190 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001191 {
1192 handshake->tls_prf = tls1_prf;
1193 handshake->calc_verify = ssl_calc_verify_tls;
1194 handshake->calc_finished = ssl_calc_finished_tls;
1195 }
1196 else
1197#endif
1198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1199#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001200 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1201 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001202 {
1203 handshake->tls_prf = tls_prf_sha384;
1204 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1205 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1206 }
1207 else
1208#endif
1209#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001210 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001211 {
1212 handshake->tls_prf = tls_prf_sha256;
1213 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1214 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1215 }
1216 else
1217#endif
1218#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1219 {
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1221 }
1222
1223 return( 0 );
1224}
1225
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001226/*
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001227 * Compute master secret if needed
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001228 *
1229 * Parameters:
1230 * [in/out] handshake
1231 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
1232 * [out] premaster (cleared)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001233 * [out] master
1234 * [in] ssl: optionally used for debugging and calc_verify
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001235 */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001236static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001237 unsigned char *master,
Manuel Pégourié-Gonnarded3b7a92019-05-03 09:58:33 +02001238 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001239{
1240 int ret;
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001241
1242#if !defined(MBEDTLS_DEBUG_C) && !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001243 ssl = NULL; /* make sure we don't use it except for debug and EMS */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001244 (void) ssl;
1245#endif
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001246
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001247 if( handshake->resume != 0 )
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001248 {
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1250 return( 0 );
1251 }
1252
1253 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
1254 handshake->pmslen );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001255
1256#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001257 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001258 {
1259 unsigned char session_hash[48];
1260 size_t hash_len;
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001261
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001262 handshake->calc_verify( ssl, session_hash, &hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001263
Manuel Pégourié-Gonnard9c5bcc92019-05-20 12:09:50 +02001264 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1265 session_hash, hash_len );
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001266
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001267 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001268 "extended master secret",
1269 session_hash, hash_len,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001270 master, 48 );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001271 }
1272 else
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001273#endif
Manuel Pégourié-Gonnardbcf258e2019-05-03 11:46:27 +02001274 {
1275 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1276 "master secret",
1277 handshake->randbytes, 64,
1278 master, 48 );
1279 }
Manuel Pégourié-Gonnarddafe5222019-05-03 09:16:16 +02001280 if( ret != 0 )
1281 {
1282 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1283 return( ret );
1284 }
1285
1286 mbedtls_platform_zeroize( handshake->premaster,
1287 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001288
1289 return( 0 );
1290}
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001291
Manuel Pégourié-Gonnard5ed5e902019-04-30 11:41:40 +02001292int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1293{
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001294 int ret;
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001295 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1296 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001297
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1299
1300 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001301 ret = ssl_set_handshake_prfs( ssl->handshake,
1302 ssl->minor_ver,
1303 ciphersuite_info->mac );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001304 if( ret != 0 )
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001305 {
1306 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001307 return( ret );
Manuel Pégourié-Gonnardaa3c7012019-04-30 12:08:59 +02001308 }
Manuel Pégourié-Gonnard52aa5202019-04-30 11:54:22 +02001309
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001310 /* Compute master secret if needed */
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001311 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardc28c8892019-05-03 09:46:14 +02001312 ssl->session_negotiate->master,
1313 ssl );
Manuel Pégourié-Gonnard7edd5872019-05-03 09:05:41 +02001314 if( ret != 0 )
1315 {
1316 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1317 return( ret );
1318 }
1319
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001320 /* Swap the client and server random values:
1321 * - MS derivation wanted client+server (RFC 5246 8.1)
1322 * - key derivation wants server+client (RFC 5246 6.3) */
1323 {
1324 unsigned char tmp[64];
1325 memcpy( tmp, ssl->handshake->randbytes, 64 );
1326 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1327 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1328 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1329 }
1330
1331 /* Populate transform structure */
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001332 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard84ef8bd2019-05-10 10:50:04 +02001333 ssl->session_negotiate->ciphersuite,
1334 ssl->session_negotiate->master,
1335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1336 ssl->session_negotiate->encrypt_then_mac,
1337#endif
1338#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1339 ssl->session_negotiate->trunc_hmac,
1340#endif
1341#if defined(MBEDTLS_ZLIB_SUPPORT)
1342 ssl->session_negotiate->compression,
1343#endif
Manuel Pégourié-Gonnard0bcfbc32019-05-06 13:32:17 +02001344 ssl->handshake->tls_prf,
1345 ssl->handshake->randbytes,
Manuel Pégourié-Gonnard1d10a982019-05-06 13:48:22 +02001346 ssl->minor_ver,
1347 ssl->conf->endpoint,
Manuel Pégourié-Gonnard12a3f442019-05-06 12:55:40 +02001348 ssl );
Manuel Pégourié-Gonnard707728d2019-05-06 12:05:58 +02001349 if( ret != 0 )
1350 {
1351 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1352 return( ret );
1353 }
1354
1355 /* We no longer need Server/ClientHello.random values */
1356 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1357 sizeof( ssl->handshake->randbytes ) );
1358
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001359 /* Allocate compression buffer */
1360#if defined(MBEDTLS_ZLIB_SUPPORT)
1361 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1362 ssl->compress_buf == NULL )
1363 {
1364 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1365 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1366 if( ssl->compress_buf == NULL )
1367 {
1368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnard762d0112019-05-20 10:27:20 +02001369 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnarda1abb262019-05-06 12:44:24 +02001370 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1371 }
1372 }
1373#endif
1374
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1376
1377 return( 0 );
1378}
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001381void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1382 unsigned char hash[36],
1383 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001384{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001385 mbedtls_md5_context md5;
1386 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 unsigned char pad_1[48];
1388 unsigned char pad_2[48];
1389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001392 mbedtls_md5_init( &md5 );
1393 mbedtls_sha1_init( &sha1 );
1394
1395 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1396 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001397
Paul Bakker380da532012-04-18 16:10:25 +00001398 memset( pad_1, 0x36, 48 );
1399 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001401 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1402 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1403 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001405 mbedtls_md5_starts_ret( &md5 );
1406 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1407 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1408 mbedtls_md5_update_ret( &md5, hash, 16 );
1409 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001411 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1412 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1413 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001414
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001415 mbedtls_sha1_starts_ret( &sha1 );
1416 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1417 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1418 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1419 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001420
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001421 *hlen = 36;
1422
1423 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001425
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001426 mbedtls_md5_free( &md5 );
1427 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001428
Paul Bakker380da532012-04-18 16:10:25 +00001429 return;
1430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001434void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1435 unsigned char hash[36],
1436 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001437{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001438 mbedtls_md5_context md5;
1439 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001442
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001443 mbedtls_md5_init( &md5 );
1444 mbedtls_sha1_init( &sha1 );
1445
1446 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1447 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001448
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001449 mbedtls_md5_finish_ret( &md5, hash );
1450 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001451
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001452 *hlen = 36;
1453
1454 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001456
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001457 mbedtls_md5_free( &md5 );
1458 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001459
Paul Bakker380da532012-04-18 16:10:25 +00001460 return;
1461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1465#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001466void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1467 unsigned char hash[32],
1468 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001469{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001470 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001471
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001472 mbedtls_sha256_init( &sha256 );
1473
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001475
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001476 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001477 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001478
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001479 *hlen = 32;
1480
1481 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001483
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001484 mbedtls_sha256_free( &sha256 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001485
Paul Bakker380da532012-04-18 16:10:25 +00001486 return;
1487}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001491void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1492 unsigned char hash[48],
1493 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001494{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001495 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001497 mbedtls_sha512_init( &sha512 );
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001501 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001502 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Manuel Pégourié-Gonnarda5759752019-05-03 11:43:28 +02001504 *hlen = 48;
1505
1506 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001508
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001509 mbedtls_sha512_free( &sha512 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001510
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 return;
1512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#endif /* MBEDTLS_SHA512_C */
1514#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1517int 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 +02001518{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001519 unsigned char *p = ssl->handshake->premaster;
1520 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001521 const unsigned char *psk = ssl->conf->psk;
1522 size_t psk_len = ssl->conf->psk_len;
1523
1524 /* If the psk callback was called, use its result */
1525 if( ssl->handshake->psk != NULL )
1526 {
1527 psk = ssl->handshake->psk;
1528 psk_len = ssl->handshake->psk_len;
1529 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001530
1531 /*
1532 * PMS = struct {
1533 * opaque other_secret<0..2^16-1>;
1534 * opaque psk<0..2^16-1>;
1535 * };
1536 * with "other_secret" depending on the particular key exchange
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1539 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001540 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001541 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001543
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001544 *(p++) = (unsigned char)( psk_len >> 8 );
1545 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001546
1547 if( end < p || (size_t)( end - p ) < psk_len )
1548 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1549
1550 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001551 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001552 }
1553 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1555#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1556 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001557 {
1558 /*
1559 * other_secret already set by the ClientKeyExchange message,
1560 * and is 48 bytes long
1561 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001562 if( end - p < 2 )
1563 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1564
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001565 *p++ = 0;
1566 *p++ = 48;
1567 p += 48;
1568 }
1569 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1571#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1572 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001573 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001574 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001575 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001576
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001577 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001579 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001580 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001583 return( ret );
1584 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001585 *(p++) = (unsigned char)( len >> 8 );
1586 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001587 p += len;
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001590 }
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1593#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1594 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001595 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001596 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001597 size_t zlen;
1598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001600 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001601 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001604 return( ret );
1605 }
1606
1607 *(p++) = (unsigned char)( zlen >> 8 );
1608 *(p++) = (unsigned char)( zlen );
1609 p += zlen;
1610
Janos Follath3fbdada2018-08-15 10:26:53 +01001611 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1612 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001613 }
1614 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1618 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001619 }
1620
1621 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001622 if( end - p < 2 )
1623 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001624
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001625 *(p++) = (unsigned char)( psk_len >> 8 );
1626 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001627
1628 if( end < p || (size_t)( end - p ) < psk_len )
1629 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1630
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001631 memcpy( p, psk, psk_len );
1632 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001633
1634 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1635
1636 return( 0 );
1637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001641/*
1642 * SSLv3.0 MAC functions
1643 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001644#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001645static void ssl_mac( mbedtls_md_context_t *md_ctx,
1646 const unsigned char *secret,
1647 const unsigned char *buf, size_t len,
1648 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001649 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
1651 unsigned char header[11];
1652 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001653 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1655 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001656
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001657 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001659 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001660 else
Paul Bakker68884e32013-01-07 18:20:04 +01001661 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663 memcpy( header, ctr, 8 );
1664 header[ 8] = (unsigned char) type;
1665 header[ 9] = (unsigned char)( len >> 8 );
1666 header[10] = (unsigned char)( len );
1667
Paul Bakker68884e32013-01-07 18:20:04 +01001668 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_md_starts( md_ctx );
1670 mbedtls_md_update( md_ctx, secret, md_size );
1671 mbedtls_md_update( md_ctx, padding, padlen );
1672 mbedtls_md_update( md_ctx, header, 11 );
1673 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001674 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Paul Bakker68884e32013-01-07 18:20:04 +01001676 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_md_starts( md_ctx );
1678 mbedtls_md_update( md_ctx, secret, md_size );
1679 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001680 mbedtls_md_update( md_ctx, out, md_size );
1681 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001684
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001685/* The function below is only used in the Lucky 13 counter-measure in
Hanno Becker30d02cd2018-10-18 15:43:13 +01001686 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001687#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001688 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1689 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1690 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1691/* This function makes sure every byte in the memory region is accessed
1692 * (in ascending addresses order) */
1693static void ssl_read_memory( unsigned char *p, size_t len )
1694{
1695 unsigned char acc = 0;
1696 volatile unsigned char force;
1697
1698 for( ; len != 0; p++, len-- )
1699 acc ^= *p;
1700
1701 force = acc;
1702 (void) force;
1703}
1704#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1705
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001706/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001708 */
Hanno Becker3307b532017-12-27 21:37:21 +00001709
Hanno Beckera5a2b082019-05-15 14:03:01 +01001710#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker89693692019-05-20 15:06:12 +01001711/* This functions transforms a DTLS plaintext fragment and a record content
1712 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker92c930f2019-04-29 17:31:37 +01001713 *
1714 * struct {
1715 * opaque content[DTLSPlaintext.length];
1716 * ContentType real_type;
1717 * uint8 zeros[length_of_padding];
1718 * } DTLSInnerPlaintext;
1719 *
1720 * Input:
1721 * - `content`: The beginning of the buffer holding the
1722 * plaintext to be wrapped.
1723 * - `*content_size`: The length of the plaintext in Bytes.
1724 * - `max_len`: The number of Bytes available starting from
1725 * `content`. This must be `>= *content_size`.
1726 * - `rec_type`: The desired record content type.
1727 *
1728 * Output:
1729 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1730 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1731 *
1732 * Returns:
1733 * - `0` on success.
1734 * - A negative error code if `max_len` didn't offer enough space
1735 * for the expansion.
1736 */
1737static int ssl_cid_build_inner_plaintext( unsigned char *content,
1738 size_t *content_size,
1739 size_t remaining,
1740 uint8_t rec_type )
1741{
1742 size_t len = *content_size;
Hanno Becker78426092019-05-13 15:31:17 +01001743 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
1744 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
1745 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker92c930f2019-04-29 17:31:37 +01001746
1747 /* Write real content type */
1748 if( remaining == 0 )
1749 return( -1 );
1750 content[ len ] = rec_type;
1751 len++;
1752 remaining--;
1753
1754 if( remaining < pad )
1755 return( -1 );
1756 memset( content + len, 0, pad );
1757 len += pad;
1758 remaining -= pad;
1759
1760 *content_size = len;
1761 return( 0 );
1762}
1763
Hanno Becker7dc25772019-05-20 15:08:01 +01001764/* This function parses a DTLSInnerPlaintext structure.
1765 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker92c930f2019-04-29 17:31:37 +01001766static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
1767 size_t *content_size,
1768 uint8_t *rec_type )
1769{
1770 size_t remaining = *content_size;
1771
1772 /* Determine length of padding by skipping zeroes from the back. */
1773 do
1774 {
1775 if( remaining == 0 )
1776 return( -1 );
1777 remaining--;
1778 } while( content[ remaining ] == 0 );
1779
1780 *content_size = remaining;
1781 *rec_type = content[ remaining ];
1782
1783 return( 0 );
1784}
Hanno Beckera5a2b082019-05-15 14:03:01 +01001785#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01001786
Hanno Becker99abf512019-05-20 14:50:53 +01001787/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckeracadb0a2019-05-08 18:15:21 +01001788 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker3307b532017-12-27 21:37:21 +00001789static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001790 size_t *add_data_len,
Hanno Becker3307b532017-12-27 21:37:21 +00001791 mbedtls_record *rec )
1792{
Hanno Becker99abf512019-05-20 14:50:53 +01001793 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckere83efe62019-04-29 13:52:53 +01001794 *
1795 * additional_data = seq_num + TLSCompressed.type +
1796 * TLSCompressed.version + TLSCompressed.length;
1797 *
Hanno Becker99abf512019-05-20 14:50:53 +01001798 * For the CID extension, this is extended as follows
1799 * (quoting draft-ietf-tls-dtls-connection-id-05,
1800 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckere83efe62019-04-29 13:52:53 +01001801 *
1802 * additional_data = seq_num + DTLSPlaintext.type +
1803 * DTLSPlaintext.version +
Hanno Becker99abf512019-05-20 14:50:53 +01001804 * cid +
1805 * cid_length +
Hanno Beckere83efe62019-04-29 13:52:53 +01001806 * length_of_DTLSInnerPlaintext;
1807 */
1808
Hanno Becker3307b532017-12-27 21:37:21 +00001809 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
1810 add_data[8] = rec->type;
Hanno Becker24ce1eb2019-05-20 15:01:46 +01001811 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckere83efe62019-04-29 13:52:53 +01001812
Hanno Beckera5a2b082019-05-15 14:03:01 +01001813#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker1f02f052019-05-09 11:38:24 +01001814 if( rec->cid_len != 0 )
1815 {
1816 memcpy( add_data + 11, rec->cid, rec->cid_len );
1817 add_data[11 + rec->cid_len + 0] = rec->cid_len;
1818 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
1819 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
1820 *add_data_len = 13 + 1 + rec->cid_len;
1821 }
1822 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01001823#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1f02f052019-05-09 11:38:24 +01001824 {
1825 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
1826 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
1827 *add_data_len = 13;
1828 }
Hanno Becker3307b532017-12-27 21:37:21 +00001829}
1830
Hanno Becker611a83b2018-01-03 14:27:32 +00001831int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
1832 mbedtls_ssl_transform *transform,
1833 mbedtls_record *rec,
1834 int (*f_rng)(void *, unsigned char *, size_t),
1835 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001838 int auth_done = 0;
Hanno Becker3307b532017-12-27 21:37:21 +00001839 unsigned char * data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01001840 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01001841 size_t add_data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00001842 size_t post_avail;
1843
1844 /* The SSL context is only used for debugging purposes! */
Hanno Becker611a83b2018-01-03 14:27:32 +00001845#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02001846 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker3307b532017-12-27 21:37:21 +00001847 ((void) ssl);
1848#endif
1849
1850 /* The PRNG is used for dynamic IV generation that's used
1851 * for CBC transformations in TLS 1.1 and TLS 1.2. */
1852#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
1853 ( defined(MBEDTLS_AES_C) || \
1854 defined(MBEDTLS_ARIA_C) || \
1855 defined(MBEDTLS_CAMELLIA_C) ) && \
1856 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
1857 ((void) f_rng);
1858 ((void) p_rng);
1859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001862
Hanno Becker3307b532017-12-27 21:37:21 +00001863 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001864 {
Hanno Becker3307b532017-12-27 21:37:21 +00001865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
1866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1867 }
Hanno Becker505089d2019-05-01 09:45:57 +01001868 if( rec == NULL
1869 || rec->buf == NULL
1870 || rec->buf_len < rec->data_offset
1871 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera5a2b082019-05-15 14:03:01 +01001872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01001873 || rec->cid_len != 0
1874#endif
1875 )
Hanno Becker3307b532017-12-27 21:37:21 +00001876 {
1877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001879 }
1880
Hanno Becker3307b532017-12-27 21:37:21 +00001881 data = rec->buf + rec->data_offset;
Hanno Becker92c930f2019-04-29 17:31:37 +01001882 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker3307b532017-12-27 21:37:21 +00001884 data, rec->data_len );
1885
1886 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
1887
1888 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
1889 {
1890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
1891 (unsigned) rec->data_len,
1892 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
1893 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1894 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001895
Hanno Beckera5a2b082019-05-15 14:03:01 +01001896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01001897 /*
1898 * Add CID information
1899 */
1900 rec->cid_len = transform->out_cid_len;
1901 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
1902 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker92c930f2019-04-29 17:31:37 +01001903
1904 if( rec->cid_len != 0 )
1905 {
1906 /*
Hanno Becker7dc25772019-05-20 15:08:01 +01001907 * Wrap plaintext into DTLSInnerPlaintext structure.
1908 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker92c930f2019-04-29 17:31:37 +01001909 *
Hanno Becker7dc25772019-05-20 15:08:01 +01001910 * Note that this changes `rec->data_len`, and hence
1911 * `post_avail` needs to be recalculated afterwards.
Hanno Becker92c930f2019-04-29 17:31:37 +01001912 */
1913 if( ssl_cid_build_inner_plaintext( data,
1914 &rec->data_len,
1915 post_avail,
1916 rec->type ) != 0 )
1917 {
1918 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1919 }
1920
1921 rec->type = MBEDTLS_SSL_MSG_CID;
1922 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01001923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01001924
Hanno Becker92c930f2019-04-29 17:31:37 +01001925 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001928 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00001930#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 if( mode == MBEDTLS_MODE_STREAM ||
1932 ( mode == MBEDTLS_MODE_CBC
1933#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3307b532017-12-27 21:37:21 +00001934 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001935#endif
1936 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 {
Hanno Becker3307b532017-12-27 21:37:21 +00001938 if( post_avail < transform->maclen )
1939 {
1940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker3307b532017-12-27 21:37:21 +00001945 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001946 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001947 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker3307b532017-12-27 21:37:21 +00001948 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
1949 data, rec->data_len, rec->ctr, rec->type, mac );
1950 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001951 }
1952 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001953#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1955 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker3307b532017-12-27 21:37:21 +00001956 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001957 {
Hanno Becker992b6872017-11-09 18:57:39 +00001958 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1959
Hanno Beckere83efe62019-04-29 13:52:53 +01001960 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00001961
Hanno Becker3307b532017-12-27 21:37:21 +00001962 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01001963 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00001964 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1965 data, rec->data_len );
1966 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1967 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1968
1969 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001970 }
1971 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001972#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001976 }
1977
Hanno Becker3307b532017-12-27 21:37:21 +00001978 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
1979 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001980
Hanno Becker3307b532017-12-27 21:37:21 +00001981 rec->data_len += transform->maclen;
1982 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001983 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001984 }
Hanno Becker5cc04d52018-01-03 15:24:20 +00001985#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001987 /*
1988 * Encrypt
1989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001990#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1991 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001993 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00001994 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker3307b532017-12-27 21:37:21 +00001996 "including %d bytes of padding",
1997 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Hanno Becker3307b532017-12-27 21:37:21 +00001999 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2000 transform->iv_enc, transform->ivlen,
2001 data, rec->data_len,
2002 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002005 return( ret );
2006 }
2007
Hanno Becker3307b532017-12-27 21:37:21 +00002008 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
Paul Bakker68884e32013-01-07 18:20:04 +01002014 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002015#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002017#if defined(MBEDTLS_GCM_C) || \
2018 defined(MBEDTLS_CCM_C) || \
2019 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002021 mode == MBEDTLS_MODE_CCM ||
2022 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002023 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002024 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002025 unsigned char iv[12];
Hanno Becker3307b532017-12-27 21:37:21 +00002026 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002027
Hanno Becker3307b532017-12-27 21:37:21 +00002028 /* Check that there's space for both the authentication tag
2029 * and the explicit IV before and after the record content. */
2030 if( post_avail < transform->taglen ||
2031 rec->data_offset < explicit_iv_len )
2032 {
2033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2034 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2035 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002036
Paul Bakker68884e32013-01-07 18:20:04 +01002037 /*
2038 * Generate IV
2039 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002040 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2041 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002042 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002043 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker3307b532017-12-27 21:37:21 +00002044 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2045 explicit_iv_len );
2046 /* Prefix record content with explicit IV. */
2047 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002048 }
2049 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2050 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002051 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002052 unsigned char i;
2053
2054 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2055
2056 for( i = 0; i < 8; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002057 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002058 }
2059 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002060 {
2061 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2063 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002064 }
2065
Hanno Beckere83efe62019-04-29 13:52:53 +01002066 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker08885812019-04-26 13:34:37 +01002067
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002068 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2069 iv, transform->ivlen );
2070 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker3307b532017-12-27 21:37:21 +00002071 data - explicit_iv_len, explicit_iv_len );
2072 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002073 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002075 "including 0 bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002076 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002077
Paul Bakker68884e32013-01-07 18:20:04 +01002078 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002079 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002080 */
Hanno Becker3307b532017-12-27 21:37:21 +00002081
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002082 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker3307b532017-12-27 21:37:21 +00002083 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002084 add_data, add_data_len, /* add data */
Hanno Becker3307b532017-12-27 21:37:21 +00002085 data, rec->data_len, /* source */
2086 data, &rec->data_len, /* destination */
2087 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002090 return( ret );
2091 }
2092
Hanno Becker3307b532017-12-27 21:37:21 +00002093 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2094 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002095
Hanno Becker3307b532017-12-27 21:37:21 +00002096 rec->data_len += transform->taglen + explicit_iv_len;
2097 rec->data_offset -= explicit_iv_len;
2098 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002099 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002102#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2103#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002104 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002107 int ret;
Hanno Becker3307b532017-12-27 21:37:21 +00002108 size_t padlen, i;
2109 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002110
Hanno Becker3307b532017-12-27 21:37:21 +00002111 /* Currently we're always using minimal padding
2112 * (up to 255 bytes would be allowed). */
2113 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2114 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 padlen = 0;
2116
Hanno Becker3307b532017-12-27 21:37:21 +00002117 /* Check there's enough space in the buffer for the padding. */
2118 if( post_avail < padlen + 1 )
2119 {
2120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2121 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2122 }
2123
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 for( i = 0; i <= padlen; i++ )
Hanno Becker3307b532017-12-27 21:37:21 +00002125 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002126
Hanno Becker3307b532017-12-27 21:37:21 +00002127 rec->data_len += padlen + 1;
2128 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002131 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002132 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2133 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002134 */
Hanno Becker3307b532017-12-27 21:37:21 +00002135 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002136 {
Hanno Becker3307b532017-12-27 21:37:21 +00002137 if( f_rng == NULL )
2138 {
2139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2141 }
2142
2143 if( rec->data_offset < transform->ivlen )
2144 {
2145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2146 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2147 }
2148
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002149 /*
2150 * Generate IV
2151 */
Hanno Becker3307b532017-12-27 21:37:21 +00002152 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002153 if( ret != 0 )
2154 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002155
Hanno Becker3307b532017-12-27 21:37:21 +00002156 memcpy( data - transform->ivlen, transform->iv_enc,
2157 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002158
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002163 "including %d bytes of IV and %d bytes of padding",
Hanno Becker3307b532017-12-27 21:37:21 +00002164 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002165 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002166
Hanno Becker3307b532017-12-27 21:37:21 +00002167 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2168 transform->iv_enc,
2169 transform->ivlen,
2170 data, rec->data_len,
2171 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002174 return( ret );
2175 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002176
Hanno Becker3307b532017-12-27 21:37:21 +00002177 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002181 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker3307b532017-12-27 21:37:21 +00002184 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002185 {
2186 /*
2187 * Save IV in SSL3 and TLS1
2188 */
Hanno Becker3307b532017-12-27 21:37:21 +00002189 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2190 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191 }
Hanno Becker3307b532017-12-27 21:37:21 +00002192 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002193#endif
Hanno Becker3307b532017-12-27 21:37:21 +00002194 {
2195 data -= transform->ivlen;
2196 rec->data_offset -= transform->ivlen;
2197 rec->data_len += transform->ivlen;
2198 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002201 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002202 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002203 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2204
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002205 /*
2206 * MAC(MAC_write_key, seq_num +
2207 * TLSCipherText.type +
2208 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002209 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002210 * IV + // except for TLS 1.0
2211 * ENC(content + padding + padding_length));
2212 */
Hanno Becker3307b532017-12-27 21:37:21 +00002213
2214 if( post_avail < transform->maclen)
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2218 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002219
Hanno Beckere83efe62019-04-29 13:52:53 +01002220 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker3307b532017-12-27 21:37:21 +00002223 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002224 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002225
Hanno Becker3307b532017-12-27 21:37:21 +00002226 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckere83efe62019-04-29 13:52:53 +01002227 add_data_len );
Hanno Becker3307b532017-12-27 21:37:21 +00002228 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2229 data, rec->data_len );
2230 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2231 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002232
Hanno Becker3307b532017-12-27 21:37:21 +00002233 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002234
Hanno Becker3307b532017-12-27 21:37:21 +00002235 rec->data_len += transform->maclen;
2236 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002237 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002238 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002241 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002243 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002248
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002249 /* Make extra sure authentication was performed, exactly once */
2250 if( auth_done != 1 )
2251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002254 }
2255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
2258 return( 0 );
2259}
2260
Hanno Becker611a83b2018-01-03 14:27:32 +00002261int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2262 mbedtls_ssl_transform *transform,
2263 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264{
Hanno Becker4c6876b2017-12-27 21:28:58 +00002265 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266 mbedtls_cipher_mode_t mode;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002267 int ret, auth_done = 0;
Hanno Becker5cc04d52018-01-03 15:24:20 +00002268#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002269 size_t padlen = 0, correct = 1;
2270#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002271 unsigned char* data;
Hanno Becker28a0c4e2019-05-20 14:54:26 +01002272 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckere83efe62019-04-29 13:52:53 +01002273 size_t add_data_len;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002274
Hanno Becker611a83b2018-01-03 14:27:32 +00002275#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnard86e48c22019-05-07 10:17:56 +02002276 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002277 ((void) ssl);
2278#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002281 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002282 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2284 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2285 }
2286 if( rec == NULL ||
2287 rec->buf == NULL ||
2288 rec->buf_len < rec->data_offset ||
2289 rec->buf_len - rec->data_offset < rec->data_len )
2290 {
2291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002293 }
2294
Hanno Becker4c6876b2017-12-27 21:28:58 +00002295 data = rec->buf + rec->data_offset;
2296 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002297
Hanno Beckera5a2b082019-05-15 14:03:01 +01002298#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere83efe62019-04-29 13:52:53 +01002299 /*
2300 * Match record's CID with incoming CID.
2301 */
Hanno Beckerabd7c892019-05-08 13:02:22 +01002302 if( rec->cid_len != transform->in_cid_len ||
2303 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2304 {
Hanno Beckere8eff9a2019-05-14 11:30:10 +01002305 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Beckerabd7c892019-05-08 13:02:22 +01002306 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01002308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2310 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002311 {
2312 padlen = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002313 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2314 transform->iv_dec,
2315 transform->ivlen,
2316 data, rec->data_len,
2317 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002320 return( ret );
2321 }
2322
Hanno Becker4c6876b2017-12-27 21:28:58 +00002323 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2326 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
Paul Bakker68884e32013-01-07 18:20:04 +01002329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331#if defined(MBEDTLS_GCM_C) || \
2332 defined(MBEDTLS_CCM_C) || \
2333 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 mode == MBEDTLS_MODE_CCM ||
2336 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002337 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002340
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002341 /*
2342 * Compute and update sizes
2343 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002344 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002347 "+ taglen (%d)", rec->data_len,
2348 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002350 }
Paul Bakker68884e32013-01-07 18:20:04 +01002351
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002352 /*
2353 * Prepare IV
2354 */
2355 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2356 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002357 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002359 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002360
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 }
2362 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2363 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002364 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 unsigned char i;
2366
2367 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2368
2369 for( i = 0; i < 8; i++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002370 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371 }
2372 else
2373 {
2374 /* Reminder if we ever add an AEAD mode with a different size */
2375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2377 }
2378
Hanno Becker4c6876b2017-12-27 21:28:58 +00002379 data += explicit_iv_len;
2380 rec->data_offset += explicit_iv_len;
2381 rec->data_len -= explicit_iv_len + transform->taglen;
2382
Hanno Beckere83efe62019-04-29 13:52:53 +01002383 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002384 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckere83efe62019-04-29 13:52:53 +01002385 add_data, add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002386
2387 memcpy( transform->iv_dec + transform->fixed_ivlen,
2388 data - explicit_iv_len, explicit_iv_len );
2389
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002390 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002391 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Becker8759e162017-12-27 21:34:08 +00002392 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002393
Paul Bakker68884e32013-01-07 18:20:04 +01002394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002397 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002398 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2399 iv, transform->ivlen,
Hanno Beckere83efe62019-04-29 13:52:53 +01002400 add_data, add_data_len,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002401 data, rec->data_len,
2402 data, &olen,
2403 data + rec->data_len,
2404 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002411 return( ret );
2412 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002413 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002414
Hanno Becker4c6876b2017-12-27 21:28:58 +00002415 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002422#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2423#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002424 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002427 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 /*
Paul Bakker45829992013-01-03 14:52:21 +01002430 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002433 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2434 {
2435 /* The ciphertext is prefixed with the CBC IV. */
2436 minlen += transform->ivlen;
2437 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#endif
Paul Bakker45829992013-01-03 14:52:21 +01002439
Hanno Becker4c6876b2017-12-27 21:28:58 +00002440 /* Size considerations:
2441 *
2442 * - The CBC cipher text must not be empty and hence
2443 * at least of size transform->ivlen.
2444 *
2445 * Together with the potential IV-prefix, this explains
2446 * the first of the two checks below.
2447 *
2448 * - The record must contain a MAC, either in plain or
2449 * encrypted, depending on whether Encrypt-then-MAC
2450 * is used or not.
2451 * - If it is, the message contains the IV-prefix,
2452 * the CBC ciphertext, and the MAC.
2453 * - If it is not, the padded plaintext, and hence
2454 * the CBC ciphertext, has at least length maclen + 1
2455 * because there is at least the padding length byte.
2456 *
2457 * As the CBC ciphertext is not empty, both cases give the
2458 * lower bound minlen + maclen + 1 on the record size, which
2459 * we test for in the second check below.
2460 */
2461 if( rec->data_len < minlen + transform->ivlen ||
2462 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002465 "+ 1 ) ( + expl IV )", rec->data_len,
2466 transform->ivlen,
2467 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002469 }
2470
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002471 /*
2472 * Authenticate before decrypt if enabled
2473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002475 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002476 {
Hanno Becker992b6872017-11-09 18:57:39 +00002477 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480
Hanno Becker4c6876b2017-12-27 21:28:58 +00002481 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2482 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483
Hanno Beckere83efe62019-04-29 13:52:53 +01002484 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002485
Hanno Beckere83efe62019-04-29 13:52:53 +01002486 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2487 add_data_len );
2488 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2489 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002490 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2491 data, rec->data_len );
2492 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2493 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002494
Hanno Becker4c6876b2017-12-27 21:28:58 +00002495 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2496 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002497 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker4c6876b2017-12-27 21:28:58 +00002498 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002499
Hanno Becker4c6876b2017-12-27 21:28:58 +00002500 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2501 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002509
2510 /*
2511 * Check length sanity
2512 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002513 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002516 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002518 }
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002521 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002522 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002523 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002524 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002525 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002526 /* This is safe because data_len >= minlen + maclen + 1 initially,
2527 * and at this point we have at most subtracted maclen (note that
2528 * minlen == transform->ivlen here). */
2529 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002530
Hanno Becker4c6876b2017-12-27 21:28:58 +00002531 data += transform->ivlen;
2532 rec->data_offset += transform->ivlen;
2533 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002536
Hanno Becker4c6876b2017-12-27 21:28:58 +00002537 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2538 transform->iv_dec, transform->ivlen,
2539 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002542 return( ret );
2543 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002544
Hanno Becker4c6876b2017-12-27 21:28:58 +00002545 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002552 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002553 {
2554 /*
2555 * Save IV in SSL3 and TLS1
2556 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002557 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2558 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Hanno Becker4c6876b2017-12-27 21:28:58 +00002562 /* Safe since data_len >= minlen + maclen + 1, so after having
2563 * subtracted at most minlen and maclen up to this point,
2564 * data_len > 0. */
2565 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002566
Hanno Becker4c6876b2017-12-27 21:28:58 +00002567 if( auth_done == 1 )
2568 {
2569 correct *= ( rec->data_len >= padlen + 1 );
2570 padlen *= ( rec->data_len >= padlen + 1 );
2571 }
2572 else
Paul Bakker45829992013-01-03 14:52:21 +01002573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002575 if( rec->data_len < transform->maclen + padlen + 1 )
2576 {
2577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2578 rec->data_len,
2579 transform->maclen,
2580 padlen + 1 ) );
2581 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002582#endif
Hanno Becker4c6876b2017-12-27 21:28:58 +00002583
2584 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2585 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Becker4c6876b2017-12-27 21:28:58 +00002588 padlen++;
2589
2590 /* Regardless of the validity of the padding,
2591 * we have data_len >= padlen here. */
2592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002594 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002596 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_SSL_DEBUG_ALL)
2599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker4c6876b2017-12-27 21:28:58 +00002600 "should be no more than %d",
2601 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002602#endif
Paul Bakker45829992013-01-03 14:52:21 +01002603 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605 }
2606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2608#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2609 defined(MBEDTLS_SSL_PROTO_TLS1_2)
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 /* The padding check involves a series of up to 256
2613 * consecutive memory reads at the end of the record
2614 * plaintext buffer. In order to hide the length and
2615 * validity of the padding, always perform exactly
2616 * `min(256,plaintext_len)` reads (but take into account
2617 * only the last `padlen` bytes for the padding check). */
2618 size_t pad_count = 0;
2619 size_t real_count = 0;
2620 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002621
Hanno Becker4c6876b2017-12-27 21:28:58 +00002622 /* Index of first padding byte; it has been ensured above
2623 * that the subtraction is safe. */
2624 size_t const padding_idx = rec->data_len - padlen;
2625 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2626 size_t const start_idx = rec->data_len - num_checks;
2627 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002628
Hanno Becker4c6876b2017-12-27 21:28:58 +00002629 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002630 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002631 real_count |= ( idx >= padding_idx );
2632 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002633 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00002634 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002637 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002639#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002640 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2644 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002648 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002649
Hanno Becker4c6876b2017-12-27 21:28:58 +00002650 /* If the padding was found to be invalid, padlen == 0
2651 * and the subtraction is safe. If the padding was found valid,
2652 * padlen hasn't been changed and the previous assertion
2653 * data_len >= padlen still holds. */
2654 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002656 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002658 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2661 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002664#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002665 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker4c6876b2017-12-27 21:28:58 +00002666 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002668
2669 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670 * Authenticate if not done yet.
2671 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 */
Hanno Becker5cc04d52018-01-03 15:24:20 +00002673#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002674 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002675 {
Hanno Becker992b6872017-11-09 18:57:39 +00002676 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002677
Hanno Becker4c6876b2017-12-27 21:28:58 +00002678 /* If the initial value of padlen was such that
2679 * data_len < maclen + padlen + 1, then padlen
2680 * got reset to 1, and the initial check
2681 * data_len >= minlen + maclen + 1
2682 * guarantees that at this point we still
2683 * have at least data_len >= maclen.
2684 *
2685 * If the initial value of padlen was such that
2686 * data_len >= maclen + padlen + 1, then we have
2687 * subtracted either padlen + 1 (if the padding was correct)
2688 * or 0 (if the padding was incorrect) since then,
2689 * hence data_len >= maclen in any case.
2690 */
2691 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Hanno Beckere83efe62019-04-29 13:52:53 +01002693 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002696 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002697 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00002698 ssl_mac( &transform->md_ctx_dec,
2699 transform->mac_dec,
2700 data, rec->data_len,
2701 rec->ctr, rec->type,
2702 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002703 }
2704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2706#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2707 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002708 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002709 {
2710 /*
2711 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002712 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002713 *
2714 * Known timing attacks:
2715 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2716 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002717 * To compensate for different timings for the MAC calculation
2718 * depending on how much padding was removed (which is determined
2719 * by padlen), process extra_run more blocks through the hash
2720 * function.
2721 *
2722 * The formula in the paper is
2723 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2724 * where L1 is the size of the header plus the decrypted message
2725 * plus CBC padding and L2 is the size of the header plus the
2726 * decrypted message. This is for an underlying hash function
2727 * with 64-byte blocks.
2728 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
2729 * correctly. We round down instead of up, so -56 is the correct
2730 * value for our calculations instead of -55.
2731 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02002732 * Repeat the formula rather than defining a block_size variable.
2733 * This avoids requiring division by a variable at runtime
2734 * (which would be marginally less efficient and would require
2735 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002736 */
2737 size_t j, extra_run = 0;
Hanno Becker4c6876b2017-12-27 21:28:58 +00002738 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002739
2740 /*
2741 * The next two sizes are the minimum and maximum values of
2742 * in_msglen over all padlen values.
2743 *
2744 * They're independent of padlen, since we previously did
2745 * in_msglen -= padlen.
2746 *
2747 * Note that max_len + maclen is never more than the buffer
2748 * length, as we previously did in_msglen -= maclen too.
2749 */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002750 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002751 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
2752
Hanno Becker4c6876b2017-12-27 21:28:58 +00002753 memset( tmp, 0, sizeof( tmp ) );
2754
2755 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02002756 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02002757#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
2758 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002759 case MBEDTLS_MD_MD5:
2760 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02002761 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02002762 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002763 extra_run =
2764 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
2765 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02002766 break;
2767#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02002768#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02002769 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02002770 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckere83efe62019-04-29 13:52:53 +01002771 extra_run =
2772 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
2773 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02002774 break;
2775#endif
2776 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02002777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02002778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2779 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01002780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002781 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002782
Hanno Beckere83efe62019-04-29 13:52:53 +01002783 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2784 add_data_len );
Hanno Becker4c6876b2017-12-27 21:28:58 +00002785 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
2786 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002787 /* Make sure we access everything even when padlen > 0. This
2788 * makes the synchronisation requirements for just-in-time
2789 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002790 ssl_read_memory( data + rec->data_len, padlen );
2791 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002792
2793 /* Call mbedtls_md_process at least once due to cache attacks
2794 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02002795 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker4c6876b2017-12-27 21:28:58 +00002796 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002797
Hanno Becker4c6876b2017-12-27 21:28:58 +00002798 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002799
2800 /* Make sure we access all the memory that could contain the MAC,
2801 * before we check it in the next code block. This makes the
2802 * synchronisation requirements for just-in-time Prime+Probe
2803 * attacks much tighter and hopefully impractical. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00002804 ssl_read_memory( data + min_len,
2805 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002806 }
2807 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2809 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002814
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002815#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker4c6876b2017-12-27 21:28:58 +00002816 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
2817 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002818#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002819
Hanno Becker4c6876b2017-12-27 21:28:58 +00002820 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2821 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823#if defined(MBEDTLS_SSL_DEBUG_ALL)
2824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002825#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002826 correct = 0;
2827 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002828 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002829 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002830
2831 /*
2832 * Finally check the correct flag
2833 */
2834 if( correct == 0 )
2835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker5cc04d52018-01-03 15:24:20 +00002836#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002837
2838 /* Make extra sure authentication was performed, exactly once */
2839 if( auth_done != 1 )
2840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Hanno Beckera5a2b082019-05-15 14:03:01 +01002845#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker92c930f2019-04-29 17:31:37 +01002846 if( rec->cid_len != 0 )
2847 {
2848 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
2849 &rec->type );
2850 if( ret != 0 )
2851 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2852 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01002853#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker92c930f2019-04-29 17:31:37 +01002854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856
2857 return( 0 );
2858}
2859
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002860#undef MAC_NONE
2861#undef MAC_PLAINTEXT
2862#undef MAC_CIPHERTEXT
2863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002864#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00002865/*
2866 * Compression/decompression functions
2867 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002869{
2870 int ret;
2871 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04002872 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002873 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002874 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002877
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002878 if( len_pre == 0 )
2879 return( 0 );
2880
Paul Bakker2770fbd2012-07-03 13:30:23 +00002881 memcpy( msg_pre, ssl->out_msg, len_pre );
2882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002884 ssl->out_msglen ) );
2885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002887 ssl->out_msg, ssl->out_msglen );
2888
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2890 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2891 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002892 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002893
Paul Bakker48916f92012-09-16 19:57:18 +00002894 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002895 if( ret != Z_OK )
2896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2898 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002899 }
2900
Angus Grattond8213d02016-05-25 20:56:48 +10002901 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04002902 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002905 ssl->out_msglen ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002908 ssl->out_msg, ssl->out_msglen );
2909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002911
2912 return( 0 );
2913}
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002916{
2917 int ret;
2918 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002919 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002920 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002921 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002924
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002925 if( len_pre == 0 )
2926 return( 0 );
2927
Paul Bakker2770fbd2012-07-03 13:30:23 +00002928 memcpy( msg_pre, ssl->in_msg, len_pre );
2929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002931 ssl->in_msglen ) );
2932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002934 ssl->in_msg, ssl->in_msglen );
2935
Paul Bakker48916f92012-09-16 19:57:18 +00002936 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2937 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2938 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10002939 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002940 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002941
Paul Bakker48916f92012-09-16 19:57:18 +00002942 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 if( ret != Z_OK )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2946 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002947 }
2948
Angus Grattond8213d02016-05-25 20:56:48 +10002949 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04002950 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002953 ssl->in_msglen ) );
2954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002955 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00002956 ssl->in_msg, ssl->in_msglen );
2957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002959
2960 return( 0 );
2961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
2965static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002967#if defined(MBEDTLS_SSL_PROTO_DTLS)
2968static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002969{
2970 /* If renegotiation is not enforced, retransmit until we would reach max
2971 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002972 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002973 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002974 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002975 unsigned char doublings = 1;
2976
2977 while( ratio != 0 )
2978 {
2979 ++doublings;
2980 ratio >>= 1;
2981 }
2982
2983 if( ++ssl->renego_records_seen > doublings )
2984 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002986 return( 0 );
2987 }
2988 }
2989
2990 return( ssl_write_hello_request( ssl ) );
2991}
2992#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002994
Paul Bakker5121ce52009-01-03 21:22:43 +00002995/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002996 * Fill the input message buffer by appending data to it.
2997 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002998 *
2999 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3000 * available (from this read and/or a previous one). Otherwise, an error code
3001 * is returned (possibly EOF or WANT_READ).
3002 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003003 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3004 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3005 * since we always read a whole datagram at once.
3006 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003007 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003008 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011{
Paul Bakker23986e52011-04-24 08:57:21 +00003012 int ret;
3013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003017 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003020 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003022 }
3023
Angus Grattond8213d02016-05-25 20:56:48 +10003024 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003028 }
3029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003031 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003033 uint32_t timeout;
3034
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003035 /* Just to be sure */
3036 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3037 {
3038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3039 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3041 }
3042
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003043 /*
3044 * The point is, we need to always read a full datagram at once, so we
3045 * sometimes read more then requested, and handle the additional data.
3046 * It could be the rest of the current record (while fetching the
3047 * header) and/or some other records in the same datagram.
3048 */
3049
3050 /*
3051 * Move to the next record in the already read datagram if applicable
3052 */
3053 if( ssl->next_record_offset != 0 )
3054 {
3055 if( ssl->in_left < ssl->next_record_offset )
3056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059 }
3060
3061 ssl->in_left -= ssl->next_record_offset;
3062
3063 if( ssl->in_left != 0 )
3064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003066 ssl->next_record_offset ) );
3067 memmove( ssl->in_hdr,
3068 ssl->in_hdr + ssl->next_record_offset,
3069 ssl->in_left );
3070 }
3071
3072 ssl->next_record_offset = 0;
3073 }
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003077
3078 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003079 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003080 */
3081 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003084 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003085 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003086
3087 /*
3088 * A record can't be split accross datagrams. If we need to read but
3089 * are not at the beginning of a new record, the caller did something
3090 * wrong.
3091 */
3092 if( ssl->in_left != 0 )
3093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3095 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003096 }
3097
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003098 /*
3099 * Don't even try to read if time's out already.
3100 * This avoids by-passing the timer when repeatedly receiving messages
3101 * that will end up being dropped.
3102 */
3103 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003104 {
3105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003106 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003107 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003108 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003109 {
Angus Grattond8213d02016-05-25 20:56:48 +10003110 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003112 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003113 timeout = ssl->handshake->retransmit_timeout;
3114 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003115 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003118
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003119 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003120 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3121 timeout );
3122 else
3123 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003126
3127 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003129 }
3130
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003131 if( ret == MBEDTLS_ERR_SSL_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( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003134 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003137 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003138 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003141 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003142 }
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003147 return( ret );
3148 }
3149
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003150 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003151 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003152#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003153 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003155 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003156 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003159 return( ret );
3160 }
3161
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003162 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003165 }
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ret < 0 )
3168 return( ret );
3169
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003170 ssl->in_left = ret;
3171 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003172 MBEDTLS_SSL_TRANSPORT_ELSE
3173#endif /* MBEDTLS_SSL_PROTO_DTLS */
3174#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003177 ssl->in_left, nb_want ) );
3178
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003179 while( ssl->in_left < nb_want )
3180 {
3181 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003182
3183 if( ssl_check_timer( ssl ) != 0 )
3184 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3185 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003186 {
3187 if( ssl->f_recv_timeout != NULL )
3188 {
3189 ret = ssl->f_recv_timeout( ssl->p_bio,
3190 ssl->in_hdr + ssl->in_left, len,
3191 ssl->conf->read_timeout );
3192 }
3193 else
3194 {
3195 ret = ssl->f_recv( ssl->p_bio,
3196 ssl->in_hdr + ssl->in_left, len );
3197 }
3198 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003201 ssl->in_left, nb_want ) );
3202 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003203
3204 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003206
3207 if( ret < 0 )
3208 return( ret );
3209
mohammad160352aecb92018-03-28 23:41:40 -07003210 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003211 {
Darryl Green11999bb2018-03-13 15:22:58 +00003212 MBEDTLS_SSL_DEBUG_MSG( 1,
3213 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003214 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3216 }
3217
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003218 ssl->in_left += ret;
3219 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003220 }
Manuel Pégourié-Gonnard25838b72019-03-19 10:23:56 +01003221#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
3225 return( 0 );
3226}
3227
3228/*
3229 * Flush any data not yet written
3230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003232{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003233 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003234 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003238 if( ssl->f_send == NULL )
3239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003241 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003243 }
3244
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003245 /* Avoid incrementing counter if data is flushed */
3246 if( ssl->out_left == 0 )
3247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003249 return( 0 );
3250 }
3251
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 while( ssl->out_left > 0 )
3253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker43395762019-05-03 14:46:38 +01003255 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256
Hanno Becker2b1e3542018-08-06 11:19:13 +01003257 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003258 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003261
3262 if( ret <= 0 )
3263 return( ret );
3264
mohammad160352aecb92018-03-28 23:41:40 -07003265 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003266 {
Darryl Green11999bb2018-03-13 15:22:58 +00003267 MBEDTLS_SSL_DEBUG_MSG( 1,
3268 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003269 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3271 }
3272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 ssl->out_left -= ret;
3274 }
3275
Hanno Becker2b1e3542018-08-06 11:19:13 +01003276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003277 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003278 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003279 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003280 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003281 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2b1e3542018-08-06 11:19:13 +01003282#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003283#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +01003284 {
3285 ssl->out_hdr = ssl->out_buf + 8;
3286 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02003287#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +01003288 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
3292 return( 0 );
3293}
3294
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003295/*
3296 * Functions to handle the DTLS retransmission state machine
3297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003299/*
3300 * Append current handshake message to current outgoing flight
3301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3306 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3307 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003308
3309 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003310 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003311 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003314 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003315 }
3316
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003317 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003318 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003321 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003322 }
3323
3324 /* Copy current handshake message with headers */
3325 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3326 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003327 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003328 msg->next = NULL;
3329
3330 /* Append to the current flight */
3331 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003332 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003333 else
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003336 while( cur->next != NULL )
3337 cur = cur->next;
3338 cur->next = msg;
3339 }
3340
Hanno Becker3b235902018-08-06 09:54:53 +01003341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003342 return( 0 );
3343}
3344
3345/*
3346 * Free the current flight of handshake messages
3347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003349{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_ssl_flight_item *cur = flight;
3351 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003352
3353 while( cur != NULL )
3354 {
3355 next = cur->next;
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 mbedtls_free( cur->p );
3358 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003359
3360 cur = next;
3361 }
3362}
3363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3365static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003366#endif
3367
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003368/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003369 * Swap transform_out and out_ctr with the alternative ones
3370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003371static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003374 unsigned char tmp_out_ctr[8];
3375
3376 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003379 return;
3380 }
3381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003383
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003384 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003385 tmp_transform = ssl->transform_out;
3386 ssl->transform_out = ssl->handshake->alt_transform_out;
3387 ssl->handshake->alt_transform_out = tmp_transform;
3388
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003389 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003390 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3391 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003392 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003393
3394 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003395 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3398 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3403 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003404 }
3405 }
3406#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003407}
3408
3409/*
3410 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003411 */
3412int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3413{
3414 int ret = 0;
3415
3416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3417
3418 ret = mbedtls_ssl_flight_transmit( ssl );
3419
3420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3421
3422 return( ret );
3423}
3424
3425/*
3426 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003427 *
3428 * Need to remember the current message in case flush_output returns
3429 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003430 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003431 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003432int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003433{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003434 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003438 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003440
3441 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003442 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003443 ssl_swap_epochs( ssl );
3444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003446 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003447
3448 while( ssl->handshake->cur_msg != NULL )
3449 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003450 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003451 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003452
Hanno Beckere1dcb032018-08-17 16:47:58 +01003453 int const is_finished =
3454 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3455 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3456
Hanno Becker04da1892018-08-14 13:22:10 +01003457 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3458 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3459
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003460 /* Swap epochs before sending Finished: we can't do it after
3461 * sending ChangeCipherSpec, in case write returns WANT_READ.
3462 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003463 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003464 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003466 ssl_swap_epochs( ssl );
3467 }
3468
Hanno Becker67bc7c32018-08-06 11:33:50 +01003469 ret = ssl_get_remaining_payload_in_datagram( ssl );
3470 if( ret < 0 )
3471 return( ret );
3472 max_frag_len = (size_t) ret;
3473
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003474 /* CCS is copied as is, while HS messages may need fragmentation */
3475 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3476 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003477 if( max_frag_len == 0 )
3478 {
3479 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3480 return( ret );
3481
3482 continue;
3483 }
3484
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003485 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003486 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003487 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003488
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003489 /* Update position inside current message */
3490 ssl->handshake->cur_msg_p += cur->len;
3491 }
3492 else
3493 {
3494 const unsigned char * const p = ssl->handshake->cur_msg_p;
3495 const size_t hs_len = cur->len - 12;
3496 const size_t frag_off = p - ( cur->p + 12 );
3497 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003498 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003499
Hanno Beckere1dcb032018-08-17 16:47:58 +01003500 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003501 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003502 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003503 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003504
Hanno Becker67bc7c32018-08-06 11:33:50 +01003505 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3506 return( ret );
3507
3508 continue;
3509 }
3510 max_hs_frag_len = max_frag_len - 12;
3511
3512 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3513 max_hs_frag_len : rem_len;
3514
3515 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003516 {
3517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003518 (unsigned) cur_hs_frag_len,
3519 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003520 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003521
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003522 /* Messages are stored with handshake headers as if not fragmented,
3523 * copy beginning of headers then fill fragmentation fields.
3524 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3525 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003526
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003527 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3528 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3529 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3530
Hanno Becker67bc7c32018-08-06 11:33:50 +01003531 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3532 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3533 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003534
3535 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3536
Hanno Becker3f7b9732018-08-28 09:53:25 +01003537 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003538 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3539 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003540 ssl->out_msgtype = cur->type;
3541
3542 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003543 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003544 }
3545
3546 /* If done with the current message move to the next one if any */
3547 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3548 {
3549 if( cur->next != NULL )
3550 {
3551 ssl->handshake->cur_msg = cur->next;
3552 ssl->handshake->cur_msg_p = cur->next->p + 12;
3553 }
3554 else
3555 {
3556 ssl->handshake->cur_msg = NULL;
3557 ssl->handshake->cur_msg_p = NULL;
3558 }
3559 }
3560
3561 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003562 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003565 return( ret );
3566 }
3567 }
3568
Hanno Becker67bc7c32018-08-06 11:33:50 +01003569 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3570 return( ret );
3571
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003572 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3574 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003575 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003578 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3579 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003580
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
3583 return( 0 );
3584}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585
3586/*
3587 * To be called when the last message of an incoming flight is received.
3588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003590{
3591 /* We won't need to resend that one any more */
3592 ssl_flight_free( ssl->handshake->flight );
3593 ssl->handshake->flight = NULL;
3594 ssl->handshake->cur_msg = NULL;
3595
3596 /* The next incoming flight will start with this msg_seq */
3597 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3598
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003599 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003600 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003601
Hanno Becker0271f962018-08-16 13:23:47 +01003602 /* Clear future message buffering structure. */
3603 ssl_buffering_free( ssl );
3604
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003605 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003606 ssl_set_timer( ssl, 0 );
3607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003608 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3609 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612 }
3613 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003615}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003616
3617/*
3618 * To be called when the last message of an outgoing flight is send.
3619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003620void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003621{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003622 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003623 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3626 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003629 }
3630 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634
Paul Bakker5121ce52009-01-03 21:22:43 +00003635/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003636 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003638
3639/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003640 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003641 *
3642 * - fill in handshake headers
3643 * - update handshake checksum
3644 * - DTLS: save message for resending
3645 * - then pass to the record layer
3646 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003647 * DTLS: except for HelloRequest, messages are only queued, and will only be
3648 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003649 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003650 * Inputs:
3651 * - ssl->out_msglen: 4 + actual handshake message len
3652 * (4 is the size of handshake headers for TLS)
3653 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3654 * - ssl->out_msg + 4: the handshake message body
3655 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003656 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003657 * - ssl->out_msglen: the length of the record contents
3658 * (including handshake headers but excluding record headers)
3659 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003660 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003661int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003663 int ret;
3664 const size_t hs_len = ssl->out_msglen - 4;
3665 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3668
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003669 /*
3670 * Sanity checks
3671 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003672 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003673 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3674 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003675 /* In SSLv3, the client might send a NoCertificate alert. */
3676#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3677 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3678 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3679 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3680#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3681 {
3682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3683 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3684 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003686
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003687 /* Whenever we send anything different from a
3688 * HelloRequest we should be in a handshake - double check. */
3689 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3690 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003691 ssl->handshake == NULL )
3692 {
3693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003698 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003699 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003700 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003706
Hanno Beckerb50a2532018-08-06 11:52:54 +01003707 /* Double-check that we did not exceed the bounds
3708 * of the outgoing record buffer.
3709 * This should never fail as the various message
3710 * writing functions must obey the bounds of the
3711 * outgoing record buffer, but better be safe.
3712 *
3713 * Note: We deliberately do not check for the MTU or MFL here.
3714 */
3715 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3716 {
3717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3718 "size %u, maximum %u",
3719 (unsigned) ssl->out_msglen,
3720 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3722 }
3723
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003724 /*
3725 * Fill handshake headers
3726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003729 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
3730 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
3731 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003733 /*
3734 * DTLS has additional fields in the Handshake layer,
3735 * between the length field and the actual payload:
3736 * uint16 message_seq;
3737 * uint24 fragment_offset;
3738 * uint24 fragment_length;
3739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003741 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003742 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003743 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10003744 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01003745 {
3746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
3747 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003748 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10003749 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01003750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3751 }
3752
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003753 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003754 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003755
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003756 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003757 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003758 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02003759 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
3760 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
3761 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02003762 }
3763 else
3764 {
3765 ssl->out_msg[4] = 0;
3766 ssl->out_msg[5] = 0;
3767 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003769 /* Handshake hashes are computed without fragmentation,
3770 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01003771 memset( ssl->out_msg + 6, 0x00, 3 );
3772 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003775
Hanno Becker0207e532018-08-28 10:28:28 +01003776 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003777 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
3778 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 }
3780
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003781 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003783 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerf6d6e302018-11-07 11:57:51 +00003784 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3785 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786 {
3787 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
3788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003790 return( ret );
3791 }
3792 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003793 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003795 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003797 {
3798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3799 return( ret );
3800 }
3801 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003802
3803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
3804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003805 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003806}
3807
3808/*
3809 * Record layer functions
3810 */
3811
3812/*
3813 * Write current record.
3814 *
3815 * Uses:
3816 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
3817 * - ssl->out_msglen: length of the record content (excl headers)
3818 * - ssl->out_msg: record content
3819 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003821{
3822 int ret, done = 0;
3823 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003824 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003825
3826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003829 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003830 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003831 {
3832 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
3833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003835 return( ret );
3836 }
3837
3838 len = ssl->out_msglen;
3839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3843 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 ret = mbedtls_ssl_hw_record_write( ssl );
3848 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
3851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003852 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003853
3854 if( ret == 0 )
3855 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00003858 if( !done )
3859 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003860 unsigned i;
3861 size_t protected_record_size;
3862
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003863 /* Skip writing the record content type to after the encryption,
3864 * as it may change when using the CID extension. */
3865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003867 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003868
Hanno Becker19859472018-08-06 09:40:20 +01003869 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003870 ssl->out_len[0] = (unsigned char)( len >> 8 );
3871 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003872
Paul Bakker48916f92012-09-16 19:57:18 +00003873 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003874 {
Hanno Becker3307b532017-12-27 21:37:21 +00003875 mbedtls_record rec;
3876
3877 rec.buf = ssl->out_iv;
3878 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
3879 ( ssl->out_iv - ssl->out_buf );
3880 rec.data_len = ssl->out_msglen;
3881 rec.data_offset = ssl->out_msg - rec.buf;
3882
3883 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
3884 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
3885 ssl->conf->transport, rec.ver );
3886 rec.type = ssl->out_msgtype;
3887
Hanno Beckera5a2b082019-05-15 14:03:01 +01003888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker505089d2019-05-01 09:45:57 +01003889 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckere83efe62019-04-29 13:52:53 +01003890 rec.cid_len = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckere83efe62019-04-29 13:52:53 +01003892
Hanno Becker611a83b2018-01-03 14:27:32 +00003893 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker3307b532017-12-27 21:37:21 +00003894 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00003897 return( ret );
3898 }
3899
Hanno Becker3307b532017-12-27 21:37:21 +00003900 if( rec.data_offset != 0 )
3901 {
3902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3903 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3904 }
3905
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003906 /* Update the record content type and CID. */
3907 ssl->out_msgtype = rec.type;
Hanno Beckera5a2b082019-05-15 14:03:01 +01003908#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker70e79282019-05-03 14:34:53 +01003909 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01003910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerc5aee962019-03-14 12:56:23 +00003911 ssl->out_msglen = len = rec.data_len;
Hanno Becker3307b532017-12-27 21:37:21 +00003912 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
3913 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00003914 }
3915
Hanno Becker43395762019-05-03 14:46:38 +01003916 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003917
3918#if defined(MBEDTLS_SSL_PROTO_DTLS)
3919 /* In case of DTLS, double-check that we don't exceed
3920 * the remaining space in the datagram. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003921 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2b1e3542018-08-06 11:19:13 +01003922 {
Hanno Becker554b0af2018-08-22 20:33:41 +01003923 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003924 if( ret < 0 )
3925 return( ret );
3926
3927 if( protected_record_size > (size_t) ret )
3928 {
3929 /* Should never happen */
3930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3931 }
3932 }
3933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003934
Hanno Beckerff3e9c22019-05-08 11:57:13 +01003935 /* Now write the potentially updated record content type. */
3936 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003939 "version = [%d:%d], msglen = %d",
3940 ssl->out_hdr[0], ssl->out_hdr[1],
3941 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01003944 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01003945
3946 ssl->out_left += protected_record_size;
3947 ssl->out_hdr += protected_record_size;
3948 ssl_update_out_pointers( ssl, ssl->transform_out );
3949
Hanno Becker04484622018-08-06 09:49:38 +01003950 for( i = 8; i > ssl_ep_len( ssl ); i-- )
3951 if( ++ssl->cur_out_ctr[i - 1] != 0 )
3952 break;
3953
3954 /* The loop goes to its end iff the counter is wrapping */
3955 if( i == ssl_ep_len( ssl ) )
3956 {
3957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
3958 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 }
3961
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02003963 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker47db8772018-08-21 13:32:13 +01003964 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003965 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003966 size_t remaining;
3967 ret = ssl_get_remaining_payload_in_datagram( ssl );
3968 if( ret < 0 )
3969 {
3970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
3971 ret );
3972 return( ret );
3973 }
3974
3975 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003976 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01003977 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003978 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01003979 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 else
3981 {
Hanno Becker513815a2018-08-20 11:56:09 +01003982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003983 }
3984 }
3985#endif /* MBEDTLS_SSL_PROTO_DTLS */
3986
3987 if( ( flush == SSL_FORCE_FLUSH ) &&
3988 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 return( ret );
3992 }
3993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
3996 return( 0 );
3997}
3998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004000
4001static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4002{
4003 if( ssl->in_msglen < ssl->in_hslen ||
4004 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4005 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4006 {
4007 return( 1 );
4008 }
4009 return( 0 );
4010}
Hanno Becker44650b72018-08-16 12:51:11 +01004011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004012static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004013{
4014 return( ( ssl->in_msg[9] << 16 ) |
4015 ( ssl->in_msg[10] << 8 ) |
4016 ssl->in_msg[11] );
4017}
4018
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004019static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004020{
4021 return( ( ssl->in_msg[6] << 16 ) |
4022 ( ssl->in_msg[7] << 8 ) |
4023 ssl->in_msg[8] );
4024}
4025
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004026static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004027{
4028 uint32_t msg_len, frag_off, frag_len;
4029
4030 msg_len = ssl_get_hs_total_len( ssl );
4031 frag_off = ssl_get_hs_frag_off( ssl );
4032 frag_len = ssl_get_hs_frag_len( ssl );
4033
4034 if( frag_off > msg_len )
4035 return( -1 );
4036
4037 if( frag_len > msg_len - frag_off )
4038 return( -1 );
4039
4040 if( frag_len + 12 > ssl->in_msglen )
4041 return( -1 );
4042
4043 return( 0 );
4044}
4045
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004046/*
4047 * Mark bits in bitmask (used for DTLS HS reassembly)
4048 */
4049static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4050{
4051 unsigned int start_bits, end_bits;
4052
4053 start_bits = 8 - ( offset % 8 );
4054 if( start_bits != 8 )
4055 {
4056 size_t first_byte_idx = offset / 8;
4057
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004058 /* Special case */
4059 if( len <= start_bits )
4060 {
4061 for( ; len != 0; len-- )
4062 mask[first_byte_idx] |= 1 << ( start_bits - len );
4063
4064 /* Avoid potential issues with offset or len becoming invalid */
4065 return;
4066 }
4067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004068 offset += start_bits; /* Now offset % 8 == 0 */
4069 len -= start_bits;
4070
4071 for( ; start_bits != 0; start_bits-- )
4072 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4073 }
4074
4075 end_bits = len % 8;
4076 if( end_bits != 0 )
4077 {
4078 size_t last_byte_idx = ( offset + len ) / 8;
4079
4080 len -= end_bits; /* Now len % 8 == 0 */
4081
4082 for( ; end_bits != 0; end_bits-- )
4083 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4084 }
4085
4086 memset( mask + offset / 8, 0xFF, len / 8 );
4087}
4088
4089/*
4090 * Check that bitmask is full
4091 */
4092static int ssl_bitmask_check( unsigned char *mask, size_t len )
4093{
4094 size_t i;
4095
4096 for( i = 0; i < len / 8; i++ )
4097 if( mask[i] != 0xFF )
4098 return( -1 );
4099
4100 for( i = 0; i < len % 8; i++ )
4101 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4102 return( -1 );
4103
4104 return( 0 );
4105}
4106
Hanno Becker56e205e2018-08-16 09:06:12 +01004107/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004108static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004109 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004110{
Hanno Becker56e205e2018-08-16 09:06:12 +01004111 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004112
Hanno Becker56e205e2018-08-16 09:06:12 +01004113 alloc_len = 12; /* Handshake header */
4114 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004115
Hanno Beckerd07df862018-08-16 09:14:58 +01004116 if( add_bitmap )
4117 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004118
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004119 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004120}
Hanno Becker56e205e2018-08-16 09:06:12 +01004121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004123
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004124static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004125{
4126 return( ( ssl->in_msg[1] << 16 ) |
4127 ( ssl->in_msg[2] << 8 ) |
4128 ssl->in_msg[3] );
4129}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004130
Simon Butcher99000142016-10-13 17:21:01 +01004131int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004136 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004138 }
4139
Hanno Becker12555c62018-08-16 12:47:53 +01004140 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004143 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004144 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004147 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004148 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004149 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004150 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004151
Hanno Becker44650b72018-08-16 12:51:11 +01004152 if( ssl_check_hs_header( ssl ) != 0 )
4153 {
4154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4156 }
4157
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004158 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004159 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4160 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4161 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4162 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004163 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004164 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4165 {
4166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4167 recv_msg_seq,
4168 ssl->handshake->in_msg_seq ) );
4169 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4170 }
4171
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004172 /* Retransmit only on last message from previous flight, to avoid
4173 * too many retransmissions.
4174 * Besides, No sane server ever retransmits HelloVerifyRequest */
4175 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004176 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004179 "message_seq = %d, start_of_flight = %d",
4180 recv_msg_seq,
4181 ssl->handshake->in_flight_start_seq ) );
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004186 return( ret );
4187 }
4188 }
4189 else
4190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004192 "message_seq = %d, expected = %d",
4193 recv_msg_seq,
4194 ssl->handshake->in_msg_seq ) );
4195 }
4196
Hanno Becker90333da2017-10-10 11:27:13 +01004197 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004198 }
4199 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004200
Hanno Becker6d97ef52018-08-16 13:09:04 +01004201 /* Message reassembly is handled alongside buffering of future
4202 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004203 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004204 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004205 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004208 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004209 }
4210 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004211 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004213#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004214 {
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004215 /* With TLS we don't handle fragmentation (for now) */
4216 if( ssl->in_msglen < ssl->in_hslen )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4219 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4220 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004221 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +02004222#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004223
Simon Butcher99000142016-10-13 17:21:01 +01004224 return( 0 );
4225}
4226
4227void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4228{
Hanno Becker0271f962018-08-16 13:23:47 +01004229 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004230
Hanno Becker0271f962018-08-16 13:23:47 +01004231 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004232 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004233 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004234 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004235
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004236 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004238 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004239 ssl->handshake != NULL )
4240 {
Hanno Becker0271f962018-08-16 13:23:47 +01004241 unsigned offset;
4242 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004243
Hanno Becker0271f962018-08-16 13:23:47 +01004244 /* Increment handshake sequence number */
4245 hs->in_msg_seq++;
4246
4247 /*
4248 * Clear up handshake buffering and reassembly structure.
4249 */
4250
4251 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004252 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004253
4254 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004255 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4256 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004257 offset++, hs_buf++ )
4258 {
4259 *hs_buf = *(hs_buf + 1);
4260 }
4261
4262 /* Create a fresh last entry */
4263 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004264 }
4265#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004266}
4267
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004268/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004269 * DTLS anti-replay: RFC 6347 4.1.2.6
4270 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004271 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4272 * Bit n is set iff record number in_window_top - n has been seen.
4273 *
4274 * Usually, in_window_top is the last record number seen and the lsb of
4275 * in_window is set. The only exception is the initial state (record number 0
4276 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004278#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4279static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004280{
4281 ssl->in_window_top = 0;
4282 ssl->in_window = 0;
4283}
4284
4285static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4286{
4287 return( ( (uint64_t) buf[0] << 40 ) |
4288 ( (uint64_t) buf[1] << 32 ) |
4289 ( (uint64_t) buf[2] << 24 ) |
4290 ( (uint64_t) buf[3] << 16 ) |
4291 ( (uint64_t) buf[4] << 8 ) |
4292 ( (uint64_t) buf[5] ) );
4293}
4294
4295/*
4296 * Return 0 if sequence number is acceptable, -1 otherwise
4297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004299{
4300 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4301 uint64_t bit;
4302
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004303 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004304 return( 0 );
4305
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004306 if( rec_seqnum > ssl->in_window_top )
4307 return( 0 );
4308
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004309 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004310
4311 if( bit >= 64 )
4312 return( -1 );
4313
4314 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4315 return( -1 );
4316
4317 return( 0 );
4318}
4319
4320/*
4321 * Update replay window on new validated record
4322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004324{
4325 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4326
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004327 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004328 return;
4329
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004330 if( rec_seqnum > ssl->in_window_top )
4331 {
4332 /* Update window_top and the contents of the window */
4333 uint64_t shift = rec_seqnum - ssl->in_window_top;
4334
4335 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004336 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004337 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004338 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004339 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004340 ssl->in_window |= 1;
4341 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004342
4343 ssl->in_window_top = rec_seqnum;
4344 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004345 else
4346 {
4347 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004348 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004349
4350 if( bit < 64 ) /* Always true, but be extra sure */
4351 ssl->in_window |= (uint64_t) 1 << bit;
4352 }
4353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004355
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004356#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004357/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004358static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4359
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004360/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004361 * Without any SSL context, check if a datagram looks like a ClientHello with
4362 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004363 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004364 *
4365 * - if cookie is valid, return 0
4366 * - if ClientHello looks superficially valid but cookie is not,
4367 * fill obuf and set olen, then
4368 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4369 * - otherwise return a specific error code
4370 */
4371static int ssl_check_dtls_clihlo_cookie(
4372 mbedtls_ssl_cookie_write_t *f_cookie_write,
4373 mbedtls_ssl_cookie_check_t *f_cookie_check,
4374 void *p_cookie,
4375 const unsigned char *cli_id, size_t cli_id_len,
4376 const unsigned char *in, size_t in_len,
4377 unsigned char *obuf, size_t buf_len, size_t *olen )
4378{
4379 size_t sid_len, cookie_len;
4380 unsigned char *p;
4381
4382 if( f_cookie_write == NULL || f_cookie_check == NULL )
4383 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4384
4385 /*
4386 * Structure of ClientHello with record and handshake headers,
4387 * and expected values. We don't need to check a lot, more checks will be
4388 * done when actually parsing the ClientHello - skipping those checks
4389 * avoids code duplication and does not make cookie forging any easier.
4390 *
4391 * 0-0 ContentType type; copied, must be handshake
4392 * 1-2 ProtocolVersion version; copied
4393 * 3-4 uint16 epoch; copied, must be 0
4394 * 5-10 uint48 sequence_number; copied
4395 * 11-12 uint16 length; (ignored)
4396 *
4397 * 13-13 HandshakeType msg_type; (ignored)
4398 * 14-16 uint24 length; (ignored)
4399 * 17-18 uint16 message_seq; copied
4400 * 19-21 uint24 fragment_offset; copied, must be 0
4401 * 22-24 uint24 fragment_length; (ignored)
4402 *
4403 * 25-26 ProtocolVersion client_version; (ignored)
4404 * 27-58 Random random; (ignored)
4405 * 59-xx SessionID session_id; 1 byte len + sid_len content
4406 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4407 * ...
4408 *
4409 * Minimum length is 61 bytes.
4410 */
4411 if( in_len < 61 ||
4412 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4413 in[3] != 0 || in[4] != 0 ||
4414 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4415 {
4416 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4417 }
4418
4419 sid_len = in[59];
4420 if( sid_len > in_len - 61 )
4421 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4422
4423 cookie_len = in[60 + sid_len];
4424 if( cookie_len > in_len - 60 )
4425 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4426
4427 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4428 cli_id, cli_id_len ) == 0 )
4429 {
4430 /* Valid cookie */
4431 return( 0 );
4432 }
4433
4434 /*
4435 * If we get here, we've got an invalid cookie, let's prepare HVR.
4436 *
4437 * 0-0 ContentType type; copied
4438 * 1-2 ProtocolVersion version; copied
4439 * 3-4 uint16 epoch; copied
4440 * 5-10 uint48 sequence_number; copied
4441 * 11-12 uint16 length; olen - 13
4442 *
4443 * 13-13 HandshakeType msg_type; hello_verify_request
4444 * 14-16 uint24 length; olen - 25
4445 * 17-18 uint16 message_seq; copied
4446 * 19-21 uint24 fragment_offset; copied
4447 * 22-24 uint24 fragment_length; olen - 25
4448 *
4449 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4450 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4451 *
4452 * Minimum length is 28.
4453 */
4454 if( buf_len < 28 )
4455 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4456
4457 /* Copy most fields and adapt others */
4458 memcpy( obuf, in, 25 );
4459 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4460 obuf[25] = 0xfe;
4461 obuf[26] = 0xff;
4462
4463 /* Generate and write actual cookie */
4464 p = obuf + 28;
4465 if( f_cookie_write( p_cookie,
4466 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4467 {
4468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4469 }
4470
4471 *olen = p - obuf;
4472
4473 /* Go back and fill length fields */
4474 obuf[27] = (unsigned char)( *olen - 28 );
4475
4476 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4477 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4478 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4479
4480 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4481 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4482
4483 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4484}
4485
4486/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004487 * Handle possible client reconnect with the same UDP quadruplet
4488 * (RFC 6347 Section 4.2.8).
4489 *
4490 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4491 * that looks like a ClientHello.
4492 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004493 * - if the input looks like a ClientHello without cookies,
4494 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004495 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004496 * - if the input looks like a ClientHello with a valid cookie,
4497 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004498 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004499 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004500 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004501 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004502 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4503 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004504 */
4505static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4506{
4507 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004508 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004509
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004510 ret = ssl_check_dtls_clihlo_cookie(
4511 ssl->conf->f_cookie_write,
4512 ssl->conf->f_cookie_check,
4513 ssl->conf->p_cookie,
4514 ssl->cli_id, ssl->cli_id_len,
4515 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004516 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004517
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004518 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4519
4520 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004521 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004522 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004523 * If the error is permanent we'll catch it later,
4524 * if it's not, then hopefully it'll work next time. */
4525 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4526
4527 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004528 }
4529
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004530 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004531 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004532 /* Got a valid cookie, partially reset context */
4533 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4534 {
4535 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4536 return( ret );
4537 }
4538
4539 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004540 }
4541
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004542 return( ret );
4543}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004544#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004545
Hanno Becker46483f12019-05-03 13:25:54 +01004546static int ssl_check_record_type( uint8_t record_type )
4547{
4548 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4549 record_type != MBEDTLS_SSL_MSG_ALERT &&
4550 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4551 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4552 {
4553 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4554 }
4555
4556 return( 0 );
4557}
4558
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004559/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004560 * ContentType type;
4561 * ProtocolVersion version;
4562 * uint16 epoch; // DTLS only
4563 * uint48 sequence_number; // DTLS only
4564 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004565 *
4566 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004567 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004568 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4569 *
4570 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004571 * 1. proceed with the record if this function returns 0
4572 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4573 * 3. return CLIENT_RECONNECT if this function return that value
4574 * 4. drop the whole datagram if this function returns anything else.
4575 * Point 2 is needed when the peer is resending, and we have already received
4576 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004578static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004579{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004580 int major_ver, minor_ver;
Hanno Becker8b09b732019-05-08 12:03:28 +01004581 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004582
Hanno Becker8b09b732019-05-08 12:03:28 +01004583 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004584
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004586 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004588 /* Check record type */
Hanno Beckera5a2b082019-05-15 14:03:01 +01004589#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004590 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b09b732019-05-08 12:03:28 +01004591 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4592 ssl->conf->cid_len != 0 )
4593 {
4594 /* Shift pointers to account for record header including CID
4595 * struct {
4596 * ContentType special_type = tls12_cid;
4597 * ProtocolVersion version;
4598 * uint16 epoch;
4599 * uint48 sequence_number;
Hanno Becker3b2bf5b2019-05-23 17:03:19 +01004600 * opaque cid[cid_length]; // Additional field compared to
4601 * // default DTLS record format
Hanno Becker8b09b732019-05-08 12:03:28 +01004602 * uint16 length;
4603 * opaque enc_content[DTLSCiphertext.length];
4604 * } DTLSCiphertext;
4605 */
4606
4607 /* So far, we only support static CID lengths
4608 * fixed in the configuration. */
4609 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4610 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4611 }
4612 else
Hanno Beckera5a2b082019-05-15 14:03:01 +01004613#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker46483f12019-05-03 13:25:54 +01004614 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004617
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004618#if defined(MBEDTLS_SSL_PROTO_TLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004619 /* Silently ignore invalid DTLS records as recommended by RFC 6347
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004620 * Section 4.1.2.7, that is, send alert only with TLS */
4621 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
4622 {
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004623 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4624 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004625 }
4626#endif /* MBEDTLS_SSL_PROTO_TLS */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004629 }
4630
4631 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004632 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4635 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636 }
4637
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004638 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4641 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004642 }
4643
Hanno Becker8b09b732019-05-08 12:03:28 +01004644 /* Now that the total length of the record header is known, ensure
4645 * that the current datagram is large enough to hold it.
4646 * This would fail, for example, if we received a datagram of
4647 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4648 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4649 if( ret != 0 )
4650 {
4651 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4652 return( ret );
4653 }
4654 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4655
4656 /* Parse and validate record length
4657 * This must happen after the CID parsing because
4658 * its position in the record header depends on
4659 * the presence of a CID. */
4660
4661 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004662 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004663 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4666 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004667 }
4668
Hanno Becker8b09b732019-05-08 12:03:28 +01004669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Beckerd8f7c4a2019-05-23 17:03:44 +01004670 "version = [%d:%d], msglen = %d",
4671 ssl->in_msgtype,
4672 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Becker8b09b732019-05-08 12:03:28 +01004673
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004674 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004675 * DTLS-related tests.
4676 * Check epoch before checking length constraint because
4677 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4678 * message gets duplicated before the corresponding Finished message,
4679 * the second ChangeCipherSpec should be discarded because it belongs
4680 * to an old epoch, but not because its length is shorter than
4681 * the minimum record length for packets using the new record transform.
4682 * Note that these two kinds of failures are handled differently,
4683 * as an unexpected record is silently skipped but an invalid
4684 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004685 */
4686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004687 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004688 {
4689 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4690
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004691 /* Check epoch (and sequence number) with DTLS */
4692 if( rec_epoch != ssl->in_epoch )
4693 {
4694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4695 "expected %d, received %d",
4696 ssl->in_epoch, rec_epoch ) );
4697
4698#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4699 /*
4700 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4701 * access the first byte of record content (handshake type), as we
4702 * have an active transform (possibly iv_len != 0), so use the
4703 * fact that the record header len is 13 instead.
4704 */
4705 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4706 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4707 rec_epoch == 0 &&
4708 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4709 ssl->in_left > 13 &&
4710 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4711 {
4712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4713 "from the same port" ) );
4714 return( ssl_handle_possible_reconnect( ssl ) );
4715 }
4716 else
4717#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004718 {
4719 /* Consider buffering the record. */
4720 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4721 {
4722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4723 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4724 }
4725
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004726 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004727 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004728 }
4729
4730#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4731 /* Replay detection only works for the current epoch */
4732 if( rec_epoch == ssl->in_epoch &&
4733 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4734 {
4735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4736 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4737 }
4738#endif
4739 }
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
Hanno Becker52c6dc62017-05-26 16:07:36 +01004742
4743 /* Check length against bounds of the current transform and version */
4744 if( ssl->transform_in == NULL )
4745 {
4746 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004747 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004748 {
4749 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4750 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4751 }
4752 }
4753 else
4754 {
4755 if( ssl->in_msglen < ssl->transform_in->minlen )
4756 {
4757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4758 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4759 }
4760
4761#if defined(MBEDTLS_SSL_PROTO_SSL3)
4762 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004763 ssl->in_msglen > ssl->transform_in->minlen + 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#endif
4769#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4770 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4771 /*
4772 * TLS encrypted messages can have up to 256 bytes of padding
4773 */
4774 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4775 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004776 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004777 {
4778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4780 }
4781#endif
4782 }
4783
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004784 return( 0 );
4785}
Paul Bakker5121ce52009-01-03 21:22:43 +00004786
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004787/*
4788 * If applicable, decrypt (and decompress) record content
4789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004791{
4792 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker43395762019-05-03 14:46:38 +01004795 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4798 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 ret = mbedtls_ssl_hw_record_read( ssl );
4803 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
4806 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004807 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004808
4809 if( ret == 0 )
4810 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00004813 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 {
Hanno Becker4c6876b2017-12-27 21:28:58 +00004815 mbedtls_record rec;
4816
4817 rec.buf = ssl->in_iv;
4818 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
4819 - ( ssl->in_iv - ssl->in_buf );
4820 rec.data_len = ssl->in_msglen;
4821 rec.data_offset = 0;
Hanno Beckera5a2b082019-05-15 14:03:01 +01004822#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker7ba35682019-05-09 15:54:28 +01004823 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Becker70e79282019-05-03 14:34:53 +01004824 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera5a2b082019-05-15 14:03:01 +01004825#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004826
4827 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
4828 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4829 ssl->conf->transport, rec.ver );
4830 rec.type = ssl->in_msgtype;
Hanno Becker611a83b2018-01-03 14:27:32 +00004831 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
4832 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004835
Hanno Beckera5a2b082019-05-15 14:03:01 +01004836#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004837 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
4838 ssl->conf->ignore_unexpected_cid
4839 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
4840 {
Hanno Becker675c4d62019-05-24 10:11:06 +01004841 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker687e0fb2019-05-08 13:02:55 +01004842 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Beckere8eff9a2019-05-14 11:30:10 +01004843 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004844#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker687e0fb2019-05-08 13:02:55 +01004845
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 return( ret );
4847 }
4848
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004849 if( ssl->in_msgtype != rec.type )
Hanno Becker93012fe2018-08-07 14:30:18 +01004850 {
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004851 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
4852 ssl->in_msgtype, rec.type ) );
Hanno Becker93012fe2018-08-07 14:30:18 +01004853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004855 /* The record content type may change during decryption,
4856 * so re-read it. */
4857 ssl->in_msgtype = rec.type;
4858 /* Also update the input buffer, because unfortunately
4859 * the server-side ssl_parse_client_hello() reparses the
4860 * record header when receiving a ClientHello initiating
4861 * a renegotiation. */
4862 ssl->in_hdr[0] = rec.type;
Hanno Beckerf5970a02019-05-08 09:38:41 +01004863 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker4c6876b2017-12-27 21:28:58 +00004864 ssl->in_msglen = rec.data_len;
4865 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4866 ssl->in_len[1] = (unsigned char)( rec.data_len );
4867
Paul Bakker5121ce52009-01-03 21:22:43 +00004868 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
4869 ssl->in_msg, ssl->in_msglen );
4870
Hanno Beckera5a2b082019-05-15 14:03:01 +01004871#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004872 /* We have already checked the record content type
4873 * in ssl_parse_record_header(), failing or silently
4874 * dropping the record in the case of an unknown type.
4875 *
4876 * Since with the use of CIDs, the record content type
4877 * might change during decryption, re-check the record
4878 * content type, but treat a failure as fatal this time. */
4879 if( ssl_check_record_type( ssl->in_msgtype ) )
4880 {
4881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4882 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4883 }
Hanno Beckera5a2b082019-05-15 14:03:01 +01004884#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerff3e9c22019-05-08 11:57:13 +01004885
Angus Grattond8213d02016-05-25 20:56:48 +10004886 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00004887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4889 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004890 }
Hanno Becker4c6876b2017-12-27 21:28:58 +00004891 else if( ssl->in_msglen == 0 )
4892 {
4893#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4894 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
4895 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4896 {
4897 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
4898 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
4899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4900 }
4901#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4902
4903 ssl->nb_zero++;
4904
4905 /*
4906 * Three or more empty messages may be a DoS attack
4907 * (excessive CPU consumption).
4908 */
4909 if( ssl->nb_zero > 3 )
4910 {
4911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker70463db2019-05-08 10:38:32 +01004912 "messages, possible DoS attack" ) );
4913 /* Treat the records as if they were not properly authenticated,
4914 * thereby failing the connection if we see more than allowed
4915 * by the configured bad MAC threshold. */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004916 return( MBEDTLS_ERR_SSL_INVALID_MAC );
4917 }
4918 }
4919 else
4920 ssl->nb_zero = 0;
4921
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004922 /* Only needed for TLS, as with DTLS in_ctr is read from the header */
4923#if defined(MBEDTLS_SSL_PROTO_TLS)
4924 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004925 {
4926 unsigned i;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004927 for( i = 8; i > 0; i-- )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004928 if( ++ssl->in_ctr[i - 1] != 0 )
4929 break;
4930
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +02004931 /* The loop goes to its end only if the counter is wrapping around */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004932 if( i == 0 )
Hanno Becker4c6876b2017-12-27 21:28:58 +00004933 {
4934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
4935 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4936 }
4937 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02004938#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker4c6876b2017-12-27 21:28:58 +00004939
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 }
4941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004943 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004945 {
4946 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004949 return( ret );
4950 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00004951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004954#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02004955 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004957 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004958 }
4959#endif
4960
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004961 return( 0 );
4962}
4963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004964static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004965
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004966/*
4967 * Read a record.
4968 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004969 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4970 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4971 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004972 */
Hanno Becker1097b342018-08-15 14:09:41 +01004973
4974/* Helper functions for mbedtls_ssl_read_record(). */
4975static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004976static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4977static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004978
Hanno Becker327c93b2018-08-15 13:56:18 +01004979int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004980 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004981{
4982 int ret;
4983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004985
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004986 if( ssl->keep_current_message == 0 )
4987 {
4988 do {
Simon Butcher99000142016-10-13 17:21:01 +01004989
Hanno Becker26994592018-08-15 14:14:59 +01004990 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004991 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004992 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004993
Hanno Beckere74d5562018-08-15 14:26:08 +01004994 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004995 {
Hanno Becker40f50842018-08-15 14:48:01 +01004996#if defined(MBEDTLS_SSL_PROTO_DTLS)
4997 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004998
Hanno Becker40f50842018-08-15 14:48:01 +01004999 /* We only check for buffered messages if the
5000 * current datagram is fully consumed. */
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005001 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005002 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005003 {
Hanno Becker40f50842018-08-15 14:48:01 +01005004 if( ssl_load_buffered_message( ssl ) == 0 )
5005 have_buffered = 1;
5006 }
5007
5008 if( have_buffered == 0 )
5009#endif /* MBEDTLS_SSL_PROTO_DTLS */
5010 {
5011 ret = ssl_get_next_record( ssl );
5012 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5013 continue;
5014
5015 if( ret != 0 )
5016 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005017 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005018 return( ret );
5019 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005020 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005021 }
5022
5023 ret = mbedtls_ssl_handle_message_type( ssl );
5024
Hanno Becker40f50842018-08-15 14:48:01 +01005025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5027 {
5028 /* Buffer future message */
5029 ret = ssl_buffer_message( ssl );
5030 if( ret != 0 )
5031 return( ret );
5032
5033 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5034 }
5035#endif /* MBEDTLS_SSL_PROTO_DTLS */
5036
Hanno Becker90333da2017-10-10 11:27:13 +01005037 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5038 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005039
5040 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005041 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005042 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005043 return( ret );
5044 }
5045
Hanno Becker327c93b2018-08-15 13:56:18 +01005046 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005047 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005048 {
5049 mbedtls_ssl_update_handshake_status( ssl );
5050 }
Simon Butcher99000142016-10-13 17:21:01 +01005051 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005052 else
Simon Butcher99000142016-10-13 17:21:01 +01005053 {
Hanno Becker02f59072018-08-15 14:00:24 +01005054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005055 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005056 }
5057
5058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5059
5060 return( 0 );
5061}
5062
Hanno Becker40f50842018-08-15 14:48:01 +01005063#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005064static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005065{
Hanno Becker40f50842018-08-15 14:48:01 +01005066 if( ssl->in_left > ssl->next_record_offset )
5067 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005068
Hanno Becker40f50842018-08-15 14:48:01 +01005069 return( 0 );
5070}
5071
5072static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5073{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005074 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005075 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005076 int ret = 0;
5077
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005078 if( hs == NULL )
5079 return( -1 );
5080
Hanno Beckere00ae372018-08-20 09:39:42 +01005081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5082
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005083 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5084 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5085 {
5086 /* Check if we have seen a ChangeCipherSpec before.
5087 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005088 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005089 {
5090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5091 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005092 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005093 }
5094
Hanno Becker39b8bc92018-08-28 17:17:13 +01005095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005096 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5097 ssl->in_msglen = 1;
5098 ssl->in_msg[0] = 1;
5099
5100 /* As long as they are equal, the exact value doesn't matter. */
5101 ssl->in_left = 0;
5102 ssl->next_record_offset = 0;
5103
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005104 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005105 goto exit;
5106 }
Hanno Becker37f95322018-08-16 13:55:32 +01005107
Hanno Beckerb8f50142018-08-28 10:01:34 +01005108#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005109 /* Debug only */
5110 {
5111 unsigned offset;
5112 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5113 {
5114 hs_buf = &hs->buffering.hs[offset];
5115 if( hs_buf->is_valid == 1 )
5116 {
5117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5118 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005119 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005120 }
5121 }
5122 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005123#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005124
5125 /* Check if we have buffered and/or fully reassembled the
5126 * next handshake message. */
5127 hs_buf = &hs->buffering.hs[0];
5128 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5129 {
5130 /* Synthesize a record containing the buffered HS message. */
5131 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5132 ( hs_buf->data[2] << 8 ) |
5133 hs_buf->data[3];
5134
5135 /* Double-check that we haven't accidentally buffered
5136 * a message that doesn't fit into the input buffer. */
5137 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5141 }
5142
5143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5144 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5145 hs_buf->data, msg_len + 12 );
5146
5147 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5148 ssl->in_hslen = msg_len + 12;
5149 ssl->in_msglen = msg_len + 12;
5150 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5151
5152 ret = 0;
5153 goto exit;
5154 }
5155 else
5156 {
5157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5158 hs->in_msg_seq ) );
5159 }
5160
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005161 ret = -1;
5162
5163exit:
5164
5165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5166 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005167}
5168
Hanno Beckera02b0b42018-08-21 17:20:27 +01005169static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5170 size_t desired )
5171{
5172 int offset;
5173 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5175 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005176
Hanno Becker01315ea2018-08-21 17:22:17 +01005177 /* Get rid of future records epoch first, if such exist. */
5178 ssl_free_buffered_record( ssl );
5179
5180 /* Check if we have enough space available now. */
5181 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5182 hs->buffering.total_bytes_buffered ) )
5183 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005185 return( 0 );
5186 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005187
Hanno Becker4f432ad2018-08-28 10:02:32 +01005188 /* We don't have enough space to buffer the next expected handshake
5189 * message. Remove buffers used for future messages to gain space,
5190 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005191 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5192 offset >= 0; offset-- )
5193 {
5194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5195 offset ) );
5196
Hanno Beckerb309b922018-08-23 13:18:05 +01005197 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005198
5199 /* Check if we have enough space available now. */
5200 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5201 hs->buffering.total_bytes_buffered ) )
5202 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005204 return( 0 );
5205 }
5206 }
5207
5208 return( -1 );
5209}
5210
Hanno Becker40f50842018-08-15 14:48:01 +01005211static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5212{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005213 int ret = 0;
5214 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5215
5216 if( hs == NULL )
5217 return( 0 );
5218
5219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5220
5221 switch( ssl->in_msgtype )
5222 {
5223 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005225
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005226 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005227 break;
5228
5229 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005230 {
5231 unsigned recv_msg_seq_offset;
5232 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5233 mbedtls_ssl_hs_buffer *hs_buf;
5234 size_t msg_len = ssl->in_hslen - 12;
5235
5236 /* We should never receive an old handshake
5237 * message - double-check nonetheless. */
5238 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5239 {
5240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5242 }
5243
5244 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5245 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5246 {
5247 /* Silently ignore -- message too far in the future */
5248 MBEDTLS_SSL_DEBUG_MSG( 2,
5249 ( "Ignore future HS message with sequence number %u, "
5250 "buffering window %u - %u",
5251 recv_msg_seq, ssl->handshake->in_msg_seq,
5252 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5253
5254 goto exit;
5255 }
5256
5257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5258 recv_msg_seq, recv_msg_seq_offset ) );
5259
5260 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5261
5262 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005263 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005264 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005265 size_t reassembly_buf_sz;
5266
Hanno Becker37f95322018-08-16 13:55:32 +01005267 hs_buf->is_fragmented =
5268 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5269
5270 /* We copy the message back into the input buffer
5271 * after reassembly, so check that it's not too large.
5272 * This is an implementation-specific limitation
5273 * and not one from the standard, hence it is not
5274 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005275 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005276 {
5277 /* Ignore message */
5278 goto exit;
5279 }
5280
Hanno Beckere0b150f2018-08-21 15:51:03 +01005281 /* Check if we have enough space to buffer the message. */
5282 if( hs->buffering.total_bytes_buffered >
5283 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5284 {
5285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5287 }
5288
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005289 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5290 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005291
5292 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5293 hs->buffering.total_bytes_buffered ) )
5294 {
5295 if( recv_msg_seq_offset > 0 )
5296 {
5297 /* If we can't buffer a future message because
5298 * of space limitations -- ignore. */
5299 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",
5300 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5301 (unsigned) hs->buffering.total_bytes_buffered ) );
5302 goto exit;
5303 }
Hanno Beckere1801392018-08-21 16:51:05 +01005304 else
5305 {
5306 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",
5307 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5308 (unsigned) hs->buffering.total_bytes_buffered ) );
5309 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005310
Hanno Beckera02b0b42018-08-21 17:20:27 +01005311 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005312 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005313 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",
5314 (unsigned) msg_len,
5315 (unsigned) reassembly_buf_sz,
5316 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005317 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005318 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5319 goto exit;
5320 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005321 }
5322
5323 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5324 msg_len ) );
5325
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005326 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5327 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005328 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005329 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005330 goto exit;
5331 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005332 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005333
5334 /* Prepare final header: copy msg_type, length and message_seq,
5335 * then add standardised fragment_offset and fragment_length */
5336 memcpy( hs_buf->data, ssl->in_msg, 6 );
5337 memset( hs_buf->data + 6, 0, 3 );
5338 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5339
5340 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005341
5342 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005343 }
5344 else
5345 {
5346 /* Make sure msg_type and length are consistent */
5347 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5350 /* Ignore */
5351 goto exit;
5352 }
5353 }
5354
Hanno Becker4422bbb2018-08-20 09:40:19 +01005355 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005356 {
5357 size_t frag_len, frag_off;
5358 unsigned char * const msg = hs_buf->data + 12;
5359
5360 /*
5361 * Check and copy current fragment
5362 */
5363
5364 /* Validation of header fields already done in
5365 * mbedtls_ssl_prepare_handshake_record(). */
5366 frag_off = ssl_get_hs_frag_off( ssl );
5367 frag_len = ssl_get_hs_frag_len( ssl );
5368
5369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5370 frag_off, frag_len ) );
5371 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5372
5373 if( hs_buf->is_fragmented )
5374 {
5375 unsigned char * const bitmask = msg + msg_len;
5376 ssl_bitmask_set( bitmask, frag_off, frag_len );
5377 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5378 msg_len ) == 0 );
5379 }
5380 else
5381 {
5382 hs_buf->is_complete = 1;
5383 }
5384
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5386 hs_buf->is_complete ? "" : "not yet " ) );
5387 }
5388
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005389 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005390 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005391
5392 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005393 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005394 break;
5395 }
5396
5397exit:
5398
5399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5400 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005401}
5402#endif /* MBEDTLS_SSL_PROTO_DTLS */
5403
Hanno Becker1097b342018-08-15 14:09:41 +01005404static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005405{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005406 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005407 * Consume last content-layer message and potentially
5408 * update in_msglen which keeps track of the contents'
5409 * consumption state.
5410 *
5411 * (1) Handshake messages:
5412 * Remove last handshake message, move content
5413 * and adapt in_msglen.
5414 *
5415 * (2) Alert messages:
5416 * Consume whole record content, in_msglen = 0.
5417 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005418 * (3) Change cipher spec:
5419 * Consume whole record content, in_msglen = 0.
5420 *
5421 * (4) Application data:
5422 * Don't do anything - the record layer provides
5423 * the application data as a stream transport
5424 * and consumes through mbedtls_ssl_read only.
5425 *
5426 */
5427
5428 /* Case (1): Handshake messages */
5429 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005430 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005431 /* Hard assertion to be sure that no application data
5432 * is in flight, as corrupting ssl->in_msglen during
5433 * ssl->in_offt != NULL is fatal. */
5434 if( ssl->in_offt != NULL )
5435 {
5436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5437 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5438 }
5439
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005440 /*
5441 * Get next Handshake message in the current record
5442 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005443
Hanno Becker4a810fb2017-05-24 16:27:30 +01005444 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005445 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005446 * current handshake content: If DTLS handshake
5447 * fragmentation is used, that's the fragment
5448 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005449 * size here is faulty and should be changed at
5450 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005451 * (2) While it doesn't seem to cause problems, one
5452 * has to be very careful not to assume that in_hslen
5453 * is always <= in_msglen in a sensible communication.
5454 * Again, it's wrong for DTLS handshake fragmentation.
5455 * The following check is therefore mandatory, and
5456 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005457 * Additionally, ssl->in_hslen might be arbitrarily out of
5458 * bounds after handling a DTLS message with an unexpected
5459 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005460 */
5461 if( ssl->in_hslen < ssl->in_msglen )
5462 {
5463 ssl->in_msglen -= ssl->in_hslen;
5464 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5465 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005466
Hanno Becker4a810fb2017-05-24 16:27:30 +01005467 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5468 ssl->in_msg, ssl->in_msglen );
5469 }
5470 else
5471 {
5472 ssl->in_msglen = 0;
5473 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005474
Hanno Becker4a810fb2017-05-24 16:27:30 +01005475 ssl->in_hslen = 0;
5476 }
5477 /* Case (4): Application data */
5478 else if( ssl->in_offt != NULL )
5479 {
5480 return( 0 );
5481 }
5482 /* Everything else (CCS & Alerts) */
5483 else
5484 {
5485 ssl->in_msglen = 0;
5486 }
5487
Hanno Becker1097b342018-08-15 14:09:41 +01005488 return( 0 );
5489}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005490
Hanno Beckere74d5562018-08-15 14:26:08 +01005491static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5492{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005493 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005494 return( 1 );
5495
5496 return( 0 );
5497}
5498
Hanno Becker5f066e72018-08-16 14:56:31 +01005499#if defined(MBEDTLS_SSL_PROTO_DTLS)
5500
5501static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5502{
5503 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5504 if( hs == NULL )
5505 return;
5506
Hanno Becker01315ea2018-08-21 17:22:17 +01005507 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005508 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005509 hs->buffering.total_bytes_buffered -=
5510 hs->buffering.future_record.len;
5511
5512 mbedtls_free( hs->buffering.future_record.data );
5513 hs->buffering.future_record.data = NULL;
5514 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005515}
5516
5517static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5518{
5519 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5520 unsigned char * rec;
5521 size_t rec_len;
5522 unsigned rec_epoch;
5523
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005524 if( MBEDTLS_SSL_TRANSPORT_IS_TLS( ssl->conf->transport ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01005525 return( 0 );
5526
5527 if( hs == NULL )
5528 return( 0 );
5529
Hanno Becker5f066e72018-08-16 14:56:31 +01005530 rec = hs->buffering.future_record.data;
5531 rec_len = hs->buffering.future_record.len;
5532 rec_epoch = hs->buffering.future_record.epoch;
5533
5534 if( rec == NULL )
5535 return( 0 );
5536
Hanno Becker4cb782d2018-08-20 11:19:05 +01005537 /* Only consider loading future records if the
5538 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005539 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005540 return( 0 );
5541
Hanno Becker5f066e72018-08-16 14:56:31 +01005542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5543
5544 if( rec_epoch != ssl->in_epoch )
5545 {
5546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5547 goto exit;
5548 }
5549
5550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5551
5552 /* Double-check that the record is not too large */
5553 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5554 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5558 }
5559
5560 memcpy( ssl->in_hdr, rec, rec_len );
5561 ssl->in_left = rec_len;
5562 ssl->next_record_offset = 0;
5563
5564 ssl_free_buffered_record( ssl );
5565
5566exit:
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5568 return( 0 );
5569}
5570
5571static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5572{
5573 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5574 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005575 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005576
5577 /* Don't buffer future records outside handshakes. */
5578 if( hs == NULL )
5579 return( 0 );
5580
5581 /* Only buffer handshake records (we are only interested
5582 * in Finished messages). */
5583 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5584 return( 0 );
5585
5586 /* Don't buffer more than one future epoch record. */
5587 if( hs->buffering.future_record.data != NULL )
5588 return( 0 );
5589
Hanno Becker01315ea2018-08-21 17:22:17 +01005590 /* Don't buffer record if there's not enough buffering space remaining. */
5591 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5592 hs->buffering.total_bytes_buffered ) )
5593 {
5594 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",
5595 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5596 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005597 return( 0 );
5598 }
5599
Hanno Becker5f066e72018-08-16 14:56:31 +01005600 /* Buffer record */
5601 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5602 ssl->in_epoch + 1 ) );
5603 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5604 rec_hdr_len + ssl->in_msglen );
5605
5606 /* ssl_parse_record_header() only considers records
5607 * of the next epoch as candidates for buffering. */
5608 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005609 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005610
5611 hs->buffering.future_record.data =
5612 mbedtls_calloc( 1, hs->buffering.future_record.len );
5613 if( hs->buffering.future_record.data == NULL )
5614 {
5615 /* If we run out of RAM trying to buffer a
5616 * record from the next epoch, just ignore. */
5617 return( 0 );
5618 }
5619
Hanno Becker01315ea2018-08-21 17:22:17 +01005620 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005621
Hanno Becker01315ea2018-08-21 17:22:17 +01005622 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005623 return( 0 );
5624}
5625
5626#endif /* MBEDTLS_SSL_PROTO_DTLS */
5627
Hanno Beckere74d5562018-08-15 14:26:08 +01005628static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005629{
5630 int ret;
5631
Hanno Becker5f066e72018-08-16 14:56:31 +01005632#if defined(MBEDTLS_SSL_PROTO_DTLS)
5633 /* We might have buffered a future record; if so,
5634 * and if the epoch matches now, load it.
5635 * On success, this call will set ssl->in_left to
5636 * the length of the buffered record, so that
5637 * the calls to ssl_fetch_input() below will
5638 * essentially be no-ops. */
5639 ret = ssl_load_buffered_record( ssl );
5640 if( ret != 0 )
5641 return( ret );
5642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005643
Hanno Becker8b09b732019-05-08 12:03:28 +01005644 /* Reset in pointers to default state for TLS/DTLS records,
5645 * assuming no CID and no offset between record content and
5646 * record plaintext. */
5647 ssl_update_in_pointers( ssl );
5648
5649 /* Ensure that we have enough space available for the default form
5650 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5651 * with no space for CIDs counted in). */
5652 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5653 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005656 return( ret );
5657 }
5658
5659 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005662 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005663 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005664 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005665 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5666 {
5667 ret = ssl_buffer_future_record( ssl );
5668 if( ret != 0 )
5669 return( ret );
5670
5671 /* Fall through to handling of unexpected records */
5672 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5673 }
5674
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005675 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5676 {
5677 /* Skip unexpected record (but not whole datagram) */
5678 ssl->next_record_offset = ssl->in_msglen
Hanno Becker43395762019-05-03 14:46:38 +01005679 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005680
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5682 "(header)" ) );
5683 }
5684 else
5685 {
5686 /* Skip invalid record and the rest of the datagram */
5687 ssl->next_record_offset = 0;
5688 ssl->in_left = 0;
5689
5690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5691 "(header)" ) );
5692 }
5693
5694 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005695 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005696 }
5697#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005699 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005700
5701 /*
5702 * Read and optionally decrypt the message contents
5703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker43395762019-05-03 14:46:38 +01005705 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 return( ret );
5709 }
5710
5711 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005713 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckere65ce782017-05-22 14:47:48 +01005714 {
Hanno Becker43395762019-05-03 14:46:38 +01005715 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005716 if( ssl->next_record_offset < ssl->in_left )
5717 {
5718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5719 }
5720 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005721 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005722#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005723#if defined(MBEDTLS_SSL_PROTO_TLS)
5724 {
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005725 ssl->in_left = 0;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005726 }
5727#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005728
5729 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005732 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005733 {
5734 /* Silently discard invalid records */
Hanno Becker16e9ae22019-05-03 16:36:59 +01005735 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005736 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005737 /* Except when waiting for Finished as a bad mac here
5738 * probably means something went wrong in the handshake
5739 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5740 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5741 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5742 {
5743#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5744 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5745 {
5746 mbedtls_ssl_send_alert_message( ssl,
5747 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5748 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5749 }
5750#endif
5751 return( ret );
5752 }
5753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005755 if( ssl->conf->badmac_limit != 0 &&
5756 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5759 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005760 }
5761#endif
5762
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763 /* As above, invalid records cause
5764 * dismissal of the whole datagram. */
5765
5766 ssl->next_record_offset = 0;
5767 ssl->in_left = 0;
5768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005770 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005771 }
5772
5773 return( ret );
5774 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005775 MBEDTLS_SSL_TRANSPORT_ELSE
5776#endif /* MBEDTLS_SSL_PROTO_DTLS */
5777#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005778 {
5779 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5781 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005783 mbedtls_ssl_send_alert_message( ssl,
5784 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5785 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005786 }
5787#endif
5788 return( ret );
5789 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02005790#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005791 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005792
Simon Butcher99000142016-10-13 17:21:01 +01005793 return( 0 );
5794}
5795
5796int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5797{
5798 int ret;
5799
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005800 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005801 * Handle particular types of records
5802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005804 {
Simon Butcher99000142016-10-13 17:21:01 +01005805 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5806 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005807 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005808 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 }
5810
Hanno Beckere678eaa2018-08-21 14:57:46 +01005811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005812 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005813 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005814 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5816 ssl->in_msglen ) );
5817 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005818 }
5819
Hanno Beckere678eaa2018-08-21 14:57:46 +01005820 if( ssl->in_msg[0] != 1 )
5821 {
5822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5823 ssl->in_msg[0] ) );
5824 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5825 }
5826
5827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005828 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005829 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5830 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5831 {
5832 if( ssl->handshake == NULL )
5833 {
5834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
5835 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5836 }
5837
5838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
5839 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5840 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005841#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01005842 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005844 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005846 if( ssl->in_msglen != 2 )
5847 {
5848 /* Note: Standard allows for more than one 2 byte alert
5849 to be packed in a single message, but Mbed TLS doesn't
5850 currently support this. */
5851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
5852 ssl->in_msglen ) );
5853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5854 }
5855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00005857 ssl->in_msg[0], ssl->in_msg[1] ) );
5858
5859 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005860 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005862 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00005865 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 }
5868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005869 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5870 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
5873 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005875
5876#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
5877 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5878 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5879 {
Hanno Becker90333da2017-10-10 11:27:13 +01005880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005881 /* Will be handled when trying to parse ServerHello */
5882 return( 0 );
5883 }
5884#endif
5885
5886#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5887 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5888 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5889 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5890 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5891 {
5892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5893 /* Will be handled in mbedtls_ssl_parse_certificate() */
5894 return( 0 );
5895 }
5896#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5897
5898 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005899 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 }
5901
Hanno Beckerc76c6192017-06-06 10:03:17 +01005902#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02005903 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005904 {
Hanno Becker74dd3a72019-05-03 16:54:26 +01005905 /* Drop unexpected ApplicationData records,
5906 * except at the beginning of renegotiations */
5907 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5908 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5909#if defined(MBEDTLS_SSL_RENEGOTIATION)
5910 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5911 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005912#endif
Hanno Becker74dd3a72019-05-03 16:54:26 +01005913 )
5914 {
5915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5916 return( MBEDTLS_ERR_SSL_NON_FATAL );
5917 }
5918
5919 if( ssl->handshake != NULL &&
5920 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5921 {
5922 ssl_handshake_wrapup_free_hs_transform( ssl );
5923 }
5924 }
Hanno Beckerf65ad822019-05-08 16:26:21 +01005925#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005926
Paul Bakker5121ce52009-01-03 21:22:43 +00005927 return( 0 );
5928}
5929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005931{
5932 int ret;
5933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5935 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5936 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005937 {
5938 return( ret );
5939 }
5940
5941 return( 0 );
5942}
5943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005945 unsigned char level,
5946 unsigned char message )
5947{
5948 int ret;
5949
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005950 if( ssl == NULL || ssl->conf == NULL )
5951 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005957 ssl->out_msglen = 2;
5958 ssl->out_msg[0] = level;
5959 ssl->out_msg[1] = message;
5960
Hanno Becker67bc7c32018-08-06 11:33:50 +01005961 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005964 return( ret );
5965 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005967
5968 return( 0 );
5969}
5970
Paul Bakker5121ce52009-01-03 21:22:43 +00005971/*
5972 * Handshake functions
5973 */
Hanno Beckerb71e90a2019-02-05 13:20:55 +00005974#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02005975/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005977{
Hanno Becker8759e162017-12-27 21:34:08 +00005978 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
5983 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02005984 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
5985 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005988 ssl->state++;
5989 return( 0 );
5990 }
5991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005994}
5995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005996int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005997{
Hanno Becker8759e162017-12-27 21:34:08 +00005998 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02005999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6003 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006004 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6005 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006008 ssl->state++;
6009 return( 0 );
6010 }
6011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6013 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006014}
Gilles Peskinef9828522017-05-03 12:28:43 +02006015
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006016#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006017/* Some certificate support -> implement write and parse */
6018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006022 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023 const mbedtls_x509_crt *crt;
Hanno Becker8759e162017-12-27 21:34:08 +00006024 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6029 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard25dbeb02015-09-16 17:30:03 +02006030 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6031 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006034 ssl->state++;
6035 return( 0 );
6036 }
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006040 {
6041 if( ssl->client_auth == 0 )
6042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006044 ssl->state++;
6045 return( 0 );
6046 }
6047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006049 /*
6050 * If using SSLv3 and got no cert, send an Alert message
6051 * (otherwise an empty Certificate message will be sent).
6052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6054 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 {
6056 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006057 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6058 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6059 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 goto write_msg;
6063 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006064#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006066#endif /* MBEDTLS_SSL_CLI_C */
6067#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006068 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6073 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 }
6075 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006078 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006079
6080 /*
6081 * 0 . 0 handshake type
6082 * 1 . 3 handshake length
6083 * 4 . 6 length of all certs
6084 * 7 . 9 length of cert. 1
6085 * 10 . n-1 peer certificate
6086 * n . n+2 length of cert. 2
6087 * n+3 . ... upper level cert, etc.
6088 */
6089 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006091
Paul Bakker29087132010-03-21 21:03:34 +00006092 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006093 {
6094 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006095 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006098 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006099 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006100 }
6101
6102 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6103 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6104 ssl->out_msg[i + 2] = (unsigned char)( n );
6105
6106 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6107 i += n; crt = crt->next;
6108 }
6109
6110 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6111 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6112 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6113
6114 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6116 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006117
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006118#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006119write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006120#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006121
6122 ssl->state++;
6123
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006124 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 return( ret );
6128 }
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006131
Paul Bakkered27a042013-04-18 22:46:23 +02006132 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133}
6134
Hanno Becker285ff0c2019-01-31 07:44:03 +00006135#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker33c3dc82019-01-30 14:46:46 +00006136static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6137 unsigned char *crt_buf,
6138 size_t crt_buf_len )
6139{
6140 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6141
6142 if( peer_crt == NULL )
6143 return( -1 );
6144
6145 if( peer_crt->raw.len != crt_buf_len )
6146 return( -1 );
6147
Hanno Becker68b856d2019-02-08 14:00:04 +00006148 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006149}
Hanno Becker285ff0c2019-01-31 07:44:03 +00006150#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006151
Hanno Becker22141592019-02-05 12:38:15 +00006152static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6153{
6154 if( session->peer_cert != NULL )
6155 {
6156 mbedtls_x509_crt_free( session->peer_cert );
6157 mbedtls_free( session->peer_cert );
6158 session->peer_cert = NULL;
6159 }
6160}
6161
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006162/*
6163 * Once the certificate message is read, parse it into a cert chain and
6164 * perform basic checks, but leave actual verification to the caller
6165 */
6166static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006167{
Hanno Becker8794fd92019-02-05 12:38:45 +00006168 int ret, crt_cnt=0;
Paul Bakker23986e52011-04-24 08:57:21 +00006169 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006170 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006171
Paul Bakker5121ce52009-01-03 21:22:43 +00006172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006173 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006176 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6177 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006179 }
6180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006181 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6182 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6186 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006188 }
6189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006190 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006191
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006195 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006196
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006197 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006201 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6202 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006204 }
6205
Hanno Becker33c3dc82019-01-30 14:46:46 +00006206 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6207 i += 3;
6208
Hanno Becker33c3dc82019-01-30 14:46:46 +00006209 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 while( i < ssl->in_hslen )
6211 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006212 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006213 if ( i + 3 > ssl->in_hslen ) {
6214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006215 mbedtls_ssl_send_alert_message( ssl,
6216 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6217 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006218 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6219 }
Hanno Becker33c3dc82019-01-30 14:46:46 +00006220 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6221 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006222 if( ssl->in_msg[i] != 0 )
6223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006225 mbedtls_ssl_send_alert_message( ssl,
6226 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6227 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 }
6230
Hanno Becker33c3dc82019-01-30 14:46:46 +00006231 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006232 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6233 | (unsigned int) ssl->in_msg[i + 2];
6234 i += 3;
6235
6236 if( n < 128 || i + n > ssl->in_hslen )
6237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006239 mbedtls_ssl_send_alert_message( ssl,
6240 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6241 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243 }
6244
Hanno Becker33c3dc82019-01-30 14:46:46 +00006245 /* Check if we're handling the first CRT in the chain. */
Hanno Becker8794fd92019-02-05 12:38:45 +00006246 if( crt_cnt++ == 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006247 {
Hanno Becker68b856d2019-02-08 14:00:04 +00006248 /* During client-side renegotiation, check that the server's
6249 * end-CRTs hasn't changed compared to the initial handshake,
6250 * mitigating the triple handshake attack. On success, reuse
6251 * the original end-CRT instead of parsing it again. */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006252#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6253 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6254 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
6255 {
6256 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006257 if( ssl_check_peer_crt_unchanged( ssl,
6258 &ssl->in_msg[i],
6259 n ) != 0 )
Hanno Becker33c3dc82019-01-30 14:46:46 +00006260 {
6261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006262 mbedtls_ssl_send_alert_message( ssl,
6263 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6264 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006265 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6266 }
6267
Hanno Becker1332f352019-02-05 15:06:15 +00006268 /* Now we can safely free the original chain. */
Hanno Becker22141592019-02-05 12:38:15 +00006269 ssl_clear_peer_cert( ssl->session );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006270
Hanno Becker1332f352019-02-05 15:06:15 +00006271 /* Intentional fallthrough. */
Hanno Becker33c3dc82019-01-30 14:46:46 +00006272 }
6273#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6274
6275 /* Outside of client-side renegotiation, create a fresh X.509 CRT
6276 * instance to parse the end-CRT into. */
6277
6278 ssl->session_negotiate->peer_cert =
6279 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6280 if( ssl->session_negotiate->peer_cert == NULL )
6281 {
6282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6283 sizeof( mbedtls_x509_crt ) ) );
Hanno Beckerfd399192019-01-31 07:44:17 +00006284 mbedtls_ssl_send_alert_message( ssl,
6285 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6286 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker33c3dc82019-01-30 14:46:46 +00006287 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6288 }
6289
6290 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
6291
6292 /* Intentional fall through */
6293 }
6294
6295 /* Parse the next certificate in the chain. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
Hanno Becker33c3dc82019-01-30 14:46:46 +00006297 ssl->in_msg + i, n );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006298 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006299 {
Hanno Becker33c3dc82019-01-30 14:46:46 +00006300 case 0: /*ok*/
6301 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6302 /* Ignore certificate with an unknown algorithm: maybe a
6303 prior certificate was already trusted. */
6304 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006305
Hanno Becker33c3dc82019-01-30 14:46:46 +00006306 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6307 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6308 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006309
Hanno Becker33c3dc82019-01-30 14:46:46 +00006310 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6311 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6312 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006313
Hanno Becker33c3dc82019-01-30 14:46:46 +00006314 default:
6315 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6316 crt_parse_der_failed:
6317 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6318 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6319 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 }
6321
6322 i += n;
6323 }
6324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006326 return( 0 );
6327}
6328
Hanno Beckerb8a08572019-02-05 12:49:06 +00006329#if defined(MBEDTLS_SSL_SRV_C)
6330static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6331{
6332 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6333 return( -1 );
6334
6335#if defined(MBEDTLS_SSL_PROTO_SSL3)
6336 /*
6337 * Check if the client sent an empty certificate
6338 */
6339 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6340 {
6341 if( ssl->in_msglen == 2 &&
6342 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6343 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6344 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6345 {
6346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6347 return( 0 );
6348 }
6349
6350 return( -1 );
6351 }
6352#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6353
6354#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6355 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6356 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6357 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6358 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6359 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6360 {
6361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6362 return( 0 );
6363 }
6364
6365 return( -1 );
6366#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6367 MBEDTLS_SSL_PROTO_TLS1_2 */
6368}
6369#endif /* MBEDTLS_SSL_SRV_C */
6370
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006371int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6372{
Hanno Becker613d4902019-02-05 13:11:17 +00006373 int ret = 0;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006374 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Hanno Becker8759e162017-12-27 21:34:08 +00006375 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006376#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6377 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6378 ? ssl->handshake->sni_authmode
6379 : ssl->conf->authmode;
6380#else
6381 const int authmode = ssl->conf->authmode;
6382#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006383 void *rs_ctx = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006384
6385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6386
6387 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK ||
6388 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK ||
6389 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ||
6390 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE )
6391 {
6392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006393 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006394 }
6395
6396#if defined(MBEDTLS_SSL_SRV_C)
6397 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6398 ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6399 {
6400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006401 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006402 }
6403
6404 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6405 authmode == MBEDTLS_SSL_VERIFY_NONE )
6406 {
6407 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker613d4902019-02-05 13:11:17 +00006409 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006410 }
6411#endif
6412
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006413#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6414 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006415 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006416 {
6417 goto crt_verify;
6418 }
6419#endif
6420
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006421 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006422 {
6423 /* mbedtls_ssl_read_record may have sent an alert already. We
6424 let it decide whether to alert. */
6425 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
6426 return( ret );
6427 }
6428
Hanno Beckerb8a08572019-02-05 12:49:06 +00006429#if defined(MBEDTLS_SSL_SRV_C)
6430 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6431 {
6432 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006433
6434 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker613d4902019-02-05 13:11:17 +00006435 ret = 0;
6436 else
6437 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006438
Hanno Becker613d4902019-02-05 13:11:17 +00006439 goto exit;
Hanno Beckerb8a08572019-02-05 12:49:06 +00006440 }
6441#endif /* MBEDTLS_SSL_SRV_C */
6442
Hanno Beckera46c2872019-02-05 13:08:01 +00006443 /* In case we tried to reuse a session but it failed. */
6444 ssl_clear_peer_cert( ssl->session_negotiate );
6445
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006446 if( ( ret = ssl_parse_certificate_chain( ssl ) ) != 0 )
Hanno Becker613d4902019-02-05 13:11:17 +00006447 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006448
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006449#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6450 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006451 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006452
6453crt_verify:
6454 if( ssl->handshake->ecrs_enabled)
6455 rs_ctx = &ssl->handshake->ecrs_ctx;
6456#endif
6457
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02006458 if( authmode != MBEDTLS_SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006459 {
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006460 mbedtls_x509_crt *ca_chain;
6461 mbedtls_x509_crl *ca_crl;
6462
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006463#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006464 if( ssl->handshake->sni_ca_chain != NULL )
6465 {
6466 ca_chain = ssl->handshake->sni_ca_chain;
6467 ca_crl = ssl->handshake->sni_ca_crl;
6468 }
6469 else
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02006470#endif
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02006471 {
6472 ca_chain = ssl->conf->ca_chain;
6473 ca_crl = ssl->conf->ca_crl;
6474 }
6475
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006476 /*
6477 * Main check: verify certificate
6478 */
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006479 ret = mbedtls_x509_crt_verify_restartable(
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02006480 ssl->session_negotiate->peer_cert,
6481 ca_chain, ca_crl,
6482 ssl->conf->cert_profile,
6483 ssl->hostname,
6484 &ssl->session_negotiate->verify_result,
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006485 ssl->conf->f_vrfy, ssl->conf->p_vrfy, rs_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +00006486
6487 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006490 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006491
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006492#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6493 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard558da9c2018-06-13 12:02:12 +02006494 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006495#endif
6496
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006497 /*
6498 * Secondary checks: always done, but change 'ret' only if it was 0
6499 */
6500
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006501#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006504
6505 /* If certificate uses an EC key, make sure the curve is OK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02006507 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006508 {
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006509 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006512 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006514 }
6515 }
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02006516#endif /* MBEDTLS_ECP_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006519 ciphersuite_info,
6520 ! ssl->conf->endpoint,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01006521 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006524 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006525 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006526 }
6527
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006528 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6529 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6530 * with details encoded in the verification flags. All other kinds
6531 * of error codes, including those from the user provided f_vrfy
6532 * functions, are treated as fatal and lead to a failure of
6533 * ssl_parse_certificate even if verification was optional. */
6534 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6535 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6536 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6537 {
Paul Bakker5121ce52009-01-03 21:22:43 +00006538 ret = 0;
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006539 }
6540
6541 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
6542 {
6543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6544 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6545 }
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006546
6547 if( ret != 0 )
6548 {
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006549 uint8_t alert;
6550
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006551 /* The certificate may have been rejected for several reasons.
6552 Pick one and send the corresponding alert. Which alert to send
6553 may be a subject of debate in some cases. */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006554 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6555 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6556 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6557 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6558 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6559 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6560 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6561 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6562 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6563 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6564 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6565 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6566 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6567 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6568 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6569 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6570 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6571 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6572 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6573 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine8498cb32017-05-10 15:39:40 +02006574 else
6575 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006576 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6577 alert );
6578 }
Hanno Becker39ae8cd2017-05-08 16:31:14 +01006579
Hanno Beckere6706e62017-05-15 16:05:15 +01006580#if defined(MBEDTLS_DEBUG_C)
6581 if( ssl->session_negotiate->verify_result != 0 )
6582 {
6583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6584 ssl->session_negotiate->verify_result ) );
6585 }
6586 else
6587 {
6588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6589 }
6590#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 }
6592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006594
Hanno Becker613d4902019-02-05 13:11:17 +00006595exit:
6596
6597 ssl->state++;
Paul Bakker5121ce52009-01-03 21:22:43 +00006598 return( ret );
6599}
Hanno Beckerb71e90a2019-02-05 13:20:55 +00006600#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006603{
6604 int ret;
6605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 ssl->out_msglen = 1;
6610 ssl->out_msg[0] = 1;
6611
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 ssl->state++;
6613
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006614 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006615 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 return( ret );
6618 }
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006621
6622 return( 0 );
6623}
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006626{
6627 int ret;
6628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006630
Hanno Becker327c93b2018-08-15 13:56:18 +01006631 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006634 return( ret );
6635 }
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00006638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006640 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6641 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006643 }
6644
Hanno Beckere678eaa2018-08-21 14:57:46 +01006645 /* CCS records are only accepted if they have length 1 and content '1',
6646 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006647
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006648 /*
6649 * Switch to our negotiated transform and session parameters for inbound
6650 * data.
6651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006653 ssl->transform_in = ssl->transform_negotiate;
6654 ssl->session_in = ssl->session_negotiate;
6655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006657 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006659#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006660 ssl_dtls_replay_reset( ssl );
6661#endif
6662
6663 /* Increment epoch */
6664 if( ++ssl->in_epoch == 0 )
6665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006667 /* This is highly unlikely to happen for legitimate reasons, so
6668 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006670 }
6671 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006672 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006673#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02006674#if defined(MBEDTLS_SSL_PROTO_TLS)
6675 {
6676 memset( ssl->in_ctr, 0, 8 );
6677 }
6678#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006679
Hanno Beckerf5970a02019-05-08 09:38:41 +01006680 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006682#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
6683 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006685 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006688 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6689 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02006691 }
6692 }
6693#endif
6694
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 ssl->state++;
6696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006698
6699 return( 0 );
6700}
6701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006702void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
6703 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00006704{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02006705 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01006706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6708 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6709 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00006710 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00006711 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006713#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6714#if defined(MBEDTLS_SHA512_C)
6715 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006716 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
6717 else
6718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006719#if defined(MBEDTLS_SHA256_C)
6720 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00006721 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006722 else
6723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006724#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006727 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006728 }
Paul Bakker380da532012-04-18 16:10:25 +00006729}
Paul Bakkerf7abd422013-04-16 13:15:56 +02006730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006731void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006732{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006733#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6734 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006735 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
6736 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006737#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006738#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6739#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006740 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006742#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006743 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006744#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006745#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02006746}
6747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006748static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006749 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006751#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6752 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006753 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6754 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006756#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6757#if defined(MBEDTLS_SHA256_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006758 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006759#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006760#if defined(MBEDTLS_SHA512_C)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006761 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01006762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006763#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006764}
6765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006766#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
6767 defined(MBEDTLS_SSL_PROTO_TLS1_1)
6768static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006769 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006770{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006771 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
6772 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006773}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006774#endif
Paul Bakker380da532012-04-18 16:10:25 +00006775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6777#if defined(MBEDTLS_SHA256_C)
6778static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006779 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006780{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006781 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006782}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006783#endif
Paul Bakker380da532012-04-18 16:10:25 +00006784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785#if defined(MBEDTLS_SHA512_C)
6786static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006787 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00006788{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006789 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00006790}
Paul Bakker769075d2012-11-24 11:26:46 +01006791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006792#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00006793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006795static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006797{
Paul Bakker3c2122f2013-06-24 19:03:14 +02006798 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006799 mbedtls_md5_context md5;
6800 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006801
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 unsigned char padbuf[48];
6803 unsigned char md5sum[16];
6804 unsigned char sha1sum[20];
6805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006806 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006807 if( !session )
6808 session = ssl->session;
6809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006811
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006812 mbedtls_md5_init( &md5 );
6813 mbedtls_sha1_init( &sha1 );
6814
6815 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6816 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006817
6818 /*
6819 * SSLv3:
6820 * hash =
6821 * MD5( master + pad2 +
6822 * MD5( handshake + sender + master + pad1 ) )
6823 * + SHA1( master + pad2 +
6824 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006825 */
6826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006828 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6829 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006830#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006832#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006833 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6834 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006837 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02006838 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00006839
Paul Bakker1ef83d62012-04-11 12:09:53 +00006840 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006841
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006842 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
6843 mbedtls_md5_update_ret( &md5, session->master, 48 );
6844 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6845 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006846
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006847 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
6848 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6849 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
6850 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00006851
Paul Bakker1ef83d62012-04-11 12:09:53 +00006852 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006854 mbedtls_md5_starts_ret( &md5 );
6855 mbedtls_md5_update_ret( &md5, session->master, 48 );
6856 mbedtls_md5_update_ret( &md5, padbuf, 48 );
6857 mbedtls_md5_update_ret( &md5, md5sum, 16 );
6858 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006859
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006860 mbedtls_sha1_starts_ret( &sha1 );
6861 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
6862 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
6863 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
6864 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006868 mbedtls_md5_free( &md5 );
6869 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006870
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006871 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
6872 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
6873 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006877#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00006880static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006881 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00006882{
Paul Bakker1ef83d62012-04-11 12:09:53 +00006883 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006884 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006885 mbedtls_md5_context md5;
6886 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006887 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00006888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006889 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006890 if( !session )
6891 session = ssl->session;
6892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006894
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006895 mbedtls_md5_init( &md5 );
6896 mbedtls_sha1_init( &sha1 );
6897
6898 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
6899 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006900
Paul Bakker1ef83d62012-04-11 12:09:53 +00006901 /*
6902 * TLSv1:
6903 * hash = PRF( master, finished_label,
6904 * MD5( handshake ) + SHA1( handshake ) )[0..11]
6905 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006907#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006908 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
6909 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006910#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006912#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006913 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
6914 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006915#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006917 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006918 ? "client finished"
6919 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006920
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006921 mbedtls_md5_finish_ret( &md5, padbuf );
6922 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006923
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006924 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006925 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006927 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006928
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006929 mbedtls_md5_free( &md5 );
6930 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006931
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006932 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006935}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006936#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006938#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6939#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006940static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006941 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00006942{
6943 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006944 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006945 mbedtls_sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00006946 unsigned char padbuf[32];
6947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006948 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006949 if( !session )
6950 session = ssl->session;
6951
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006952 mbedtls_sha256_init( &sha256 );
6953
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006955
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02006956 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006957
6958 /*
6959 * TLSv1.2:
6960 * hash = PRF( master, finished_label,
6961 * Hash( handshake ) )[0.11]
6962 */
6963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006964#if !defined(MBEDTLS_SHA256_ALT)
6965 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006966 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02006967#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00006968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006969 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02006970 ? "client finished"
6971 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00006972
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01006973 mbedtls_sha256_finish_ret( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006974
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02006975 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00006976 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006978 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006979
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006980 mbedtls_sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006981
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006982 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00006985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006986#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00006987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006988#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00006989static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006990 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00006991{
6992 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02006993 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02006994 mbedtls_sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00006995 unsigned char padbuf[48];
6996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006997 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00006998 if( !session )
6999 session = ssl->session;
7000
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007001 mbedtls_sha512_init( &sha512 );
7002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007004
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007005 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007006
7007 /*
7008 * TLSv1.2:
7009 * hash = PRF( master, finished_label,
7010 * Hash( handshake ) )[0.11]
7011 */
7012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007013#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007014 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7015 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007016#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007018 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007019 ? "client finished"
7020 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00007021
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007022 mbedtls_sha512_finish_ret( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007023
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007024 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007025 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007027 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007028
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007029 mbedtls_sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007030
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007031 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035#endif /* MBEDTLS_SHA512_C */
7036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007041
7042 /*
7043 * Free our handshake params
7044 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007045 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007046 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007047 ssl->handshake = NULL;
7048
7049 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007050 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007051 */
7052 if( ssl->transform )
7053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007054 mbedtls_ssl_transform_free( ssl->transform );
7055 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007056 }
7057 ssl->transform = ssl->transform_negotiate;
7058 ssl->transform_negotiate = NULL;
7059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007061}
7062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007064{
7065 int resume = ssl->handshake->resume;
7066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007069#if defined(MBEDTLS_SSL_RENEGOTIATION)
7070 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007073 ssl->renego_records_seen = 0;
7074 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007075#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007076
7077 /*
7078 * Free the previous session and switch in the current one
7079 */
Paul Bakker0a597072012-09-25 21:55:46 +00007080 if( ssl->session )
7081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007082#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007083 /* RFC 7366 3.1: keep the EtM state */
7084 ssl->session_negotiate->encrypt_then_mac =
7085 ssl->session->encrypt_then_mac;
7086#endif
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 mbedtls_ssl_session_free( ssl->session );
7089 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007090 }
7091 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007092 ssl->session_negotiate = NULL;
7093
Paul Bakker0a597072012-09-25 21:55:46 +00007094 /*
7095 * Add cache entry
7096 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007097 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007098 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007099 resume == 0 )
7100 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007101 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007103 }
Paul Bakker0a597072012-09-25 21:55:46 +00007104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007106 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007107 ssl->handshake->flight != NULL )
7108 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007109 /* Cancel handshake timer */
7110 ssl_set_timer( ssl, 0 );
7111
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007112 /* Keep last flight around in case we need to resend it:
7113 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007115 }
7116 else
7117#endif
7118 ssl_handshake_wrapup_free_hs_transform( ssl );
7119
Paul Bakker48916f92012-09-16 19:57:18 +00007120 ssl->state++;
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007123}
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007126{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007127 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007130
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007131 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007132
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007133 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007134
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007135 /*
7136 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7137 * may define some other value. Currently (early 2016), no defined
7138 * ciphersuite does this (and this is unlikely to change as activity has
7139 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007144 ssl->verify_data_len = hash_len;
7145 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007146#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007147
Paul Bakker5121ce52009-01-03 21:22:43 +00007148 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7150 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007151
7152 /*
7153 * In case of session resuming, invert the client and server
7154 * ChangeCipherSpec messages order.
7155 */
Paul Bakker0a597072012-09-25 21:55:46 +00007156 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007159 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007163 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007166 }
7167 else
7168 ssl->state++;
7169
Paul Bakker48916f92012-09-16 19:57:18 +00007170 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007171 * Switch to our negotiated transform and session parameters for outbound
7172 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007177 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007178 {
7179 unsigned char i;
7180
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007181 /* Remember current epoch settings for resending */
7182 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007183 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007184
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007185 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007186 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007187
7188 /* Increment epoch */
7189 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007190 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007191 break;
7192
7193 /* The loop goes to its end iff the counter is wrapping */
7194 if( i == 0 )
7195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7197 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007198 }
7199 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007200 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007202#if defined(MBEDTLS_SSL_PROTO_TLS)
7203 {
7204 memset( ssl->cur_out_ctr, 0, 8 );
7205 }
7206#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007207
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007208 ssl->transform_out = ssl->transform_negotiate;
7209 ssl->session_out = ssl->session_negotiate;
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7212 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7217 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007218 }
7219 }
7220#endif
7221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007223 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007225#endif
7226
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007227 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007228 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007230 return( ret );
7231 }
7232
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007234 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007235 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7236 {
7237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7238 return( ret );
7239 }
7240#endif
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007243
7244 return( 0 );
7245}
7246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007248#define SSL_MAX_HASH_LEN 36
7249#else
7250#define SSL_MAX_HASH_LEN 12
7251#endif
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007254{
Paul Bakker23986e52011-04-24 08:57:21 +00007255 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007256 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007257 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007260
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007261 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007262
Hanno Becker327c93b2018-08-15 13:56:18 +01007263 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007266 return( ret );
7267 }
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007272 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7273 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007275 }
7276
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007277 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_PROTO_SSL3)
7279 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007280 hash_len = 36;
7281 else
7282#endif
7283 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7286 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7290 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007292 }
7293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007294 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007295 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007298 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7299 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007301 }
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007304 ssl->verify_data_len = hash_len;
7305 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007306#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007307
Paul Bakker0a597072012-09-25 21:55:46 +00007308 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007311 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007315 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007318 }
7319 else
7320 ssl->state++;
7321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007323 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007324 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007325#endif
7326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
7329 return( 0 );
7330}
7331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7337 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7338 mbedtls_md5_init( &handshake->fin_md5 );
7339 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007340 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7341 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007342#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7344#if defined(MBEDTLS_SHA256_C)
7345 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007346 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007347#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#if defined(MBEDTLS_SHA512_C)
7349 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007350 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007351#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007352#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007353
7354 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007355
7356#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7357 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7358 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7359#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361#if defined(MBEDTLS_DHM_C)
7362 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007364#if defined(MBEDTLS_ECDH_C)
7365 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007366#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007367#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007368 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007369#if defined(MBEDTLS_SSL_CLI_C)
7370 handshake->ecjpake_cache = NULL;
7371 handshake->ecjpake_cache_len = 0;
7372#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007373#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007374
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007375#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007376 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007377#endif
7378
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007379#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7380 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7381#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007382}
7383
Hanno Becker611a83b2018-01-03 14:27:32 +00007384void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7389 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007390
Hanno Becker92231322018-01-03 15:32:51 +00007391#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 mbedtls_md_init( &transform->md_ctx_enc );
7393 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +00007394#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007395}
7396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007398{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007400}
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007403{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007404 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007405 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007407 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007409 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007410 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007411
7412 /*
7413 * Either the pointers are now NULL or cleared properly and can be freed.
7414 * Now allocate missing structures.
7415 */
7416 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007417 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007418 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007419 }
Paul Bakker48916f92012-09-16 19:57:18 +00007420
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007421 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007422 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007423 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007424 }
Paul Bakker48916f92012-09-16 19:57:18 +00007425
Paul Bakker82788fb2014-10-20 13:59:19 +02007426 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007427 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007428 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007429 }
Paul Bakker48916f92012-09-16 19:57:18 +00007430
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007431 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007432 if( ssl->handshake == NULL ||
7433 ssl->transform_negotiate == NULL ||
7434 ssl->session_negotiate == NULL )
7435 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 mbedtls_free( ssl->handshake );
7439 mbedtls_free( ssl->transform_negotiate );
7440 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007441
7442 ssl->handshake = NULL;
7443 ssl->transform_negotiate = NULL;
7444 ssl->session_negotiate = NULL;
7445
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007446 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007447 }
7448
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007449 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Becker611a83b2018-01-03 14:27:32 +00007451 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007452 ssl_handshake_params_init( ssl->handshake );
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02007455 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007456 {
7457 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007458
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007459 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7460 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7461 else
7462 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007463
7464 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007465 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007466#endif
7467
Paul Bakker48916f92012-09-16 19:57:18 +00007468 return( 0 );
7469}
7470
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007471#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007472/* Dummy cookie callbacks for defaults */
7473static int ssl_cookie_write_dummy( void *ctx,
7474 unsigned char **p, unsigned char *end,
7475 const unsigned char *cli_id, size_t cli_id_len )
7476{
7477 ((void) ctx);
7478 ((void) p);
7479 ((void) end);
7480 ((void) cli_id);
7481 ((void) cli_id_len);
7482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007484}
7485
7486static int ssl_cookie_check_dummy( void *ctx,
7487 const unsigned char *cookie, size_t cookie_len,
7488 const unsigned char *cli_id, size_t cli_id_len )
7489{
7490 ((void) ctx);
7491 ((void) cookie);
7492 ((void) cookie_len);
7493 ((void) cli_id);
7494 ((void) cli_id_len);
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007497}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007498#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007499
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007500/* Once ssl->out_hdr as the address of the beginning of the
7501 * next outgoing record is set, deduce the other pointers.
7502 *
7503 * Note: For TLS, we save the implicit record sequence number
7504 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7505 * and the caller has to make sure there's space for this.
7506 */
7507
7508static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7509 mbedtls_ssl_transform *transform )
7510{
7511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007512 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007513 {
7514 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007515#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007516 ssl->out_cid = ssl->out_ctr + 8;
7517 ssl->out_len = ssl->out_cid;
7518 if( transform != NULL )
7519 ssl->out_len += transform->out_cid_len;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007520#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007521 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007522#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007523 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007524 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007525 MBEDTLS_SSL_TRANSPORT_ELSE
7526#endif /* MBEDTLS_SSL_PROTO_DTLS */
7527#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007528 {
7529 ssl->out_ctr = ssl->out_hdr - 8;
7530 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007532 ssl->out_cid = ssl->out_len;
7533#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007534 ssl->out_iv = ssl->out_hdr + 5;
7535 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007536#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007537
7538 /* Adjust out_msg to make space for explicit IV, if used. */
7539 if( transform != NULL &&
7540 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7541 {
7542 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7543 }
7544 else
7545 ssl->out_msg = ssl->out_iv;
7546}
7547
7548/* Once ssl->in_hdr as the address of the beginning of the
7549 * next incoming record is set, deduce the other pointers.
7550 *
7551 * Note: For TLS, we save the implicit record sequence number
7552 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7553 * and the caller has to make sure there's space for this.
7554 */
7555
Hanno Beckerf5970a02019-05-08 09:38:41 +01007556static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007557{
Hanno Beckerf5970a02019-05-08 09:38:41 +01007558 /* This function sets the pointers to match the case
7559 * of unprotected TLS/DTLS records, with both ssl->in_iv
7560 * and ssl->in_msg pointing to the beginning of the record
7561 * content.
7562 *
7563 * When decrypting a protected record, ssl->in_msg
7564 * will be shifted to point to the beginning of the
7565 * record plaintext.
7566 */
7567
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007568#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007569 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007570 {
Hanno Becker70e79282019-05-03 14:34:53 +01007571 /* This sets the header pointers to match records
7572 * without CID. When we receive a record containing
7573 * a CID, the fields are shifted accordingly in
7574 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007575 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007576#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker70e79282019-05-03 14:34:53 +01007577 ssl->in_cid = ssl->in_ctr + 8;
7578 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera5a2b082019-05-15 14:03:01 +01007579#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007580 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker70e79282019-05-03 14:34:53 +01007582 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007583 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007584 MBEDTLS_SSL_TRANSPORT_ELSE
7585#endif /* MBEDTLS_SSL_PROTO_DTLS */
7586#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007587 {
7588 ssl->in_ctr = ssl->in_hdr - 8;
7589 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera5a2b082019-05-15 14:03:01 +01007590#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker9bf10ea2019-05-08 16:43:21 +01007591 ssl->in_cid = ssl->in_len;
7592#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007593 ssl->in_iv = ssl->in_hdr + 5;
7594 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007595#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007596
Hanno Beckerf5970a02019-05-08 09:38:41 +01007597 /* This will be adjusted at record decryption time. */
7598 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007599}
7600
Paul Bakker5121ce52009-01-03 21:22:43 +00007601/*
7602 * Initialize an SSL context
7603 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02007604void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
7605{
7606 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
7607}
7608
7609/*
7610 * Setup an SSL context
7611 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007612
7613static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
7614{
7615 /* Set the incoming and outgoing record pointers. */
7616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007617 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007618 {
7619 ssl->out_hdr = ssl->out_buf;
7620 ssl->in_hdr = ssl->in_buf;
7621 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007622 MBEDTLS_SSL_TRANSPORT_ELSE
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007623#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007624#if defined(MBEDTLS_SSL_PROTO_TLS)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007625 {
7626 ssl->out_hdr = ssl->out_buf + 8;
7627 ssl->in_hdr = ssl->in_buf + 8;
7628 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02007629#endif /* MBEDTLS_SSL_PROTO_TLS */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007630
7631 /* Derive other internal pointers. */
7632 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Beckerf5970a02019-05-08 09:38:41 +01007633 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007634}
7635
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007636int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02007637 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00007638{
Paul Bakker48916f92012-09-16 19:57:18 +00007639 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00007640
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02007641 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00007642
7643 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01007644 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00007645 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02007646
7647 /* Set to NULL in case of an error condition */
7648 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02007649
Angus Grattond8213d02016-05-25 20:56:48 +10007650 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
7651 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00007652 {
Angus Grattond8213d02016-05-25 20:56:48 +10007653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007654 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007655 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10007656 }
7657
7658 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
7659 if( ssl->out_buf == NULL )
7660 {
7661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02007662 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02007663 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007664 }
7665
Hanno Becker2a43f6f2018-08-10 11:12:52 +01007666 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02007667
Paul Bakker48916f92012-09-16 19:57:18 +00007668 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02007669 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00007670
7671 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02007672
7673error:
7674 mbedtls_free( ssl->in_buf );
7675 mbedtls_free( ssl->out_buf );
7676
7677 ssl->conf = NULL;
7678
7679 ssl->in_buf = NULL;
7680 ssl->out_buf = NULL;
7681
7682 ssl->in_hdr = NULL;
7683 ssl->in_ctr = NULL;
7684 ssl->in_len = NULL;
7685 ssl->in_iv = NULL;
7686 ssl->in_msg = NULL;
7687
7688 ssl->out_hdr = NULL;
7689 ssl->out_ctr = NULL;
7690 ssl->out_len = NULL;
7691 ssl->out_iv = NULL;
7692 ssl->out_msg = NULL;
7693
k-stachowiak9f7798e2018-07-31 16:52:32 +02007694 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007695}
7696
7697/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00007698 * Reset an initialized and used SSL context for re-use while retaining
7699 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007700 *
7701 * If partial is non-zero, keep data in the input buffer and client ID.
7702 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00007703 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007704static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00007705{
Paul Bakker48916f92012-09-16 19:57:18 +00007706 int ret;
7707
Hanno Becker7e772132018-08-10 12:38:21 +01007708#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
7709 !defined(MBEDTLS_SSL_SRV_C)
7710 ((void) partial);
7711#endif
7712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007714
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007715 /* Cancel any possibly running timer */
7716 ssl_set_timer( ssl, 0 );
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718#if defined(MBEDTLS_SSL_RENEGOTIATION)
7719 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007720 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00007721
7722 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
7724 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00007727
Paul Bakker7eb013f2011-10-06 12:37:39 +00007728 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01007729 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007730
7731 ssl->in_msgtype = 0;
7732 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007734 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007735 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02007736#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02007738 ssl_dtls_replay_reset( ssl );
7739#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007740
7741 ssl->in_hslen = 0;
7742 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01007743
7744 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007745
7746 ssl->out_msgtype = 0;
7747 ssl->out_msglen = 0;
7748 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
7750 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01007751 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01007752#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00007753
Hanno Becker19859472018-08-06 09:40:20 +01007754 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
7755
Paul Bakker48916f92012-09-16 19:57:18 +00007756 ssl->transform_in = NULL;
7757 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00007758
Hanno Becker78640902018-08-13 16:35:15 +01007759 ssl->session_in = NULL;
7760 ssl->session_out = NULL;
7761
Angus Grattond8213d02016-05-25 20:56:48 +10007762 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007763
7764#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007765 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007766#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
7767 {
7768 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10007769 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01007770 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7773 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00007774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
7776 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
7779 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007780 }
Paul Bakker05ef8352012-05-08 09:17:57 +00007781 }
7782#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00007783
Paul Bakker48916f92012-09-16 19:57:18 +00007784 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00007785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 mbedtls_ssl_transform_free( ssl->transform );
7787 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007788 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00007789 }
Paul Bakker48916f92012-09-16 19:57:18 +00007790
Paul Bakkerc0463502013-02-14 11:19:38 +01007791 if( ssl->session )
7792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793 mbedtls_ssl_session_free( ssl->session );
7794 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01007795 ssl->session = NULL;
7796 }
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02007799 ssl->alpn_chosen = NULL;
7800#endif
7801
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007802#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01007803#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007804 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01007805#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007806 {
7807 mbedtls_free( ssl->cli_id );
7808 ssl->cli_id = NULL;
7809 ssl->cli_id_len = 0;
7810 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02007811#endif
7812
Paul Bakker48916f92012-09-16 19:57:18 +00007813 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
7814 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00007815
7816 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00007817}
7818
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02007819/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02007820 * Reset an initialized and used SSL context for re-use while retaining
7821 * all application-set variables, function pointers and data.
7822 */
7823int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
7824{
7825 return( ssl_session_reset_int( ssl, 0 ) );
7826}
7827
7828/*
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 * SSL set accessors
7830 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007831void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00007832{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007833 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00007834}
7835
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02007836void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007837{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007838 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01007839}
7840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007842void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007843{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007844 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02007845}
7846#endif
7847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007849void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007850{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007851 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02007852}
7853#endif
7854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01007856
Hanno Becker1841b0a2018-08-24 11:13:57 +01007857void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
7858 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01007859{
7860 ssl->disable_datagram_packing = !allow_packing;
7861}
7862
7863void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
7864 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007865{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007866 conf->hs_timeout_min = min;
7867 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02007868}
7869#endif
7870
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007871void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00007872{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007873 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00007874}
7875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007877void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02007878 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007879 void *p_vrfy )
7880{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007881 conf->f_vrfy = f_vrfy;
7882 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00007885
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007886void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00007887 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00007888 void *p_rng )
7889{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01007890 conf->f_rng = f_rng;
7891 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00007892}
7893
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007894void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02007895 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00007896 void *p_dbg )
7897{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007898 conf->f_dbg = f_dbg;
7899 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00007900}
7901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007903 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00007904 mbedtls_ssl_send_t *f_send,
7905 mbedtls_ssl_recv_t *f_recv,
7906 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007907{
7908 ssl->p_bio = p_bio;
7909 ssl->f_send = f_send;
7910 ssl->f_recv = f_recv;
7911 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007912}
7913
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02007914#if defined(MBEDTLS_SSL_PROTO_DTLS)
7915void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
7916{
7917 ssl->mtu = mtu;
7918}
7919#endif
7920
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007921void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01007922{
7923 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02007924}
7925
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007926void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
7927 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00007928 mbedtls_ssl_set_timer_t *f_set_timer,
7929 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007930{
7931 ssl->p_timer = p_timer;
7932 ssl->f_set_timer = f_set_timer;
7933 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007934
7935 /* Make sure we start with no timer running */
7936 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02007937}
7938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007940void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007941 void *p_cache,
7942 int (*f_get_cache)(void *, mbedtls_ssl_session *),
7943 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007944{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007945 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007946 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007947 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00007948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951#if defined(MBEDTLS_SSL_CLI_C)
7952int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00007953{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007954 int ret;
7955
7956 if( ssl == NULL ||
7957 session == NULL ||
7958 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007959 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007962 }
7963
7964 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
7965 return( ret );
7966
Paul Bakker0a597072012-09-25 21:55:46 +00007967 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02007968
7969 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007970}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00007972
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007973void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007974 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00007975{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007976 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
7977 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
7978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
7979 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007980}
7981
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02007982void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007983 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007984 int major, int minor )
7985{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007987 return;
7988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007989 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02007990 return;
7991
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02007992 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00007993}
7994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007995#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007996void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01007997 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02007998{
7999 conf->cert_profile = profile;
8000}
8001
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008002/* Append a new keycert entry to a (possibly empty) list */
8003static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8004 mbedtls_x509_crt *cert,
8005 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008006{
niisato8ee24222018-06-25 19:05:48 +09008007 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008008
niisato8ee24222018-06-25 19:05:48 +09008009 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8010 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008011 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008012
niisato8ee24222018-06-25 19:05:48 +09008013 new_cert->cert = cert;
8014 new_cert->key = key;
8015 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008016
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008017 /* Update head is the list was null, else add to the end */
8018 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008019 {
niisato8ee24222018-06-25 19:05:48 +09008020 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008021 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008022 else
8023 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008024 mbedtls_ssl_key_cert *cur = *head;
8025 while( cur->next != NULL )
8026 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008027 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008028 }
8029
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008030 return( 0 );
8031}
8032
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008033int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008034 mbedtls_x509_crt *own_cert,
8035 mbedtls_pk_context *pk_key )
8036{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008037 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008038}
8039
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008040void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008041 mbedtls_x509_crt *ca_chain,
8042 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008043{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008044 conf->ca_chain = ca_chain;
8045 conf->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00008046}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008048
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008049#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8050int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8051 mbedtls_x509_crt *own_cert,
8052 mbedtls_pk_context *pk_key )
8053{
8054 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8055 own_cert, pk_key ) );
8056}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008057
8058void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8059 mbedtls_x509_crt *ca_chain,
8060 mbedtls_x509_crl *ca_crl )
8061{
8062 ssl->handshake->sni_ca_chain = ca_chain;
8063 ssl->handshake->sni_ca_crl = ca_crl;
8064}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008065
8066void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8067 int authmode )
8068{
8069 ssl->handshake->sni_authmode = authmode;
8070}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008071#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8072
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008073#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008074/*
8075 * Set EC J-PAKE password for current handshake
8076 */
8077int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8078 const unsigned char *pw,
8079 size_t pw_len )
8080{
8081 mbedtls_ecjpake_role role;
8082
Janos Follath8eb64132016-06-03 15:40:57 +01008083 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008084 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8085
8086 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8087 role = MBEDTLS_ECJPAKE_SERVER;
8088 else
8089 role = MBEDTLS_ECJPAKE_CLIENT;
8090
8091 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8092 role,
8093 MBEDTLS_MD_SHA256,
8094 MBEDTLS_ECP_DP_SECP256R1,
8095 pw, pw_len ) );
8096}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008097#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008100int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008101 const unsigned char *psk, size_t psk_len,
8102 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008103{
Paul Bakker6db455e2013-09-18 17:29:31 +02008104 if( psk == NULL || psk_identity == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008105 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker6db455e2013-09-18 17:29:31 +02008106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008107 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8108 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01008109
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008110 /* Identity len will be encoded on two bytes */
8111 if( ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008112 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008113 {
8114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8115 }
8116
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008117 if( conf->psk != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02008118 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008119 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008120
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008121 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008122 conf->psk = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008123 conf->psk_len = 0;
8124 }
8125 if( conf->psk_identity != NULL )
8126 {
8127 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard173c7902015-10-20 19:56:45 +02008128 conf->psk_identity = NULL;
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008129 conf->psk_identity_len = 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02008130 }
8131
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008132 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ||
8133 ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05008134 {
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008135 mbedtls_free( conf->psk );
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008136 mbedtls_free( conf->psk_identity );
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008137 conf->psk = NULL;
Manuel Pégourié-Gonnard24417f02015-09-28 18:09:45 +02008138 conf->psk_identity = NULL;
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008139 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Mansour Moufidf81088b2015-02-17 13:10:21 -05008140 }
Paul Bakker6db455e2013-09-18 17:29:31 +02008141
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008142 conf->psk_len = psk_len;
8143 conf->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02008144
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008145 memcpy( conf->psk, psk, conf->psk_len );
8146 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02008147
8148 return( 0 );
8149}
8150
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008151int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8152 const unsigned char *psk, size_t psk_len )
8153{
8154 if( psk == NULL || ssl->handshake == NULL )
8155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8156
8157 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8158 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8159
8160 if( ssl->handshake->psk != NULL )
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008161 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008162 mbedtls_platform_zeroize( ssl->handshake->psk,
8163 ssl->handshake->psk_len );
Simon Butcher5b8d1d62015-10-04 22:06:51 +01008164 mbedtls_free( ssl->handshake->psk );
Andres Amaya Garciabbafd342017-07-05 14:25:21 +01008165 ssl->handshake->psk_len = 0;
Andres Amaya Garciaa0049882017-06-26 11:35:17 +01008166 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008167
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008168 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008169 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008170
8171 ssl->handshake->psk_len = psk_len;
8172 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8173
8174 return( 0 );
8175}
8176
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008177void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008179 size_t),
8180 void *p_psk )
8181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008182 conf->f_psk = f_psk;
8183 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008186
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008187#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008188
8189#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008190int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008191{
8192 int ret;
8193
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008194 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8195 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8196 {
8197 mbedtls_mpi_free( &conf->dhm_P );
8198 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008199 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008201
8202 return( 0 );
8203}
Hanno Becker470a8c42017-10-04 15:28:46 +01008204#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008205
Hanno Beckera90658f2017-10-04 15:29:08 +01008206int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8207 const unsigned char *dhm_P, size_t P_len,
8208 const unsigned char *dhm_G, size_t G_len )
8209{
8210 int ret;
8211
8212 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8213 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8214 {
8215 mbedtls_mpi_free( &conf->dhm_P );
8216 mbedtls_mpi_free( &conf->dhm_G );
8217 return( ret );
8218 }
8219
8220 return( 0 );
8221}
Paul Bakker5121ce52009-01-03 21:22:43 +00008222
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008223int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008224{
8225 int ret;
8226
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008227 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8228 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8229 {
8230 mbedtls_mpi_free( &conf->dhm_P );
8231 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008232 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008233 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008234
8235 return( 0 );
8236}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008237#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008238
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008239#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8240/*
8241 * Set the minimum length for Diffie-Hellman parameters
8242 */
8243void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8244 unsigned int bitlen )
8245{
8246 conf->dhm_min_bitlen = bitlen;
8247}
8248#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8249
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008250#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008251/*
8252 * Set allowed/preferred hashes for handshake signatures
8253 */
8254void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8255 const int *hashes )
8256{
8257 conf->sig_hashes = hashes;
8258}
Hanno Becker947194e2017-04-07 13:25:49 +01008259#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008260
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008261#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008262/*
8263 * Set the allowed elliptic curves
8264 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008265void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008266 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008268 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008269}
Hanno Becker947194e2017-04-07 13:25:49 +01008270#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008271
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008272#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008273int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008274{
Hanno Becker947194e2017-04-07 13:25:49 +01008275 /* Initialize to suppress unnecessary compiler warning */
8276 size_t hostname_len = 0;
8277
8278 /* Check if new hostname is valid before
8279 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008280 if( hostname != NULL )
8281 {
8282 hostname_len = strlen( hostname );
8283
8284 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8285 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8286 }
8287
8288 /* Now it's clear that we will overwrite the old hostname,
8289 * so we can free it safely */
8290
8291 if( ssl->hostname != NULL )
8292 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008293 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008294 mbedtls_free( ssl->hostname );
8295 }
8296
8297 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008298
Paul Bakker5121ce52009-01-03 21:22:43 +00008299 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008300 {
8301 ssl->hostname = NULL;
8302 }
8303 else
8304 {
8305 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008306 if( ssl->hostname == NULL )
8307 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008308
Hanno Becker947194e2017-04-07 13:25:49 +01008309 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008310
Hanno Becker947194e2017-04-07 13:25:49 +01008311 ssl->hostname[hostname_len] = '\0';
8312 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008313
8314 return( 0 );
8315}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008316#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008317
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008318#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008319void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008321 const unsigned char *, size_t),
8322 void *p_sni )
8323{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008324 conf->f_sni = f_sni;
8325 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008329#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008330int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008331{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008332 size_t cur_len, tot_len;
8333 const char **p;
8334
8335 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008336 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8337 * MUST NOT be truncated."
8338 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008339 */
8340 tot_len = 0;
8341 for( p = protos; *p != NULL; p++ )
8342 {
8343 cur_len = strlen( *p );
8344 tot_len += cur_len;
8345
8346 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008348 }
8349
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008350 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008351
8352 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008353}
8354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008356{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008357 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008358}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008360
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008361void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008362{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008363 conf->max_major_ver = major;
8364 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008365}
8366
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008367void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008368{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008369 conf->min_major_ver = major;
8370 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008371}
8372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008374void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008375{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008376 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008377}
8378#endif
8379
Janos Follath088ce432017-04-10 12:42:31 +01008380#if defined(MBEDTLS_SSL_SRV_C)
8381void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8382 char cert_req_ca_list )
8383{
8384 conf->cert_req_ca_list = cert_req_ca_list;
8385}
8386#endif
8387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008388#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008389void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008391 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008392}
8393#endif
8394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008395#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008396void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008397{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008398 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008399}
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008400
8401void mbedtls_ssl_conf_extended_master_secret_enforce( mbedtls_ssl_config *conf,
Jarno Lamsa842be162019-06-10 15:05:33 +03008402 char ems_enf )
Jarno Lamsa7a5e2be2019-06-10 10:13:03 +03008403{
8404 conf->enforce_extended_master_secret = ems_enf;
8405}
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008406#endif
8407
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008408#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008409void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008411 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008412}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008413#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008416int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008419 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008422 }
8423
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008424 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008425
8426 return( 0 );
8427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008428#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008430#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008431void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008435#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008438void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008439{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008440 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008441}
8442#endif
8443
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008444void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008446 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008447}
8448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008450void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008451{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008452 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008453}
8454
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008455void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008456{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008457 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008458}
8459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008461 const unsigned char period[8] )
8462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008463 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008464}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008465#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008468#if defined(MBEDTLS_SSL_CLI_C)
8469void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008470{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01008471 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008472}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008473#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02008474
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008475#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008476void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
8477 mbedtls_ssl_ticket_write_t *f_ticket_write,
8478 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
8479 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02008480{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02008481 conf->f_ticket_write = f_ticket_write;
8482 conf->f_ticket_parse = f_ticket_parse;
8483 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02008484}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02008485#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008486#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02008487
Robert Cragie4feb7ae2015-10-02 13:33:37 +01008488#if defined(MBEDTLS_SSL_EXPORT_KEYS)
8489void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
8490 mbedtls_ssl_export_keys_t *f_export_keys,
8491 void *p_export_keys )
8492{
8493 conf->f_export_keys = f_export_keys;
8494 conf->p_export_keys = p_export_keys;
8495}
8496#endif
8497
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008498#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008499void mbedtls_ssl_conf_async_private_cb(
8500 mbedtls_ssl_config *conf,
8501 mbedtls_ssl_async_sign_t *f_async_sign,
8502 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
8503 mbedtls_ssl_async_resume_t *f_async_resume,
8504 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008505 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008506{
8507 conf->f_async_sign_start = f_async_sign;
8508 conf->f_async_decrypt_start = f_async_decrypt;
8509 conf->f_async_resume = f_async_resume;
8510 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008511 conf->p_async_config_data = async_config_data;
8512}
8513
Gilles Peskine8f97af72018-04-26 11:46:10 +02008514void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
8515{
8516 return( conf->p_async_config_data );
8517}
8518
Gilles Peskine1febfef2018-04-30 11:54:39 +02008519void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008520{
8521 if( ssl->handshake == NULL )
8522 return( NULL );
8523 else
8524 return( ssl->handshake->user_async_ctx );
8525}
8526
Gilles Peskine1febfef2018-04-30 11:54:39 +02008527void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02008528 void *ctx )
8529{
8530 if( ssl->handshake != NULL )
8531 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008532}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02008533#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01008534
Paul Bakker5121ce52009-01-03 21:22:43 +00008535/*
8536 * SSL get accessors
8537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008539{
8540 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
8541}
8542
Hanno Becker8b170a02017-10-10 11:51:19 +01008543int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
8544{
8545 /*
8546 * Case A: We're currently holding back
8547 * a message for further processing.
8548 */
8549
8550 if( ssl->keep_current_message == 1 )
8551 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008553 return( 1 );
8554 }
8555
8556 /*
8557 * Case B: Further records are pending in the current datagram.
8558 */
8559
8560#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008561 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Becker8b170a02017-10-10 11:51:19 +01008562 ssl->in_left > ssl->next_record_offset )
8563 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008565 return( 1 );
8566 }
8567#endif /* MBEDTLS_SSL_PROTO_DTLS */
8568
8569 /*
8570 * Case C: A handshake message is being processed.
8571 */
8572
Hanno Becker8b170a02017-10-10 11:51:19 +01008573 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
8574 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008576 return( 1 );
8577 }
8578
8579 /*
8580 * Case D: An application data message is being processed
8581 */
8582 if( ssl->in_offt != NULL )
8583 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01008584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01008585 return( 1 );
8586 }
8587
8588 /*
8589 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01008590 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01008591 * we implement support for multiple alerts in single records.
8592 */
8593
8594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
8595 return( 0 );
8596}
8597
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008598uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008599{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00008600 if( ssl->session != NULL )
8601 return( ssl->session->verify_result );
8602
8603 if( ssl->session_negotiate != NULL )
8604 return( ssl->session_negotiate->verify_result );
8605
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02008606 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00008607}
8608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00008610{
Paul Bakker926c8e42013-03-06 10:23:34 +01008611 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008612 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01008613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008614 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00008615}
8616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008617const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00008618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02008620 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008621 {
8622 switch( ssl->minor_ver )
8623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008625 return( "DTLSv1.0" );
8626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008627 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01008628 return( "DTLSv1.2" );
8629
8630 default:
8631 return( "unknown (DTLS)" );
8632 }
8633 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008634 MBEDTLS_SSL_TRANSPORT_ELSE
8635#endif /* MBEDTLS_SSL_PROTO_DTLS */
8636#if defined(MBEDTLS_SSL_PROTO_TLS)
Paul Bakker43ca69c2011-01-15 17:35:19 +00008637 {
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008638 switch( ssl->minor_ver )
8639 {
8640 case MBEDTLS_SSL_MINOR_VERSION_0:
8641 return( "SSLv3.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008642
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008643 case MBEDTLS_SSL_MINOR_VERSION_1:
8644 return( "TLSv1.0" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008645
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008646 case MBEDTLS_SSL_MINOR_VERSION_2:
8647 return( "TLSv1.1" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00008648
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008649 case MBEDTLS_SSL_MINOR_VERSION_3:
8650 return( "TLSv1.2" );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008651
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008652 default:
8653 return( "unknown" );
8654 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00008655 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02008656#endif /* MBEDTLS_SSL_PROTO_TLS */
Paul Bakker43ca69c2011-01-15 17:35:19 +00008657}
8658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008659int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008660{
Hanno Becker3136ede2018-08-17 15:28:19 +01008661 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008662 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008663 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008664
Hanno Becker43395762019-05-03 14:46:38 +01008665 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
8666
Hanno Becker78640902018-08-13 16:35:15 +01008667 if( transform == NULL )
Hanno Becker43395762019-05-03 14:46:38 +01008668 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01008669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670#if defined(MBEDTLS_ZLIB_SUPPORT)
8671 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
8672 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008673#endif
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008677 case MBEDTLS_MODE_GCM:
8678 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008679 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008681 transform_expansion = transform->minlen;
8682 break;
8683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008684 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01008685
8686 block_size = mbedtls_cipher_get_block_size(
8687 &transform->cipher_ctx_enc );
8688
Hanno Becker3136ede2018-08-17 15:28:19 +01008689 /* Expansion due to the addition of the MAC. */
8690 transform_expansion += transform->maclen;
8691
8692 /* Expansion due to the addition of CBC padding;
8693 * Theoretically up to 256 bytes, but we never use
8694 * more than the block size of the underlying cipher. */
8695 transform_expansion += block_size;
8696
8697 /* For TLS 1.1 or higher, an explicit IV is added
8698 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01008699#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
8700 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01008701 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01008702#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01008703
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008704 break;
8705
8706 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02008707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008709 }
8710
Hanno Beckera5a2b082019-05-15 14:03:01 +01008711#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckeradd01902019-05-08 15:40:11 +01008712 if( transform->out_cid_len != 0 )
8713 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera5a2b082019-05-15 14:03:01 +01008714#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckeradd01902019-05-08 15:40:11 +01008715
Hanno Becker43395762019-05-03 14:46:38 +01008716 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02008717}
8718
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008719#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8720size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
8721{
8722 size_t max_len;
8723
8724 /*
8725 * Assume mfl_code is correct since it was checked when set
8726 */
Angus Grattond8213d02016-05-25 20:56:48 +10008727 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008728
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008729 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008730 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10008731 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008732 {
Angus Grattond8213d02016-05-25 20:56:48 +10008733 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008734 }
8735
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02008736 /* During a handshake, use the value being negotiated */
8737 if( ssl->session_negotiate != NULL &&
8738 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
8739 {
8740 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
8741 }
8742
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008743 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02008744}
8745#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8746
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008747#if defined(MBEDTLS_SSL_PROTO_DTLS)
8748static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
8749{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04008750 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
8751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
8752 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
8753 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
8754 return ( 0 );
8755
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008756 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
8757 return( ssl->mtu );
8758
8759 if( ssl->mtu == 0 )
8760 return( ssl->handshake->mtu );
8761
8762 return( ssl->mtu < ssl->handshake->mtu ?
8763 ssl->mtu : ssl->handshake->mtu );
8764}
8765#endif /* MBEDTLS_SSL_PROTO_DTLS */
8766
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008767int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
8768{
8769 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
8770
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02008771#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8772 !defined(MBEDTLS_SSL_PROTO_DTLS)
8773 (void) ssl;
8774#endif
8775
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008776#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8777 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
8778
8779 if( max_len > mfl )
8780 max_len = mfl;
8781#endif
8782
8783#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008784 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008785 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008786 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008787 const int ret = mbedtls_ssl_get_record_expansion( ssl );
8788 const size_t overhead = (size_t) ret;
8789
8790 if( ret < 0 )
8791 return( ret );
8792
8793 if( mtu <= overhead )
8794 {
8795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
8796 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
8797 }
8798
8799 if( max_len > mtu - overhead )
8800 max_len = mtu - overhead;
8801 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02008802#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008803
Hanno Becker0defedb2018-08-10 12:35:02 +01008804#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
8805 !defined(MBEDTLS_SSL_PROTO_DTLS)
8806 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02008807#endif
8808
8809 return( (int) max_len );
8810}
8811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812#if defined(MBEDTLS_X509_CRT_PARSE_C)
8813const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00008814{
8815 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008816 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008817
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008818 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00008819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008820#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00008821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822#if defined(MBEDTLS_SSL_CLI_C)
Hanno Becker933b9fc2019-02-05 11:42:30 +00008823int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
8824 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008825{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008826 if( ssl == NULL ||
8827 dst == NULL ||
8828 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008829 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008832 }
8833
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008834 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008836#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02008837
Manuel Pégourié-Gonnard37a53242019-05-20 11:12:28 +02008838const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
8839{
8840 if( ssl == NULL )
8841 return( NULL );
8842
8843 return( ssl->session );
8844}
8845
Paul Bakker5121ce52009-01-03 21:22:43 +00008846/*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008847 * Define ticket header determining Mbed TLS version
8848 * and structure of the ticket.
8849 */
8850
Hanno Becker41527622019-05-16 12:50:45 +01008851/*
Hanno Becker26829e92019-05-28 14:30:45 +01008852 * Define bitflag determining compile-time settings influencing
8853 * structure of serialized SSL sessions.
Hanno Becker41527622019-05-16 12:50:45 +01008854 */
8855
Hanno Becker26829e92019-05-28 14:30:45 +01008856#if defined(MBEDTLS_HAVE_TIME)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008857#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker26829e92019-05-28 14:30:45 +01008858#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008859#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker41527622019-05-16 12:50:45 +01008860#endif /* MBEDTLS_HAVE_TIME */
8861
8862#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008863#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker41527622019-05-16 12:50:45 +01008864#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008865#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker41527622019-05-16 12:50:45 +01008866#endif /* MBEDTLS_X509_CRT_PARSE_C */
8867
8868#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008869#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker41527622019-05-16 12:50:45 +01008870#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008871#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker41527622019-05-16 12:50:45 +01008872#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
8873
8874#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008875#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker41527622019-05-16 12:50:45 +01008876#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008877#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker41527622019-05-16 12:50:45 +01008878#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
8879
8880#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008881#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
Hanno Becker41527622019-05-16 12:50:45 +01008882#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008883#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
Hanno Becker41527622019-05-16 12:50:45 +01008884#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
8885
8886#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008887#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker41527622019-05-16 12:50:45 +01008888#else
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008889#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker41527622019-05-16 12:50:45 +01008890#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
8891
Hanno Becker41527622019-05-16 12:50:45 +01008892#if defined(MBEDTLS_SSL_SESSION_TICKETS)
8893#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
8894#else
8895#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
8896#endif /* MBEDTLS_SSL_SESSION_TICKETS */
8897
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008898#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
8899#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
8900#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
8901#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
8902#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
8903#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
8904#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008905
Hanno Becker26829e92019-05-28 14:30:45 +01008906#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Hanno Beckerbaf968c2019-05-29 11:10:18 +01008907 ( (uint16_t) ( \
8908 ( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
8909 ( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
8910 ( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
8911 ( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
8912 ( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
8913 ( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
Hanno Becker7bf77102019-06-04 09:43:16 +01008914 ( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
Hanno Becker41527622019-05-16 12:50:45 +01008915
Hanno Becker557fe9f2019-05-16 12:41:07 +01008916static unsigned char ssl_serialized_session_header[] = {
Hanno Becker41527622019-05-16 12:50:45 +01008917 MBEDTLS_VERSION_MAJOR,
8918 MBEDTLS_VERSION_MINOR,
8919 MBEDTLS_VERSION_PATCH,
Hanno Becker26829e92019-05-28 14:30:45 +01008920 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
8921 ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
Hanno Becker557fe9f2019-05-16 12:41:07 +01008922};
Hanno Beckerb5352f02019-05-16 12:39:07 +01008923
8924/*
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008925 * Serialize a session in the following format:
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008926 * (in the presentation language of TLS, RFC 8446 section 3)
8927 *
Hanno Becker26829e92019-05-28 14:30:45 +01008928 * opaque mbedtls_version[3]; // major, minor, patch
8929 * opaque session_format[2]; // version-specific 16-bit field determining
8930 * // the format of the remaining
8931 * // serialized data.
Hanno Beckerb36db4f2019-05-29 11:08:00 +01008932 *
8933 * Note: When updating the format, remember to keep
8934 * these version+format bytes.
8935 *
Hanno Becker7bf77102019-06-04 09:43:16 +01008936 * // In this version, `session_format` determines
8937 * // the setting of those compile-time
8938 * // configuration options which influence
Hanno Becker26829e92019-05-28 14:30:45 +01008939 * // the structure of mbedtls_ssl_session.
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008940 * uint64 start_time;
Hanno Becker26829e92019-05-28 14:30:45 +01008941 * uint8 ciphersuite[2]; // defined by the standard
8942 * uint8 compression; // 0 or 1
8943 * uint8 session_id_len; // at most 32
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008944 * opaque session_id[32];
Hanno Becker26829e92019-05-28 14:30:45 +01008945 * opaque master[48]; // fixed length in the standard
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008946 * uint32 verify_result;
Hanno Becker26829e92019-05-28 14:30:45 +01008947 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8948 * opaque ticket<0..2^24-1>; // length 0 means no ticket
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008949 * uint32 ticket_lifetime;
Hanno Becker26829e92019-05-28 14:30:45 +01008950 * uint8 mfl_code; // up to 255 according to standard
8951 * uint8 trunc_hmac; // 0 or 1
8952 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008953 *
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008954 * The order is the same as in the definition of the structure, except
8955 * verify_result is put before peer_cert so that all mandatory fields come
8956 * together in one block.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008957 */
8958int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
8959 unsigned char *buf,
8960 size_t buf_len,
8961 size_t *olen )
8962{
8963 unsigned char *p = buf;
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008964 size_t used = 0;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008965#if defined(MBEDTLS_HAVE_TIME)
8966 uint64_t start;
8967#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008968#if defined(MBEDTLS_X509_CRT_PARSE_C)
8969 size_t cert_len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008970#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008971
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008972 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01008973 * Add version identifier
8974 */
8975
8976 used += sizeof( ssl_serialized_session_header );
8977
8978 if( used <= buf_len )
8979 {
8980 memcpy( p, ssl_serialized_session_header,
8981 sizeof( ssl_serialized_session_header ) );
8982 p += sizeof( ssl_serialized_session_header );
8983 }
8984
8985 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008986 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02008987 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008988#if defined(MBEDTLS_HAVE_TIME)
8989 used += 8;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02008990
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02008991 if( used <= buf_len )
8992 {
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02008993 start = (uint64_t) session->start;
8994
8995 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
8996 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
8997 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
8998 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
8999 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9000 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9001 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9002 *p++ = (unsigned char)( ( start ) & 0xFF );
9003 }
9004#endif /* MBEDTLS_HAVE_TIME */
9005
9006 /*
9007 * Basic mandatory fields
9008 */
9009 used += 2 /* ciphersuite */
9010 + 1 /* compression */
9011 + 1 /* id_len */
9012 + sizeof( session->id )
9013 + sizeof( session->master )
9014 + 4; /* verify_result */
9015
9016 if( used <= buf_len )
9017 {
9018 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9019 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9020
9021 *p++ = (unsigned char)( session->compression & 0xFF );
9022
9023 *p++ = (unsigned char)( session->id_len & 0xFF );
9024 memcpy( p, session->id, 32 );
9025 p += 32;
9026
9027 memcpy( p, session->master, 48 );
9028 p += 48;
9029
9030 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9031 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9032 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9033 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009034 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009035
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009036 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009037 * Peer's end-entity certificate
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009038 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009039#if defined(MBEDTLS_X509_CRT_PARSE_C)
9040 if( session->peer_cert == NULL )
9041 cert_len = 0;
9042 else
9043 cert_len = session->peer_cert->raw.len;
9044
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009045 used += 3 + cert_len;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009046
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009047 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009048 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009049 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9050 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9051 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
9052
9053 if( session->peer_cert != NULL )
9054 {
9055 memcpy( p, session->peer_cert->raw.p, cert_len );
9056 p += cert_len;
9057 }
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009058 }
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009059#endif /* MBEDTLS_X509_CRT_PARSE_C */
9060
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009061 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009062 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009063 */
9064#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009065 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009066
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009067 if( used <= buf_len )
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009068 {
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009069 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9070 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9071 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9072
9073 if( session->ticket != NULL )
9074 {
9075 memcpy( p, session->ticket, session->ticket_len );
9076 p += session->ticket_len;
9077 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009078
9079 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
9080 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
9081 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
9082 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009083 }
9084#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9085
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009086 /*
9087 * Misc extension-related info
9088 */
9089#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9090 used += 1;
9091
9092 if( used <= buf_len )
9093 *p++ = session->mfl_code;
9094#endif
9095
9096#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9097 used += 1;
9098
9099 if( used <= buf_len )
9100 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
9101#endif
9102
9103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9104 used += 1;
9105
9106 if( used <= buf_len )
9107 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
9108#endif
9109
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009110 /* Done */
Manuel Pégourié-Gonnard32ce5962019-05-21 11:01:32 +02009111 *olen = used;
9112
9113 if( used > buf_len )
9114 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009115
9116 return( 0 );
9117}
9118
9119/*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009120 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009121 *
9122 * This internal version is wrapped by a public function that cleans up in
9123 * case of error.
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009124 */
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009125static int ssl_session_load( mbedtls_ssl_session *session,
9126 const unsigned char *buf,
9127 size_t len )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009128{
9129 const unsigned char *p = buf;
9130 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009131#if defined(MBEDTLS_HAVE_TIME)
9132 uint64_t start;
9133#endif
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009134#if defined(MBEDTLS_X509_CRT_PARSE_C)
9135 size_t cert_len;
9136#endif /* MBEDTLS_X509_CRT_PARSE_C */
9137
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009138 /*
Hanno Beckerb5352f02019-05-16 12:39:07 +01009139 * Check version identifier
9140 */
9141
9142 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
Hanno Becker1d8b6d72019-05-28 13:59:44 +01009143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009144
9145 if( memcmp( p, ssl_serialized_session_header,
9146 sizeof( ssl_serialized_session_header ) ) != 0 )
9147 {
Hanno Becker5dbcc9f2019-06-03 12:58:39 +01009148 return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerb5352f02019-05-16 12:39:07 +01009149 }
9150 p += sizeof( ssl_serialized_session_header );
9151
9152 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009153 * Time
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009154 */
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009155#if defined(MBEDTLS_HAVE_TIME)
9156 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9158
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009159 start = ( (uint64_t) p[0] << 56 ) |
9160 ( (uint64_t) p[1] << 48 ) |
9161 ( (uint64_t) p[2] << 40 ) |
9162 ( (uint64_t) p[3] << 32 ) |
9163 ( (uint64_t) p[4] << 24 ) |
9164 ( (uint64_t) p[5] << 16 ) |
9165 ( (uint64_t) p[6] << 8 ) |
9166 ( (uint64_t) p[7] );
9167 p += 8;
9168
9169 session->start = (time_t) start;
9170#endif /* MBEDTLS_HAVE_TIME */
9171
9172 /*
9173 * Basic mandatory fields
9174 */
9175 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
9176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9177
9178 session->ciphersuite = ( p[0] << 8 ) | p[1];
9179 p += 2;
9180
9181 session->compression = *p++;
9182
9183 session->id_len = *p++;
9184 memcpy( session->id, p, 32 );
9185 p += 32;
9186
9187 memcpy( session->master, p, 48 );
9188 p += 48;
9189
9190 session->verify_result = ( (uint32_t) p[0] << 24 ) |
9191 ( (uint32_t) p[1] << 16 ) |
9192 ( (uint32_t) p[2] << 8 ) |
9193 ( (uint32_t) p[3] );
9194 p += 4;
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009195
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009196 /* Immediately clear invalid pointer values that have been read, in case
9197 * we exit early before we replaced them with valid ones. */
9198#if defined(MBEDTLS_X509_CRT_PARSE_C)
9199 session->peer_cert = NULL;
9200#endif
9201#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9202 session->ticket = NULL;
9203#endif
9204
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009205 /*
9206 * Peer certificate
9207 */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009208#if defined(MBEDTLS_X509_CRT_PARSE_C)
9209 if( 3 > (size_t)( end - p ) )
9210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9211
9212 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9213 p += 3;
9214
9215 if( cert_len == 0 )
9216 {
9217 session->peer_cert = NULL;
9218 }
9219 else
9220 {
9221 int ret;
9222
9223 if( cert_len > (size_t)( end - p ) )
9224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9225
9226 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
9227
9228 if( session->peer_cert == NULL )
9229 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9230
9231 mbedtls_x509_crt_init( session->peer_cert );
9232
9233 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
9234 p, cert_len ) ) != 0 )
9235 {
9236 mbedtls_x509_crt_free( session->peer_cert );
9237 mbedtls_free( session->peer_cert );
9238 session->peer_cert = NULL;
9239 return( ret );
9240 }
9241
9242 p += cert_len;
9243 }
9244#endif /* MBEDTLS_X509_CRT_PARSE_C */
9245
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009246 /*
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009247 * Session ticket and associated data
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009248 */
9249#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9250 if( 3 > (size_t)( end - p ) )
9251 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9252
9253 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
9254 p += 3;
9255
9256 if( session->ticket_len != 0 )
9257 {
9258 if( session->ticket_len > (size_t)( end - p ) )
9259 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9260
9261 session->ticket = mbedtls_calloc( 1, session->ticket_len );
9262 if( session->ticket == NULL )
9263 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9264
9265 memcpy( session->ticket, p, session->ticket_len );
9266 p += session->ticket_len;
9267 }
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009268
9269 if( 4 > (size_t)( end - p ) )
9270 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9271
9272 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
9273 ( (uint32_t) p[1] << 16 ) |
9274 ( (uint32_t) p[2] << 8 ) |
9275 ( (uint32_t) p[3] );
9276 p += 4;
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009277#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9278
Manuel Pégourié-Gonnard60a42992019-05-24 12:06:29 +02009279 /*
9280 * Misc extension-related info
9281 */
9282#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9283 if( 1 > (size_t)( end - p ) )
9284 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9285
9286 session->mfl_code = *p++;
9287#endif
9288
9289#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
9290 if( 1 > (size_t)( end - p ) )
9291 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9292
9293 session->trunc_hmac = *p++;
9294#endif
9295
9296#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9297 if( 1 > (size_t)( end - p ) )
9298 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9299
9300 session->encrypt_then_mac = *p++;
9301#endif
9302
Manuel Pégourié-Gonnardef4ae612019-05-16 11:11:08 +02009303 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnard91f4ca22019-05-16 10:08:35 +02009304 if( p != end )
9305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9306
9307 return( 0 );
9308}
9309
9310/*
Manuel Pégourié-Gonnard35ccdbb2019-06-03 09:55:16 +02009311 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnard57098112019-05-23 12:28:45 +02009312 */
9313int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
9314 const unsigned char *buf,
9315 size_t len )
9316{
9317 int ret = ssl_session_load( session, buf, len );
9318
9319 if( ret != 0 )
9320 mbedtls_ssl_session_free( session );
9321
9322 return( ret );
9323}
9324
9325/*
Paul Bakker1961b702013-01-25 14:49:24 +01009326 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009331
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009332 if( ssl == NULL || ssl->conf == NULL )
9333 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009336 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009340 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009342#endif
9343
Paul Bakker1961b702013-01-25 14:49:24 +01009344 return( ret );
9345}
9346
9347/*
9348 * Perform the SSL handshake
9349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009351{
9352 int ret = 0;
9353
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009354 if( ssl == NULL || ssl->conf == NULL )
9355 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009362
9363 if( ret != 0 )
9364 break;
9365 }
9366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009368
9369 return( ret );
9370}
9371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009372#if defined(MBEDTLS_SSL_RENEGOTIATION)
9373#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009374/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009375 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009378{
9379 int ret;
9380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009382
9383 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9385 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009386
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009387 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009388 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009390 return( ret );
9391 }
9392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009394
9395 return( 0 );
9396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009398
9399/*
9400 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009401 * - any side: calling mbedtls_ssl_renegotiate(),
9402 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9403 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009404 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009405 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009409{
9410 int ret;
9411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009413
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009414 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9415 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009416
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009417 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9418 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009420 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009422 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009423 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009424 ssl->handshake->out_msg_seq = 1;
9425 else
9426 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009427 }
9428#endif
9429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009430 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9431 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009433 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009436 return( ret );
9437 }
9438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009440
9441 return( 0 );
9442}
9443
9444/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009445 * Renegotiate current connection on client,
9446 * or request renegotiation on server
9447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009450 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009451
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009452 if( ssl == NULL || ssl->conf == NULL )
9453 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009456 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009457 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9460 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009462 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009463
9464 /* Did we already try/start sending HelloRequest? */
9465 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009467
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009468 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009470#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009472#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009473 /*
9474 * On client, either start the renegotiation process or,
9475 * if already in progress, continue the handshake
9476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009479 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9480 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009481
9482 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009484 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009485 return( ret );
9486 }
9487 }
9488 else
9489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009493 return( ret );
9494 }
9495 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009497
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009498 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009499}
9500
9501/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009502 * Check record counters and renegotiate if they're above the limit.
9503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009505{
Andres AG2196c7f2016-12-15 17:01:16 +00009506 size_t ep_len = ssl_ep_len( ssl );
9507 int in_ctr_cmp;
9508 int out_ctr_cmp;
9509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9511 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009512 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009513 {
9514 return( 0 );
9515 }
9516
Andres AG2196c7f2016-12-15 17:01:16 +00009517 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9518 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009519 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009520 ssl->conf->renego_period + ep_len, 8 - ep_len );
9521
9522 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009523 {
9524 return( 0 );
9525 }
9526
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009531
9532/*
9533 * Receive application data decrypted from the SSL layer
9534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009536{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009537 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009538 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009539
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009540 if( ssl == NULL || ssl->conf == NULL )
9541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009546 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009549 return( ret );
9550
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009551 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009553 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009554 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009555 return( ret );
9556 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009557 }
9558#endif
9559
Hanno Becker4a810fb2017-05-24 16:27:30 +01009560 /*
9561 * Check if renegotiation is necessary and/or handshake is
9562 * in process. If yes, perform/continue, and fall through
9563 * if an unexpected packet is received while the client
9564 * is waiting for the ServerHello.
9565 *
9566 * (There is no equivalent to the last condition on
9567 * the server-side as it is not treated as within
9568 * a handshake while waiting for the ClientHello
9569 * after a renegotiation request.)
9570 */
9571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009573 ret = ssl_check_ctr_renegotiate( ssl );
9574 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9575 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009578 return( ret );
9579 }
9580#endif
9581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009585 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9586 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009587 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009589 return( ret );
9590 }
9591 }
9592
Hanno Beckere41158b2017-10-23 13:30:32 +01009593 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009594 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009595 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009596 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009597 if( ssl->f_get_timer != NULL &&
9598 ssl->f_get_timer( ssl->p_timer ) == -1 )
9599 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009600 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009601 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009602
Hanno Becker327c93b2018-08-15 13:56:18 +01009603 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009604 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009605 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9606 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009607
Hanno Becker4a810fb2017-05-24 16:27:30 +01009608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009610 }
9611
9612 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009614 {
9615 /*
9616 * OpenSSL sends empty messages to randomize the IV
9617 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009618 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009621 return( 0 );
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009624 return( ret );
9625 }
9626 }
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009631
Hanno Becker4a810fb2017-05-24 16:27:30 +01009632 /*
9633 * - For client-side, expect SERVER_HELLO_REQUEST.
9634 * - For server-side, expect CLIENT_HELLO.
9635 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9636 */
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009639 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009641 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009644
9645 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009647 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009648 {
9649 continue;
9650 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009651 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009652#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009653#if defined(MBEDTLS_SSL_PROTO_TLS)
9654 {
9655 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9656 }
9657#endif
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009658 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009659#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009660
Hanno Becker4a810fb2017-05-24 16:27:30 +01009661#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009662 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009666
9667 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009669 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Hanno Becker90333da2017-10-10 11:27:13 +01009670 {
9671 continue;
9672 }
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009673 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009674#endif
Manuel Pégourié-Gonnard889bbc72019-06-18 10:56:09 +02009675#if defined(MBEDTLS_SSL_PROTO_TLS)
9676 {
9677 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
9678 }
9679#endif
Paul Bakker48916f92012-09-16 19:57:18 +00009680 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009681#endif /* MBEDTLS_SSL_SRV_C */
9682
Hanno Becker21df7f92017-10-17 11:03:26 +01009683#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009684 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009685 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9686 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9687 ssl->conf->allow_legacy_renegotiation ==
9688 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9689 {
9690 /*
9691 * Accept renegotiation request
9692 */
Paul Bakker48916f92012-09-16 19:57:18 +00009693
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009694 /* DTLS clients need to know renego is server-initiated */
9695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +02009696 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) &&
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009697 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9698 {
9699 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9700 }
9701#endif
9702 ret = ssl_start_renegotiation( ssl );
9703 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9704 ret != 0 )
9705 {
9706 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9707 return( ret );
9708 }
9709 }
9710 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009711#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009712 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009713 /*
9714 * Refuse renegotiation
9715 */
9716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009719#if defined(MBEDTLS_SSL_PROTO_SSL3)
9720 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009721 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009722 /* SSLv3 does not have a "no_renegotiation" warning, so
9723 we send a fatal alert and abort the connection. */
9724 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9725 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9726 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009727 }
9728 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9730#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9731 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9732 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9735 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9736 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009737 {
9738 return( ret );
9739 }
Paul Bakker48916f92012-09-16 19:57:18 +00009740 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009741 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9743 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009747 }
Paul Bakker48916f92012-09-16 19:57:18 +00009748 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009749
Hanno Becker90333da2017-10-10 11:27:13 +01009750 /* At this point, we don't know whether the renegotiation has been
9751 * completed or not. The cases to consider are the following:
9752 * 1) The renegotiation is complete. In this case, no new record
9753 * has been read yet.
9754 * 2) The renegotiation is incomplete because the client received
9755 * an application data record while awaiting the ServerHello.
9756 * 3) The renegotiation is incomplete because the client received
9757 * a non-handshake, non-application data message while awaiting
9758 * the ServerHello.
9759 * In each of these case, looping will be the proper action:
9760 * - For 1), the next iteration will read a new record and check
9761 * if it's application data.
9762 * - For 2), the loop condition isn't satisfied as application data
9763 * is present, hence continue is the same as break
9764 * - For 3), the loop condition is satisfied and read_record
9765 * will re-deliver the message that was held back by the client
9766 * when expecting the ServerHello.
9767 */
9768 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009769 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009770#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009772 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009773 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009774 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009775 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009778 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009780 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009781 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9786 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009789 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009790 }
9791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9795 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009796 }
9797
9798 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009799
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009800 /* We're going to return something now, cancel timer,
9801 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009802 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009803 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009804
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009805#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009806 /* If we requested renego but received AppData, resend HelloRequest.
9807 * Do it now, after setting in_offt, to avoid taking this branch
9808 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009810 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009812 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009813 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009816 return( ret );
9817 }
9818 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009820#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 }
9822
9823 n = ( len < ssl->in_msglen )
9824 ? len : ssl->in_msglen;
9825
9826 memcpy( buf, ssl->in_offt, n );
9827 ssl->in_msglen -= n;
9828
9829 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009830 {
9831 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009832 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009833 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009834 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009835 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009836 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009837 /* more data available */
9838 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009842
Paul Bakker23986e52011-04-24 08:57:21 +00009843 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009844}
9845
9846/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009847 * Send application data to be encrypted by the SSL layer, taking care of max
9848 * fragment length and buffer size.
9849 *
9850 * According to RFC 5246 Section 6.2.1:
9851 *
9852 * Zero-length fragments of Application data MAY be sent as they are
9853 * potentially useful as a traffic analysis countermeasure.
9854 *
9855 * Therefore, it is possible that the input message length is 0 and the
9856 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009857 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009858static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009859 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009860{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009861 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9862 const size_t max_len = (size_t) ret;
9863
9864 if( ret < 0 )
9865 {
9866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9867 return( ret );
9868 }
9869
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009870 if( len > max_len )
9871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009872#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009873 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( ssl->conf->transport ) )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009876 "maximum fragment length: %d > %d",
9877 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009879 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009880 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009881#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009882#if defined(MBEDTLS_SSL_PROTO_TLS)
9883 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009884 len = max_len;
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +02009885 }
9886#endif
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009887 }
Paul Bakker887bd502011-06-08 13:10:54 +00009888
Paul Bakker5121ce52009-01-03 21:22:43 +00009889 if( ssl->out_left != 0 )
9890 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009891 /*
9892 * The user has previously tried to send the data and
9893 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9894 * written. In this case, we expect the high-level write function
9895 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009900 return( ret );
9901 }
9902 }
Paul Bakker887bd502011-06-08 13:10:54 +00009903 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009904 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009905 /*
9906 * The user is trying to send a message the first time, so we need to
9907 * copy the data into the internal buffers and setup the data structure
9908 * to keep track of partial writes
9909 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009910 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009912 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009913
Hanno Becker67bc7c32018-08-06 11:33:50 +01009914 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009917 return( ret );
9918 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009919 }
9920
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009921 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009922}
9923
9924/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009925 * Write application data, doing 1/n-1 splitting if necessary.
9926 *
9927 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009928 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009929 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009932static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009933 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009934{
9935 int ret;
9936
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009937 if( ssl->conf->cbc_record_splitting ==
9938 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009939 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9941 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9942 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009943 {
9944 return( ssl_write_real( ssl, buf, len ) );
9945 }
9946
9947 if( ssl->split_done == 0 )
9948 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009949 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009950 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009951 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009952 }
9953
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009954 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9955 return( ret );
9956 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009957
9958 return( ret + 1 );
9959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009961
9962/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009963 * Write application data (public-facing wrapper)
9964 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009965int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009966{
9967 int ret;
9968
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009970
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009971 if( ssl == NULL || ssl->conf == NULL )
9972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9973
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009974#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009975 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
9976 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009977 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009978 return( ret );
9979 }
9980#endif
9981
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009982 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009983 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009984 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009985 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02009986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009987 return( ret );
9988 }
9989 }
9990
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009991#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009992 ret = ssl_write_split( ssl, buf, len );
9993#else
9994 ret = ssl_write_real( ssl, buf, len );
9995#endif
9996
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009998
9999 return( ret );
10000}
10001
10002/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010003 * Notify the peer that the connection is being closed
10004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010006{
10007 int ret;
10008
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010009 if( ssl == NULL || ssl->conf == NULL )
10010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010013
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010014 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010015 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010017 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010019 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10020 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10021 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 return( ret );
10025 }
10026 }
10027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010030 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031}
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010034{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010035 if( transform == NULL )
10036 return;
10037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010039 deflateEnd( &transform->ctx_deflate );
10040 inflateEnd( &transform->ctx_inflate );
10041#endif
10042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010043 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10044 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010045
Hanno Becker92231322018-01-03 15:32:51 +000010046#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047 mbedtls_md_free( &transform->md_ctx_enc );
10048 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Becker92231322018-01-03 15:32:51 +000010049#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010050
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010051 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010052}
10053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
10055static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010058
10059 while( cur != NULL )
10060 {
10061 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010063 cur = next;
10064 }
10065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010067
Hanno Becker0271f962018-08-16 13:23:47 +010010068#if defined(MBEDTLS_SSL_PROTO_DTLS)
10069
10070static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10071{
10072 unsigned offset;
10073 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10074
10075 if( hs == NULL )
10076 return;
10077
Hanno Becker283f5ef2018-08-24 09:34:47 +010010078 ssl_free_buffered_record( ssl );
10079
Hanno Becker0271f962018-08-16 13:23:47 +010010080 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010081 ssl_buffering_free_slot( ssl, offset );
10082}
10083
10084static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10085 uint8_t slot )
10086{
10087 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10088 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010089
10090 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10091 return;
10092
Hanno Beckere605b192018-08-21 15:59:07 +010010093 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010094 {
Hanno Beckere605b192018-08-21 15:59:07 +010010095 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010096 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010097 mbedtls_free( hs_buf->data );
10098 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010099 }
10100}
10101
10102#endif /* MBEDTLS_SSL_PROTO_DTLS */
10103
Gilles Peskine9b562d52018-04-25 20:32:43 +020010104void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010105{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010106 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10107
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010108 if( handshake == NULL )
10109 return;
10110
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010111#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10112 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10113 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010114 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010115 handshake->async_in_progress = 0;
10116 }
10117#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10118
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010119#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10120 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10121 mbedtls_md5_free( &handshake->fin_md5 );
10122 mbedtls_sha1_free( &handshake->fin_sha1 );
10123#endif
10124#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10125#if defined(MBEDTLS_SHA256_C)
10126 mbedtls_sha256_free( &handshake->fin_sha256 );
10127#endif
10128#if defined(MBEDTLS_SHA512_C)
10129 mbedtls_sha512_free( &handshake->fin_sha512 );
10130#endif
10131#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133#if defined(MBEDTLS_DHM_C)
10134 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010135#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#if defined(MBEDTLS_ECDH_C)
10137 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010138#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010139#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010140 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010141#if defined(MBEDTLS_SSL_CLI_C)
10142 mbedtls_free( handshake->ecjpake_cache );
10143 handshake->ecjpake_cache = NULL;
10144 handshake->ecjpake_cache_len = 0;
10145#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010146#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010147
Janos Follath4ae5c292016-02-10 11:27:43 +000010148#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10149 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010150 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010151 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010152#endif
10153
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010154#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10155 if( handshake->psk != NULL )
10156 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010157 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010158 mbedtls_free( handshake->psk );
10159 }
10160#endif
10161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010162#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10163 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010164 /*
10165 * Free only the linked list wrapper, not the keys themselves
10166 * since the belong to the SNI callback
10167 */
10168 if( handshake->sni_key_cert != NULL )
10169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010170 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010171
10172 while( cur != NULL )
10173 {
10174 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010175 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010176 cur = next;
10177 }
10178 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010179#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010180
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010181#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010182 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010183#endif
10184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010185#if defined(MBEDTLS_SSL_PROTO_DTLS)
10186 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010187 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010188 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010189#endif
10190
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010191 mbedtls_platform_zeroize( handshake,
10192 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010193}
10194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010196{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010197 if( session == NULL )
10198 return;
10199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker22141592019-02-05 12:38:15 +000010201 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010202#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010203
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010204#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010206#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010207
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010208 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010209}
10210
Paul Bakker5121ce52009-01-03 21:22:43 +000010211/*
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010212 * Serialize a full SSL context
10213 */
10214int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
10215 unsigned char *buf,
10216 size_t buf_len,
10217 size_t *olen )
10218{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010219 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010220 (void) ssl;
10221
10222 if( buf != NULL )
10223 memset( buf, 0, buf_len );
10224
10225 *olen = 0;
10226
10227 return( 0 );
10228}
10229
10230/*
10231 * Deserialize a full SSL context
10232 */
10233int mbedtls_ssl_context_load( mbedtls_ssl_context *ssl,
10234 const unsigned char *buf,
10235 size_t len )
10236{
Manuel Pégourié-Gonnardcc71c772019-06-05 09:47:18 +020010237 /* Unimplemented */
Manuel Pégourié-Gonnardd87601e2019-05-28 13:02:16 +020010238 (void) ssl;
10239 (void) buf;
10240 (void) len;
10241
10242 return( 0 );
10243}
10244
10245/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010246 * Free an SSL context
10247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010249{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010250 if( ssl == NULL )
10251 return;
10252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010254
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010255 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010256 {
Angus Grattond8213d02016-05-25 20:56:48 +100010257 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010258 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010259 }
10260
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010261 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010262 {
Angus Grattond8213d02016-05-25 20:56:48 +100010263 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010265 }
10266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010267#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010268 if( ssl->compress_buf != NULL )
10269 {
Angus Grattond8213d02016-05-25 20:56:48 +100010270 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010272 }
10273#endif
10274
Paul Bakker48916f92012-09-16 19:57:18 +000010275 if( ssl->transform )
10276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 mbedtls_ssl_transform_free( ssl->transform );
10278 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010279 }
10280
10281 if( ssl->handshake )
10282 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010283 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010284 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10285 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010287 mbedtls_free( ssl->handshake );
10288 mbedtls_free( ssl->transform_negotiate );
10289 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010290 }
10291
Paul Bakkerc0463502013-02-14 11:19:38 +010010292 if( ssl->session )
10293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010294 mbedtls_ssl_session_free( ssl->session );
10295 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010296 }
10297
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010298#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010299 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010300 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010301 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010302 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010303 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10307 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10310 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010311 }
10312#endif
10313
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010314#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010316#endif
10317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010319
Paul Bakker86f04f42013-02-14 11:20:09 +010010320 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010321 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322}
10323
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010324/*
10325 * Initialze mbedtls_ssl_config
10326 */
10327void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10328{
10329 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnarde744eab2019-03-18 10:51:18 +010010330
10331#if !defined(MBEDTLS_SSL_PROTO_TLS)
10332 conf->transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
10333#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010334}
10335
Simon Butcherc97b6972015-12-27 23:48:17 +000010336#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010337static int ssl_preset_default_hashes[] = {
10338#if defined(MBEDTLS_SHA512_C)
10339 MBEDTLS_MD_SHA512,
10340 MBEDTLS_MD_SHA384,
10341#endif
10342#if defined(MBEDTLS_SHA256_C)
10343 MBEDTLS_MD_SHA256,
10344 MBEDTLS_MD_SHA224,
10345#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010346#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010347 MBEDTLS_MD_SHA1,
10348#endif
10349 MBEDTLS_MD_NONE
10350};
Simon Butcherc97b6972015-12-27 23:48:17 +000010351#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010352
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010353static int ssl_preset_suiteb_ciphersuites[] = {
10354 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10355 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10356 0
10357};
10358
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010359#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010360static int ssl_preset_suiteb_hashes[] = {
10361 MBEDTLS_MD_SHA256,
10362 MBEDTLS_MD_SHA384,
10363 MBEDTLS_MD_NONE
10364};
10365#endif
10366
10367#if defined(MBEDTLS_ECP_C)
10368static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10369 MBEDTLS_ECP_DP_SECP256R1,
10370 MBEDTLS_ECP_DP_SECP384R1,
10371 MBEDTLS_ECP_DP_NONE
10372};
10373#endif
10374
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010375/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010376 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010377 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010378int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010379 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010380{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010381#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010382 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010383#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010384
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010385 /* Use the functions here so that they are covered in tests,
10386 * but otherwise access member directly for efficiency */
10387 mbedtls_ssl_conf_endpoint( conf, endpoint );
10388 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010389
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010390 /*
10391 * Things that are common to all presets
10392 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010393#if defined(MBEDTLS_SSL_CLI_C)
10394 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10395 {
10396 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10397#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10398 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10399#endif
10400 }
10401#endif
10402
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010403#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010404 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010405#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010406
10407#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10408 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10409#endif
10410
10411#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10412 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
Jarno Lamsad9382f82019-06-10 10:27:14 +030010413 conf->enforce_extended_master_secret =
Jarno Lamsa18b9a492019-06-10 15:23:29 +030010414 MBEDTLS_SSL_EXTENDED_MS_ENFORCE_DISABLED;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010415#endif
10416
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010417#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10418 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10419#endif
10420
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010421#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422 conf->f_cookie_write = ssl_cookie_write_dummy;
10423 conf->f_cookie_check = ssl_cookie_check_dummy;
10424#endif
10425
10426#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10427 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10428#endif
10429
Janos Follath088ce432017-04-10 12:42:31 +010010430#if defined(MBEDTLS_SSL_SRV_C)
10431 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10432#endif
10433
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010434#if defined(MBEDTLS_SSL_PROTO_DTLS)
10435 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10436 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10437#endif
10438
10439#if defined(MBEDTLS_SSL_RENEGOTIATION)
10440 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010441 memset( conf->renego_period, 0x00, 2 );
10442 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010443#endif
10444
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010445#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10446 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10447 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010448 const unsigned char dhm_p[] =
10449 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10450 const unsigned char dhm_g[] =
10451 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10452
Hanno Beckera90658f2017-10-04 15:29:08 +010010453 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10454 dhm_p, sizeof( dhm_p ),
10455 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010456 {
10457 return( ret );
10458 }
10459 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010460#endif
10461
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010462 /*
10463 * Preset-specific defaults
10464 */
10465 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010466 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010467 /*
10468 * NSA Suite B
10469 */
10470 case MBEDTLS_SSL_PRESET_SUITEB:
10471 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10472 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10473 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10474 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10475
10476 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10477 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10478 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10479 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10480 ssl_preset_suiteb_ciphersuites;
10481
10482#if defined(MBEDTLS_X509_CRT_PARSE_C)
10483 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010484#endif
10485
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010486#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010487 conf->sig_hashes = ssl_preset_suiteb_hashes;
10488#endif
10489
10490#if defined(MBEDTLS_ECP_C)
10491 conf->curve_list = ssl_preset_suiteb_curves;
10492#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010493 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010494
10495 /*
10496 * Default
10497 */
10498 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010499 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10500 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10501 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10502 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10503 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10504 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10505 MBEDTLS_SSL_MIN_MINOR_VERSION :
10506 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10508 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10509
10510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard64c16812019-06-06 12:43:51 +020010511 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10513#endif
10514
10515 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10516 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10517 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10518 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10519 mbedtls_ssl_list_ciphersuites();
10520
10521#if defined(MBEDTLS_X509_CRT_PARSE_C)
10522 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10523#endif
10524
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010525#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010526 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010527#endif
10528
10529#if defined(MBEDTLS_ECP_C)
10530 conf->curve_list = mbedtls_ecp_grp_id_list();
10531#endif
10532
10533#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10534 conf->dhm_min_bitlen = 1024;
10535#endif
10536 }
10537
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010538 return( 0 );
10539}
10540
10541/*
10542 * Free mbedtls_ssl_config
10543 */
10544void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10545{
10546#if defined(MBEDTLS_DHM_C)
10547 mbedtls_mpi_free( &conf->dhm_P );
10548 mbedtls_mpi_free( &conf->dhm_G );
10549#endif
10550
10551#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10552 if( conf->psk != NULL )
10553 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010554 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010555 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010556 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010557 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010558 }
10559
10560 if( conf->psk_identity != NULL )
10561 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010562 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010563 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010564 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010565 conf->psk_identity_len = 0;
10566 }
10567#endif
10568
10569#if defined(MBEDTLS_X509_CRT_PARSE_C)
10570 ssl_key_cert_free( conf->key_cert );
10571#endif
10572
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010573 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010574}
10575
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010576#if defined(MBEDTLS_PK_C) && \
10577 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010578/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010581unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010582{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010583#if defined(MBEDTLS_RSA_C)
10584 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10585 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010587#if defined(MBEDTLS_ECDSA_C)
10588 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10589 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010591 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010592}
10593
Hanno Becker7e5437a2017-04-28 17:15:26 +010010594unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10595{
10596 switch( type ) {
10597 case MBEDTLS_PK_RSA:
10598 return( MBEDTLS_SSL_SIG_RSA );
10599 case MBEDTLS_PK_ECDSA:
10600 case MBEDTLS_PK_ECKEY:
10601 return( MBEDTLS_SSL_SIG_ECDSA );
10602 default:
10603 return( MBEDTLS_SSL_SIG_ANON );
10604 }
10605}
10606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010608{
10609 switch( sig )
10610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010611#if defined(MBEDTLS_RSA_C)
10612 case MBEDTLS_SSL_SIG_RSA:
10613 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010615#if defined(MBEDTLS_ECDSA_C)
10616 case MBEDTLS_SSL_SIG_ECDSA:
10617 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010618#endif
10619 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010621 }
10622}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010623#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010624
Hanno Becker7e5437a2017-04-28 17:15:26 +010010625#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10626 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10627
10628/* Find an entry in a signature-hash set matching a given hash algorithm. */
10629mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10630 mbedtls_pk_type_t sig_alg )
10631{
10632 switch( sig_alg )
10633 {
10634 case MBEDTLS_PK_RSA:
10635 return( set->rsa );
10636 case MBEDTLS_PK_ECDSA:
10637 return( set->ecdsa );
10638 default:
10639 return( MBEDTLS_MD_NONE );
10640 }
10641}
10642
10643/* Add a signature-hash-pair to a signature-hash set */
10644void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10645 mbedtls_pk_type_t sig_alg,
10646 mbedtls_md_type_t md_alg )
10647{
10648 switch( sig_alg )
10649 {
10650 case MBEDTLS_PK_RSA:
10651 if( set->rsa == MBEDTLS_MD_NONE )
10652 set->rsa = md_alg;
10653 break;
10654
10655 case MBEDTLS_PK_ECDSA:
10656 if( set->ecdsa == MBEDTLS_MD_NONE )
10657 set->ecdsa = md_alg;
10658 break;
10659
10660 default:
10661 break;
10662 }
10663}
10664
10665/* Allow exactly one hash algorithm for each signature. */
10666void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10667 mbedtls_md_type_t md_alg )
10668{
10669 set->rsa = md_alg;
10670 set->ecdsa = md_alg;
10671}
10672
10673#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10674 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10675
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010676/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010677 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010680{
10681 switch( hash )
10682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010683#if defined(MBEDTLS_MD5_C)
10684 case MBEDTLS_SSL_HASH_MD5:
10685 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010686#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010687#if defined(MBEDTLS_SHA1_C)
10688 case MBEDTLS_SSL_HASH_SHA1:
10689 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010690#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691#if defined(MBEDTLS_SHA256_C)
10692 case MBEDTLS_SSL_HASH_SHA224:
10693 return( MBEDTLS_MD_SHA224 );
10694 case MBEDTLS_SSL_HASH_SHA256:
10695 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010697#if defined(MBEDTLS_SHA512_C)
10698 case MBEDTLS_SSL_HASH_SHA384:
10699 return( MBEDTLS_MD_SHA384 );
10700 case MBEDTLS_SSL_HASH_SHA512:
10701 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010702#endif
10703 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010705 }
10706}
10707
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010708/*
10709 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10710 */
10711unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10712{
10713 switch( md )
10714 {
10715#if defined(MBEDTLS_MD5_C)
10716 case MBEDTLS_MD_MD5:
10717 return( MBEDTLS_SSL_HASH_MD5 );
10718#endif
10719#if defined(MBEDTLS_SHA1_C)
10720 case MBEDTLS_MD_SHA1:
10721 return( MBEDTLS_SSL_HASH_SHA1 );
10722#endif
10723#if defined(MBEDTLS_SHA256_C)
10724 case MBEDTLS_MD_SHA224:
10725 return( MBEDTLS_SSL_HASH_SHA224 );
10726 case MBEDTLS_MD_SHA256:
10727 return( MBEDTLS_SSL_HASH_SHA256 );
10728#endif
10729#if defined(MBEDTLS_SHA512_C)
10730 case MBEDTLS_MD_SHA384:
10731 return( MBEDTLS_SSL_HASH_SHA384 );
10732 case MBEDTLS_MD_SHA512:
10733 return( MBEDTLS_SSL_HASH_SHA512 );
10734#endif
10735 default:
10736 return( MBEDTLS_SSL_HASH_NONE );
10737 }
10738}
10739
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010740#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010741/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010742 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010743 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010744 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010745int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010746{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010748
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010749 if( ssl->conf->curve_list == NULL )
10750 return( -1 );
10751
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010752 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010753 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010754 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010755
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010756 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010758#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010759
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010760#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010761/*
10762 * Check if a hash proposed by the peer is in our list.
10763 * Return 0 if we're willing to use it, -1 otherwise.
10764 */
10765int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10766 mbedtls_md_type_t md )
10767{
10768 const int *cur;
10769
10770 if( ssl->conf->sig_hashes == NULL )
10771 return( -1 );
10772
10773 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10774 if( *cur == (int) md )
10775 return( 0 );
10776
10777 return( -1 );
10778}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010779#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_X509_CRT_PARSE_C)
10782int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10783 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010784 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010785 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010786{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010787 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010789 int usage = 0;
10790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010791#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010792 const char *ext_oid;
10793 size_t ext_len;
10794#endif
10795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10797 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010798 ((void) cert);
10799 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010800 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010801#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010803#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10804 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010805 {
10806 /* Server part of the key exchange */
10807 switch( ciphersuite->key_exchange )
10808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010809 case MBEDTLS_KEY_EXCHANGE_RSA:
10810 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010811 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010812 break;
10813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010814 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10815 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10816 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10817 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010818 break;
10819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10821 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010822 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010823 break;
10824
10825 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010826 case MBEDTLS_KEY_EXCHANGE_NONE:
10827 case MBEDTLS_KEY_EXCHANGE_PSK:
10828 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10829 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010830 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010831 usage = 0;
10832 }
10833 }
10834 else
10835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10837 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010838 }
10839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010840 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010841 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010842 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010843 ret = -1;
10844 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010845#else
10846 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10850 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10853 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010854 }
10855 else
10856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10858 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010859 }
10860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010862 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010863 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010864 ret = -1;
10865 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010866#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010867
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010868 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010871
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010872/*
10873 * Convert version numbers to/from wire format
10874 * and, for DTLS, to/from TLS equivalent.
10875 *
10876 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010877 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010878 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10879 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010882 unsigned char ver[2] )
10883{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010884#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10885 ((void) transport);
10886#endif
10887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010889 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010891 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010892 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10893
10894 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10895 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10896 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010897 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010898#endif
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010899#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010900 {
10901 ver[0] = (unsigned char) major;
10902 ver[1] = (unsigned char) minor;
10903 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010904#endif
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010905}
10906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010907void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010908 const unsigned char ver[2] )
10909{
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010910#if !defined(MBEDTLS_SSL_TRANSPORT__BOTH)
10911 ((void) transport);
10912#endif
10913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010915 if( MBEDTLS_SSL_TRANSPORT_IS_DTLS( transport ) )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010916 {
10917 *major = 255 - ver[0] + 2;
10918 *minor = 255 - ver[1] + 1;
10919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010920 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010921 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10922 }
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010923 MBEDTLS_SSL_TRANSPORT_ELSE
Manuel Pégourié-Gonnard8794a422019-06-11 10:04:57 +020010924#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardff4bd9f2019-06-06 10:34:48 +020010925#if defined(MBEDTLS_SSL_PROTO_TLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010926 {
10927 *major = ver[0];
10928 *minor = ver[1];
10929 }
Manuel Pégourié-Gonnardec1c2222019-06-12 10:18:26 +020010930#endif /* MBEDTLS_SSL_PROTO_TLS */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010931}
10932
Simon Butcher99000142016-10-13 17:21:01 +010010933int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10934{
10935#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10936 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10937 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10938
10939 switch( md )
10940 {
10941#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10942#if defined(MBEDTLS_MD5_C)
10943 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010944 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010945#endif
10946#if defined(MBEDTLS_SHA1_C)
10947 case MBEDTLS_SSL_HASH_SHA1:
10948 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10949 break;
10950#endif
10951#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10952#if defined(MBEDTLS_SHA512_C)
10953 case MBEDTLS_SSL_HASH_SHA384:
10954 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10955 break;
10956#endif
10957#if defined(MBEDTLS_SHA256_C)
10958 case MBEDTLS_SSL_HASH_SHA256:
10959 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10960 break;
10961#endif
10962 default:
10963 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10964 }
10965
10966 return 0;
10967#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10968 (void) ssl;
10969 (void) md;
10970
10971 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10973}
10974
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010975#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10976 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10977int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10978 unsigned char *output,
10979 unsigned char *data, size_t data_len )
10980{
10981 int ret = 0;
10982 mbedtls_md5_context mbedtls_md5;
10983 mbedtls_sha1_context mbedtls_sha1;
10984
10985 mbedtls_md5_init( &mbedtls_md5 );
10986 mbedtls_sha1_init( &mbedtls_sha1 );
10987
10988 /*
10989 * digitally-signed struct {
10990 * opaque md5_hash[16];
10991 * opaque sha_hash[20];
10992 * };
10993 *
10994 * md5_hash
10995 * MD5(ClientHello.random + ServerHello.random
10996 * + ServerParams);
10997 * sha_hash
10998 * SHA(ClientHello.random + ServerHello.random
10999 * + ServerParams);
11000 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011001 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011002 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011004 goto exit;
11005 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011006 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011007 ssl->handshake->randbytes, 64 ) ) != 0 )
11008 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011009 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011010 goto exit;
11011 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011012 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011013 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011014 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011015 goto exit;
11016 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011017 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011018 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011020 goto exit;
11021 }
11022
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011023 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011024 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011025 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011026 goto exit;
11027 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011028 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011029 ssl->handshake->randbytes, 64 ) ) != 0 )
11030 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011032 goto exit;
11033 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011034 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011035 data_len ) ) != 0 )
11036 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011037 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011038 goto exit;
11039 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011040 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011041 output + 16 ) ) != 0 )
11042 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011044 goto exit;
11045 }
11046
11047exit:
11048 mbedtls_md5_free( &mbedtls_md5 );
11049 mbedtls_sha1_free( &mbedtls_sha1 );
11050
11051 if( ret != 0 )
11052 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11053 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11054
11055 return( ret );
11056
11057}
11058#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11059 MBEDTLS_SSL_PROTO_TLS1_1 */
11060
11061#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11062 defined(MBEDTLS_SSL_PROTO_TLS1_2)
11063int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011064 unsigned char *hash, size_t *hashlen,
11065 unsigned char *data, size_t data_len,
11066 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011067{
11068 int ret = 0;
11069 mbedtls_md_context_t ctx;
11070 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011071 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011072
11073 mbedtls_md_init( &ctx );
11074
11075 /*
11076 * digitally-signed struct {
11077 * opaque client_random[32];
11078 * opaque server_random[32];
11079 * ServerDHParams params;
11080 * };
11081 */
11082 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11083 {
11084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11085 goto exit;
11086 }
11087 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11088 {
11089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11090 goto exit;
11091 }
11092 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11093 {
11094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11095 goto exit;
11096 }
11097 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11098 {
11099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11100 goto exit;
11101 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011102 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011103 {
11104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11105 goto exit;
11106 }
11107
11108exit:
11109 mbedtls_md_free( &ctx );
11110
11111 if( ret != 0 )
11112 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11113 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11114
11115 return( ret );
11116}
11117#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11118 MBEDTLS_SSL_PROTO_TLS1_2 */
11119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011120#endif /* MBEDTLS_SSL_TLS_C */